1
2
3
4
5 package org.apache.maven.api.model;
6
7 import java.io.Serializable;
8 import java.util.ArrayList;
9 import java.util.Collection;
10 import java.util.Collections;
11 import java.util.HashMap;
12 import java.util.List;
13 import java.util.Map;
14 import org.apache.maven.api.annotations.Experimental;
15 import org.apache.maven.api.annotations.Generated;
16 import org.apache.maven.api.annotations.Immutable;
17 import org.apache.maven.api.annotations.Nonnull;
18 import org.apache.maven.api.annotations.NotThreadSafe;
19 import org.apache.maven.api.annotations.ThreadSafe;
20
21
22
23
24 @Experimental
25 @Generated @ThreadSafe @Immutable
26 public class PluginContainer
27 implements Serializable, InputLocationTracker
28 {
29
30
31
32 final List<Plugin> plugins;
33
34 final Map<Object, InputLocation> locations;
35
36
37
38
39
40 PluginContainer(
41 Collection<Plugin> plugins,
42 Map<Object, InputLocation> locations
43 ) {
44 this.plugins = ImmutableCollections.copy(plugins);
45 this.locations = ImmutableCollections.copy(locations);
46 }
47
48
49
50
51
52
53 @Nonnull
54 public List<Plugin> getPlugins() {
55 return this.plugins;
56 }
57
58
59
60
61 public InputLocation getLocation(Object key) {
62 return locations != null ? locations.get(key) : null;
63 }
64
65
66
67
68
69
70 @Nonnull
71 public Builder with() {
72 return newBuilder(this);
73 }
74
75
76
77
78
79
80 @Nonnull
81 public PluginContainer withPlugins(Collection<Plugin> plugins) {
82 return newBuilder(this, true).plugins(plugins).build();
83 }
84
85
86
87
88
89
90
91
92 @Nonnull
93 public static PluginContainer newInstance() {
94 return newInstance(true);
95 }
96
97
98
99
100
101
102
103
104 @Nonnull
105 public static PluginContainer newInstance(boolean withDefaults) {
106 return newBuilder(withDefaults).build();
107 }
108
109
110
111
112
113
114
115
116 @Nonnull
117 public static Builder newBuilder() {
118 return newBuilder(true);
119 }
120
121
122
123
124
125
126
127 @Nonnull
128 public static Builder newBuilder(boolean withDefaults) {
129 return new Builder(withDefaults);
130 }
131
132
133
134
135
136
137
138
139 @Nonnull
140 public static Builder newBuilder(PluginContainer from) {
141 return newBuilder(from, false);
142 }
143
144
145
146
147
148
149
150
151 @Nonnull
152 public static Builder newBuilder(PluginContainer from, boolean forceCopy) {
153 return new Builder(from, forceCopy);
154 }
155
156
157
158
159
160
161 @NotThreadSafe
162 public static class Builder
163 {
164 PluginContainer base;
165 Collection<Plugin> plugins;
166 Map<Object, InputLocation> locations;
167
168 Builder(boolean withDefaults) {
169 if (withDefaults) {
170 }
171 }
172
173 Builder(PluginContainer base, boolean forceCopy) {
174 if (forceCopy) {
175 this.plugins = base.plugins;
176 this.locations = base.locations;
177 } else {
178 this.base = base;
179 }
180 }
181
182 @Nonnull
183 public Builder plugins(Collection<Plugin> plugins) {
184 this.plugins = plugins;
185 return this;
186 }
187
188
189 @Nonnull
190 public Builder location(Object key, InputLocation location) {
191 if (location != null) {
192 if (!(this.locations instanceof HashMap)) {
193 this.locations = this.locations != null ? new HashMap<>(this.locations) : new HashMap<>();
194 }
195 this.locations.put(key, location);
196 }
197 return this;
198 }
199
200 @Nonnull
201 public PluginContainer build() {
202 if (base != null
203 && (plugins == null || plugins == base.plugins)
204 ) {
205 return base;
206 }
207 Map<Object, InputLocation> newlocs = this.locations != null ? this.locations : Collections.emptyMap();
208 Map<Object, InputLocation> oldlocs = this.base != null && this.base.locations != null ? this.base.locations : Collections.emptyMap();
209 Map<Object, InputLocation> locations = new HashMap<>();
210 locations.put("", newlocs.containsKey("") ? newlocs.get("") : oldlocs.get(""));
211 locations.put("plugins", newlocs.containsKey("plugins") ? newlocs.get("plugins") : oldlocs.get("plugins"));
212 return new PluginContainer(
213 plugins != null ? plugins : (base != null ? base.plugins : null),
214 locations
215 );
216 }
217 }
218
219
220
221 volatile Map<String, Plugin> pluginMap;
222
223
224
225
226
227 public Map<String, Plugin> getPluginsAsMap() {
228 if (pluginMap == null) {
229 synchronized (this) {
230 if (pluginMap == null) {
231 pluginMap = ImmutableCollections.copy(plugins.stream().collect(
232 java.util.stream.Collectors.toMap(
233 Plugin::getKey, java.util.function.Function.identity())));
234 }
235 }
236 }
237 return pluginMap;
238 }
239
240
241
242
243 @Override
244 public String toString()
245 {
246 return "PluginContainer {}";
247 }
248
249
250 }