View Javadoc

1   package org.apache.maven.artifact;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *  http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import org.apache.maven.artifact.versioning.VersionRange;
23  import org.apache.maven.artifact.handler.ArtifactHandler;
24  
25  import java.util.ArrayList;
26  import java.util.Collection;
27  import java.util.Iterator;
28  import java.util.LinkedHashMap;
29  import java.util.List;
30  import java.util.Map;
31  
32  public final class ArtifactUtils
33  {
34  
35      private ArtifactUtils()
36      {
37      }
38  
39      public static boolean isSnapshot( String version )
40      {
41          return version != null &&
42              ( version.toUpperCase().endsWith( Artifact.SNAPSHOT_VERSION ) || Artifact.VERSION_FILE_PATTERN.matcher( version )
43                  .matches() );
44      }
45  
46      public static String versionlessKey( Artifact artifact )
47      {
48          return versionlessKey( artifact.getGroupId(), artifact.getArtifactId() );
49      }
50  
51      public static String versionlessKey( String groupId, String artifactId )
52      {
53          if ( groupId == null )
54          {
55              throw new NullPointerException( "groupId was null" );
56          }
57          if ( artifactId == null )
58          {
59              throw new NullPointerException( "artifactId was null" );
60          }
61          return groupId + ":" + artifactId;
62      }
63  
64      public static String artifactId( String groupId, String artifactId, String type, String version )
65      {
66          return artifactId( groupId, artifactId, type, null, version );
67      }
68  
69      public static String artifactId( String groupId, String artifactId, String type, String classifier,
70                                       String baseVersion )
71      {
72          return groupId + ":" + artifactId + ":" + type + ( classifier != null ? ":" + classifier : "" ) + ":" +
73              baseVersion;
74      }
75  
76      public static Map artifactMapByVersionlessId( Collection artifacts )
77      {
78          Map artifactMap = new LinkedHashMap();
79  
80          if ( artifacts != null )
81          {
82              for ( Iterator it = artifacts.iterator(); it.hasNext(); )
83              {
84                  Artifact artifact = (Artifact) it.next();
85  
86                  artifactMap.put( versionlessKey( artifact ), artifact );
87              }
88          }
89  
90          return artifactMap;
91      }
92  
93      public static Map artifactMapByArtifactId( Collection artifacts )
94      {
95          Map artifactMap = new LinkedHashMap();
96  
97          if ( artifacts != null )
98          {
99              for ( Iterator it = artifacts.iterator(); it.hasNext(); )
100             {
101                 Artifact artifact = (Artifact) it.next();
102 
103                 artifactMap.put( artifact.getId(), artifact );
104             }
105         }
106 
107         return artifactMap;
108     }
109 
110     public static Artifact copyArtifact( Artifact artifact )
111     {
112         VersionRange range = artifact.getVersionRange();
113         DefaultArtifact clone = new DefaultArtifact( artifact.getGroupId(), artifact.getArtifactId(), range.cloneOf(),
114                                                      artifact.getScope(), artifact.getType(), artifact.getClassifier(),
115                                                      artifact.getArtifactHandler(), artifact.isOptional() );
116         clone.setRelease( artifact.isRelease() );
117         clone.setResolvedVersion( artifact.getVersion() );
118         clone.setResolved( artifact.isResolved() );
119         clone.setFile( artifact.getFile() );
120 
121         clone.setAvailableVersions( copyList( artifact.getAvailableVersions() ) );
122         clone.setBaseVersion( artifact.getBaseVersion() );
123         clone.setDependencyFilter( artifact.getDependencyFilter() );
124         clone.setDependencyTrail( copyList( artifact.getDependencyTrail() ) );
125         clone.setDownloadUrl( artifact.getDownloadUrl() );
126         clone.setRepository( artifact.getRepository() );
127 
128         return clone;
129     }
130     
131     private static List copyList( List original )
132     {
133         List copy = null;
134         
135         if ( original != null )
136         {
137             copy = new ArrayList();
138             
139             if ( !original.isEmpty() )
140             {
141                 copy.addAll( original );
142             }
143         }
144         
145         return copy;
146     }
147 
148 }