View Javadoc
1   // =================== DO NOT EDIT THIS FILE ====================
2   //  Generated by Modello Velocity from model-v3.vm
3   //  template, any modifications will be overwritten.
4   // ==============================================================
5   package org.apache.maven.artifact.repository.metadata;
6   
7   import java.io.Serializable;
8   import java.util.AbstractList;
9   import java.util.ArrayList;
10  import java.util.Collections;
11  import java.util.HashMap;
12  import java.util.List;
13  import java.util.Map;
14  import java.util.Objects;
15  import java.util.Set;
16  import java.util.stream.Collectors;
17  import java.util.stream.Stream;
18  import org.apache.maven.api.annotations.Generated;
19  import org.apache.maven.api.annotations.Nonnull;
20  
21  @Generated
22  public class Metadata
23      extends BaseObject
24  {
25  
26      public Metadata() {
27          this(org.apache.maven.api.metadata.Metadata.newInstance());
28      }
29  
30      public Metadata(org.apache.maven.api.metadata.Metadata delegate) {
31          this(delegate, null);
32      }
33  
34      public Metadata(org.apache.maven.api.metadata.Metadata delegate, BaseObject parent) {
35          super(delegate, parent);
36      }
37  
38      public Metadata clone(){
39          return new Metadata(getDelegate());
40      }
41  
42      public org.apache.maven.api.metadata.Metadata getDelegate() {
43          return (org.apache.maven.api.metadata.Metadata) super.getDelegate();
44      }
45  
46      @Override
47      public boolean equals(Object o) {
48          if (this == o) {
49              return true;
50          }
51          if (o == null || !(o instanceof Metadata)) {
52              return false;
53          }
54          Metadata that = (Metadata) o;
55          return Objects.equals(this.delegate, that.delegate);
56      }
57  
58      @Override
59      public int hashCode() {
60          return getDelegate().hashCode();
61      }
62  
63      public String getModelEncoding() {
64          return getDelegate().getModelEncoding();
65      }
66  
67      public String getModelVersion() {
68          return getDelegate().getModelVersion();
69      }
70  
71      public void setModelVersion(String modelVersion) {
72          if (!Objects.equals(modelVersion, getModelVersion())) {
73              update(getDelegate().withModelVersion(modelVersion));
74          }
75      }
76  
77      public String getGroupId() {
78          return getDelegate().getGroupId();
79      }
80  
81      public void setGroupId(String groupId) {
82          if (!Objects.equals(groupId, getGroupId())) {
83              update(getDelegate().withGroupId(groupId));
84          }
85      }
86  
87      public String getArtifactId() {
88          return getDelegate().getArtifactId();
89      }
90  
91      public void setArtifactId(String artifactId) {
92          if (!Objects.equals(artifactId, getArtifactId())) {
93              update(getDelegate().withArtifactId(artifactId));
94          }
95      }
96  
97      public Versioning getVersioning() {
98          return getDelegate().getVersioning() != null ? new Versioning(getDelegate().getVersioning(), this) : null;
99      }
100 
101     public void setVersioning(Versioning versioning) {
102         if (!Objects.equals(versioning, getVersioning())){
103             if (versioning != null) {
104                 update(getDelegate().withVersioning(versioning.getDelegate()));
105                 versioning.childrenTracking = this::replace;
106             } else {
107                 update(getDelegate().withVersioning(null));
108             }
109         }
110     }
111 
112     public String getVersion() {
113         return getDelegate().getVersion();
114     }
115 
116     public void setVersion(String version) {
117         if (!Objects.equals(version, getVersion())) {
118             update(getDelegate().withVersion(version));
119         }
120     }
121 
122     @Nonnull
123     public List<Plugin> getPlugins() {
124         return new WrapperList<Plugin, org.apache.maven.api.metadata.Plugin>(
125                     () -> getDelegate().getPlugins(), l -> update(getDelegate().withPlugins(l)),
126                     d -> new Plugin(d, this), Plugin::getDelegate);
127     }
128 
129     public void setPlugins(List<Plugin> plugins) {
130         if (plugins == null) {
131             plugins = Collections.emptyList();
132         }
133         if (!Objects.equals(plugins, getPlugins())) {
134             update(getDelegate().withPlugins(
135                 plugins.stream().map(c -> c.getDelegate()).collect(Collectors.toList())));
136             plugins.forEach(e -> e.childrenTracking = this::replace);
137         }
138     }
139 
140     public void addPlugin(Plugin plugin) {
141         update(getDelegate().withPlugins(
142                Stream.concat(getDelegate().getPlugins().stream(), Stream.of(plugin.getDelegate()))
143                         .collect(Collectors.toList())));
144         plugin.childrenTracking = this::replace;
145     }
146 
147     public void removePlugin(Plugin plugin) {
148         update(getDelegate().withPlugins(
149                getDelegate().getPlugins().stream()
150                         .filter(e -> !Objects.equals(e, plugin))
151                         .collect(Collectors.toList())));
152         plugin.childrenTracking = null;
153     }
154 
155     protected boolean replace(Object oldDelegate, Object newDelegate) {
156         if (super.replace(oldDelegate, newDelegate)) {
157             return true;
158         }
159         if (oldDelegate == getDelegate().getVersioning()) {
160             update(getDelegate().withVersioning((org.apache.maven.api.metadata.Versioning) newDelegate));
161             return true;
162         }
163         if (getDelegate().getPlugins().contains(oldDelegate)) {
164             List<org.apache.maven.api.metadata.Plugin> list = new ArrayList<>(getDelegate().getPlugins());
165             list.replaceAll(d -> d == oldDelegate ? (org.apache.maven.api.metadata.Plugin) newDelegate : d);
166             update(getDelegate().withPlugins(list));
167             return true;
168         }
169         return false;
170     }
171 
172     public static List<org.apache.maven.api.metadata.Metadata> metadataToApiV4(List<Metadata> list) {
173         return list != null ? new WrapperList<>(list, Metadata::getDelegate, Metadata::new) : null;
174     }
175 
176     public static List<Metadata> metadataToApiV3(List<org.apache.maven.api.metadata.Metadata> list) {
177         return list != null ? new WrapperList<>(list, Metadata::new, Metadata::getDelegate) : null;
178     }
179 
180 
181     private String getSnapshotVersionKey( SnapshotVersion sv )
182     {
183         return sv.getClassifier() + ":" + sv.getExtension();
184     }
185 
186     public boolean merge( Metadata sourceMetadata )
187     {
188         boolean changed = false;
189 
190         for ( Plugin plugin : sourceMetadata.getPlugins() )
191         {
192             boolean found = false;
193 
194             for ( Plugin preExisting : getPlugins() )
195             {
196                 if ( java.util.Objects.equals( preExisting.getPrefix(), plugin.getPrefix() ) )
197                 {
198                     found = true;
199                     break;
200                 }
201             }
202 
203             if ( !found )
204             {
205                 Plugin mappedPlugin = new Plugin();
206 
207                 mappedPlugin.setArtifactId( plugin.getArtifactId() );
208 
209                 mappedPlugin.setPrefix( plugin.getPrefix() );
210 
211                 mappedPlugin.setName( plugin.getName() );
212 
213                 addPlugin( mappedPlugin );
214 
215                 changed = true;
216             }
217         }
218 
219         Versioning versioning = sourceMetadata.getVersioning();
220         if ( versioning != null )
221         {
222             Versioning v = getVersioning();
223             if ( v == null )
224             {
225                 v = new Versioning();
226                 setVersioning( v );
227                 changed = true;
228             }
229 
230             for ( String version : versioning.getVersions() )
231             {
232                 if ( !v.getVersions().contains( version ) )
233                 {
234                     changed = true;
235                     v.getVersions().add( version );
236                 }
237             }
238 
239             if ( "null".equals( versioning.getLastUpdated() ) )
240             {
241                 versioning.setLastUpdated( null );
242             }
243 
244             if ( "null".equals( v.getLastUpdated() ) )
245             {
246                 v.setLastUpdated( null );
247             }
248 
249             if ( versioning.getLastUpdated() == null || versioning.getLastUpdated().length() == 0 )
250             {
251                 // this should only be for historical reasons - we assume local is newer
252                 versioning.setLastUpdated( v.getLastUpdated() );
253             }
254 
255             if ( v.getLastUpdated() == null || v.getLastUpdated().length() == 0
256                  || versioning.getLastUpdated().compareTo( v.getLastUpdated() ) >= 0 )
257             {
258                 changed = true;
259                 v.setLastUpdated( versioning.getLastUpdated() );
260 
261                 if ( versioning.getRelease() != null )
262                 {
263                     changed = true;
264                     v.setRelease( versioning.getRelease() );
265                 }
266                 if ( versioning.getLatest() != null )
267                 {
268                     changed = true;
269                     v.setLatest( versioning.getLatest() );
270                 }
271 
272                 Snapshot s = v.getSnapshot();
273                 Snapshot snapshot = versioning.getSnapshot();
274                 if ( snapshot != null )
275                 {
276                     boolean updateSnapshotVersions = false;
277                     if ( s == null )
278                     {
279                         s = new Snapshot();
280                         v.setSnapshot( s );
281                         changed = true;
282                         updateSnapshotVersions = true;
283                     }
284 
285                     // overwrite
286                     if ( s.getTimestamp() == null ? snapshot.getTimestamp() != null
287                         : !s.getTimestamp().equals( snapshot.getTimestamp() ) )
288                     {
289                         s.setTimestamp( snapshot.getTimestamp() );
290                         changed = true;
291                         updateSnapshotVersions = true;
292                     }
293                     if ( s.getBuildNumber() != snapshot.getBuildNumber() )
294                     {
295                         s.setBuildNumber( snapshot.getBuildNumber() );
296                         changed = true;
297                     }
298                     if ( s.isLocalCopy() != snapshot.isLocalCopy() )
299                     {
300                         s.setLocalCopy( snapshot.isLocalCopy() );
301                         changed = true;
302                     }
303                     if ( updateSnapshotVersions )
304                     {
305                         java.util.Map<String, SnapshotVersion> versions = new java.util.LinkedHashMap<>();
306                         // never convert from legacy to new format if either source or target is legacy format
307                         if ( !v.getSnapshotVersions().isEmpty() )
308                         {
309                             for ( SnapshotVersion sv : versioning.getSnapshotVersions() )
310                             {
311                                 String key = getSnapshotVersionKey( sv );
312                                 versions.put( key, sv );
313                             }
314                             // never convert from legacy format
315                             if ( !versions.isEmpty() )
316                             {
317                                 for ( SnapshotVersion sv : v.getSnapshotVersions() )
318                                 {
319                                     String key = getSnapshotVersionKey( sv );
320                                     if ( !versions.containsKey( key ) )
321                                     {
322                                         versions.put( key, sv );
323                                     }
324                                 }
325                             }
326                             v.setSnapshotVersions( new java.util.ArrayList<SnapshotVersion>( versions.values() ) );
327                         }
328 
329                         changed = true;
330                     }
331                 }
332             }
333         }
334         return changed;
335     }
336           
337 }