Coverage Report - org.apache.maven.artifact.ArtifactUtils
 
Classes in this File Line Coverage Branch Coverage Complexity
ArtifactUtils
38 %
18/48
31 %
8/26
2,091
 
 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  
 
 24  
 import java.util.ArrayList;
 25  
 import java.util.Collection;
 26  
 import java.util.Iterator;
 27  
 import java.util.LinkedHashMap;
 28  
 import java.util.List;
 29  
 import java.util.Map;
 30  
 import java.util.regex.Matcher;
 31  
 
 32  
 public final class ArtifactUtils
 33  
 {
 34  
 
 35  
     private ArtifactUtils()
 36  0
     {
 37  0
     }
 38  
 
 39  
     public static boolean isSnapshot( String version )
 40  
     {
 41  0
         return version != null &&
 42  
             ( version.toUpperCase().endsWith( Artifact.SNAPSHOT_VERSION ) || Artifact.VERSION_FILE_PATTERN.matcher( version )
 43  
                 .matches() );
 44  
     }
 45  
 
 46  
     public static String toSnapshotVersion( String version )
 47  
     {
 48  0
         Matcher m = Artifact.VERSION_FILE_PATTERN.matcher( version );
 49  0
         if ( m.matches() )
 50  
         {
 51  0
             return m.group( 1 ) + '-' + Artifact.SNAPSHOT_VERSION;
 52  
         }
 53  
         else
 54  
         {
 55  0
             return version;
 56  
         }
 57  
     }
 58  
 
 59  
     public static String versionlessKey( Artifact artifact )
 60  
     {
 61  5
         return versionlessKey( artifact.getGroupId(), artifact.getArtifactId() );
 62  
     }
 63  
 
 64  
     public static String versionlessKey( String groupId, String artifactId )
 65  
     {
 66  5
         if ( groupId == null )
 67  
         {
 68  0
             throw new NullPointerException( "groupId was null" );
 69  
         }
 70  5
         if ( artifactId == null )
 71  
         {
 72  0
             throw new NullPointerException( "artifactId was null" );
 73  
         }
 74  5
         return groupId + ":" + artifactId;
 75  
     }
 76  
 
 77  
     public static String artifactId( String groupId, String artifactId, String type, String version )
 78  
     {
 79  0
         return artifactId( groupId, artifactId, type, null, version );
 80  
     }
 81  
 
 82  
     public static String artifactId( String groupId, String artifactId, String type, String classifier,
 83  
                                      String baseVersion )
 84  
     {
 85  0
         return groupId + ":" + artifactId + ":" + type + ( classifier != null ? ":" + classifier : "" ) + ":" +
 86  
             baseVersion;
 87  
     }
 88  
 
 89  
     public static Map artifactMapByVersionlessId( Collection artifacts )
 90  
     {
 91  1
         Map artifactMap = new LinkedHashMap();
 92  
 
 93  1
         if ( artifacts != null )
 94  
         {
 95  1
             for ( Iterator it = artifacts.iterator(); it.hasNext(); )
 96  
             {
 97  5
                 Artifact artifact = (Artifact) it.next();
 98  
 
 99  5
                 artifactMap.put( versionlessKey( artifact ), artifact );
 100  5
             }
 101  
         }
 102  
 
 103  1
         return artifactMap;
 104  
     }
 105  
 
 106  
     public static Map artifactMapByArtifactId( Collection artifacts )
 107  
     {
 108  1
         Map artifactMap = new LinkedHashMap();
 109  
 
 110  1
         if ( artifacts != null )
 111  
         {
 112  1
             for ( Iterator it = artifacts.iterator(); it.hasNext(); )
 113  
             {
 114  5
                 Artifact artifact = (Artifact) it.next();
 115  
 
 116  5
                 artifactMap.put( artifact.getId(), artifact );
 117  5
             }
 118  
         }
 119  
 
 120  1
         return artifactMap;
 121  
     }
 122  
 
 123  
     public static Artifact copyArtifact( Artifact artifact )
 124  
     {
 125  0
         VersionRange range = artifact.getVersionRange();
 126  0
         DefaultArtifact clone = new DefaultArtifact( artifact.getGroupId(), artifact.getArtifactId(), range.cloneOf(),
 127  
                                                      artifact.getScope(), artifact.getType(), artifact.getClassifier(),
 128  
                                                      artifact.getArtifactHandler(), artifact.isOptional() );
 129  0
         clone.setRelease( artifact.isRelease() );
 130  0
         clone.setResolvedVersion( artifact.getVersion() );
 131  0
         clone.setResolved( artifact.isResolved() );
 132  0
         clone.setFile( artifact.getFile() );
 133  
 
 134  0
         clone.setAvailableVersions( copyList( artifact.getAvailableVersions() ) );
 135  0
         clone.setBaseVersion( artifact.getBaseVersion() );
 136  0
         clone.setDependencyFilter( artifact.getDependencyFilter() );
 137  0
         clone.setDependencyTrail( copyList( artifact.getDependencyTrail() ) );
 138  0
         clone.setDownloadUrl( artifact.getDownloadUrl() );
 139  0
         clone.setRepository( artifact.getRepository() );
 140  
 
 141  0
         return clone;
 142  
     }
 143  
     
 144  
     private static List copyList( List original )
 145  
     {
 146  0
         List copy = null;
 147  
         
 148  0
         if ( original != null )
 149  
         {
 150  0
             copy = new ArrayList();
 151  
             
 152  0
             if ( !original.isEmpty() )
 153  
             {
 154  0
                 copy.addAll( original );
 155  
             }
 156  
         }
 157  
         
 158  0
         return copy;
 159  
     }
 160  
 
 161  
 }