blob: 4ac5689ea8178ac4a1996e568de12c0c53bc65f8 [file] [log] [blame]
Googler4b7aae42017-05-04 17:16:09 -04001// Copyright 2017 The Bazel Authors. 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
15package com.google.devtools.build.lib.analysis;
16
tomlua155b532017-11-08 20:12:47 +010017import com.google.common.base.Preconditions;
adonovana11e2d02019-12-06 07:11:35 -080018import com.google.devtools.build.lib.packages.Info;
dslomovde965ac2017-07-31 21:07:51 +020019import com.google.devtools.build.lib.packages.Provider;
Kurt Alfred Kluever4192ca62022-07-05 06:30:29 -070020import com.google.errorprone.annotations.CanIgnoreReturnValue;
Googler4b7aae42017-05-04 17:16:09 -040021import java.util.Arrays;
22import java.util.LinkedHashMap;
23import javax.annotation.Nullable;
24
25/** A builder for {@link TransitiveInfoProviderMap}. */
26public class TransitiveInfoProviderMapBuilder {
27
28 // TODO(arielb): share the instance with the outerclass and copy on write instead?
dslomovf6a7e5a2017-07-05 07:23:31 -040029 private final LinkedHashMap<Object, Object> providers = new LinkedHashMap<>();
Googler4b7aae42017-05-04 17:16:09 -040030
31 /**
32 * Returns <tt>true</tt> if a {@link TransitiveInfoProvider} has been added for the class
33 * provided.
34 */
35 public boolean contains(Class<? extends TransitiveInfoProvider> providerClass) {
36 return providers.containsKey(providerClass);
37 }
38
dslomovf6a7e5a2017-07-05 07:23:31 -040039 public boolean contains(String legacyId) {
40 return providers.containsKey(legacyId);
41 }
42
dslomovde965ac2017-07-31 21:07:51 +020043 public boolean contains(Provider.Key key) {
dslomovf6a7e5a2017-07-05 07:23:31 -040044 return providers.containsKey(key);
45 }
46
Kurt Alfred Kluever4192ca62022-07-05 06:30:29 -070047 @CanIgnoreReturnValue
Googler4b7aae42017-05-04 17:16:09 -040048 public <T extends TransitiveInfoProvider> TransitiveInfoProviderMapBuilder put(
49 Class<? extends T> providerClass, T provider) {
50 Preconditions.checkNotNull(providerClass);
51 Preconditions.checkNotNull(provider);
dslomovde965ac2017-07-31 21:07:51 +020052 Preconditions.checkState(
adonovana11e2d02019-12-06 07:11:35 -080053 !(provider instanceof Info), "Expose %s as native declared provider", providerClass);
dslomov11da2202017-07-27 23:48:56 +020054
Googler4b7aae42017-05-04 17:16:09 -040055 // TODO(arielb): throw an exception if the providerClass is already present?
56 // This is enforced by aspects but RuleConfiguredTarget presents violations
57 // particularly around LicensesProvider
58 providers.put(providerClass, provider);
59 return this;
60 }
61
Kurt Alfred Kluever4192ca62022-07-05 06:30:29 -070062 @CanIgnoreReturnValue
adonovana11e2d02019-12-06 07:11:35 -080063 public TransitiveInfoProviderMapBuilder put(Info classObject) {
dslomovf6a7e5a2017-07-05 07:23:31 -040064 Preconditions.checkNotNull(classObject);
plfd7ad8d12021-09-17 07:12:59 -070065 // TODO(bazel-team): VisibilityProvider should be migrated to Info to avoid the
66 // PackageSpecificationInfo check. Perhaps as part of a wider effort to migrate all native
67 // TransitiveInfoProviders to Info.
68 Preconditions.checkState(
69 !(classObject instanceof TransitiveInfoProvider)
70 || classObject.getProvider().getPrintableName().equals("PackageSpecificationInfo"),
dslomov11da2202017-07-27 23:48:56 +020071 "Declared provider %s should not implement TransitiveInfoProvider",
72 classObject.getClass());
73
dslomovde965ac2017-07-31 21:07:51 +020074 providers.put(classObject.getProvider().getKey(), classObject);
dslomovf6a7e5a2017-07-05 07:23:31 -040075 return this;
76 }
77
Kurt Alfred Kluever4192ca62022-07-05 06:30:29 -070078 @CanIgnoreReturnValue
dslomovf6a7e5a2017-07-05 07:23:31 -040079 public TransitiveInfoProviderMapBuilder put(String legacyKey, Object classObject) {
80 Preconditions.checkNotNull(legacyKey);
81 Preconditions.checkNotNull(classObject);
82 providers.put(legacyKey, classObject);
83 return this;
84 }
85
86
Googler4b7aae42017-05-04 17:16:09 -040087 public TransitiveInfoProviderMapBuilder add(TransitiveInfoProvider provider) {
88 return put(TransitiveInfoProviderEffectiveClassHelper.get(provider), provider);
89 }
90
91 public TransitiveInfoProviderMapBuilder add(TransitiveInfoProvider... providers) {
92 return addAll(Arrays.asList(providers));
93 }
94
Kurt Alfred Kluever4192ca62022-07-05 06:30:29 -070095 @CanIgnoreReturnValue
dslomovf6a7e5a2017-07-05 07:23:31 -040096 public TransitiveInfoProviderMapBuilder addAll(TransitiveInfoProviderMap other) {
97 for (int i = 0; i < other.getProviderCount(); ++i) {
98 providers.put(other.getProviderKeyAt(i), other.getProviderInstanceAt(i));
Googler4e0a5cb2017-04-09 17:58:18 -040099 }
100 return this;
Googler4b7aae42017-05-04 17:16:09 -0400101 }
102
Kurt Alfred Kluever4192ca62022-07-05 06:30:29 -0700103 @CanIgnoreReturnValue
Googler4b7aae42017-05-04 17:16:09 -0400104 public TransitiveInfoProviderMapBuilder addAll(Iterable<TransitiveInfoProvider> providers) {
105 for (TransitiveInfoProvider provider : providers) {
106 add(provider);
107 }
108 return this;
109 }
110
111 @Nullable
112 public <P extends TransitiveInfoProvider> P getProvider(Class<P> providerClass) {
Googler3e1bf5c2019-11-04 12:41:49 -0800113 return providerClass.cast(providers.get(providerClass));
Googler4b7aae42017-05-04 17:16:09 -0400114 }
115
dslomovf6a7e5a2017-07-05 07:23:31 -0400116 @Nullable
adonovana11e2d02019-12-06 07:11:35 -0800117 public Info getProvider(Provider.Key key) {
118 return (Info) providers.get(key);
dslomovf6a7e5a2017-07-05 07:23:31 -0400119 }
120
Googler4b7aae42017-05-04 17:16:09 -0400121 public TransitiveInfoProviderMap build() {
janakr61aa6b72018-02-23 10:37:50 -0800122 return TransitiveInfoProviderMapImpl.create(providers);
Googler4b7aae42017-05-04 17:16:09 -0400123 }
124}