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