Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1 | // Copyright 2014 Google Inc. All rights reserved. |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | package com.google.devtools.build.lib.analysis; |
| 16 | |
| 17 | import com.google.common.base.Preconditions; |
| 18 | import com.google.common.collect.ImmutableMap; |
| 19 | import com.google.common.collect.UnmodifiableIterator; |
| 20 | import com.google.devtools.build.lib.concurrent.ThreadSafety.Immutable; |
| 21 | |
| 22 | import java.util.LinkedHashMap; |
| 23 | import java.util.Map; |
| 24 | |
| 25 | /** |
| 26 | * Extra information about a configured target computed on request of a dependent. |
| 27 | * |
| 28 | * <p>Analogous to {@link ConfiguredTarget}: contains a bunch of transitive info providers, which |
| 29 | * are merged with the providers of the associated configured target before they are passed to |
| 30 | * the configured target factories that depend on the configured target to which this aspect is |
| 31 | * added. |
| 32 | * |
| 33 | * <p>Aspects are created alongside configured targets on request from dependents. |
| 34 | */ |
| 35 | @Immutable |
| 36 | public final class Aspect implements Iterable<TransitiveInfoProvider> { |
| 37 | private final |
| 38 | ImmutableMap<Class<? extends TransitiveInfoProvider>, TransitiveInfoProvider> providers; |
| 39 | |
| 40 | private Aspect( |
| 41 | ImmutableMap<Class<? extends TransitiveInfoProvider>, TransitiveInfoProvider> providers) { |
| 42 | this.providers = providers; |
| 43 | } |
| 44 | |
| 45 | /** |
| 46 | * Returns the providers created by the aspect. |
| 47 | */ |
| 48 | public ImmutableMap<Class<? extends TransitiveInfoProvider>, TransitiveInfoProvider> |
| 49 | getProviders() { |
| 50 | return providers; |
| 51 | } |
| 52 | |
| 53 | @Override |
| 54 | public UnmodifiableIterator<TransitiveInfoProvider> iterator() { |
| 55 | return providers.values().iterator(); |
| 56 | } |
| 57 | |
| 58 | /** |
| 59 | * Builder for {@link Aspect}. |
| 60 | */ |
| 61 | public static class Builder { |
| 62 | private final Map<Class<? extends TransitiveInfoProvider>, TransitiveInfoProvider> |
| 63 | providers = new LinkedHashMap<>(); |
| 64 | |
| 65 | /** |
| 66 | * Adds a provider to the aspect. |
| 67 | */ |
| 68 | public Builder addProvider( |
| 69 | Class<? extends TransitiveInfoProvider> key, TransitiveInfoProvider value) { |
| 70 | Preconditions.checkNotNull(key); |
| 71 | Preconditions.checkNotNull(value); |
| 72 | AnalysisUtils.checkProvider(key); |
| 73 | Preconditions.checkState(!providers.containsKey(key)); |
| 74 | providers.put(key, value); |
| 75 | return this; |
| 76 | } |
| 77 | |
| 78 | public Aspect build() { |
| 79 | return new Aspect(ImmutableMap.copyOf(providers)); |
| 80 | } |
| 81 | } |
| 82 | } |