Coverage Report - org.apache.maven.artifact.versioning.DefaultArtifactVersion
 
Classes in this File Line Coverage Branch Coverage Complexity
DefaultArtifactVersion
99 %
87/88
97 %
64/66
3,25
 
 1  
 package org.apache.maven.artifact.versioning;
 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 java.util.StringTokenizer;
 23  
 
 24  
 /**
 25  
  * Default implementation of artifact versioning.
 26  
  *
 27  
  * @author <a href="mailto:brett@apache.org">Brett Porter</a>
 28  
  * @version $Id: DefaultArtifactVersion.java 674059 2008-07-04 14:04:24Z brett $
 29  
  */
 30  
 public class DefaultArtifactVersion
 31  
     implements ArtifactVersion
 32  
 {
 33  
     private Integer majorVersion;
 34  
 
 35  
     private Integer minorVersion;
 36  
 
 37  
     private Integer incrementalVersion;
 38  
 
 39  
     private Integer buildNumber;
 40  
 
 41  
     private String qualifier;
 42  
 
 43  
     private String unparsed;
 44  
 
 45  
     public DefaultArtifactVersion( String version )
 46  1416
     {
 47  1416
         parseVersion( version );
 48  1416
     }
 49  
 
 50  
     public int compareTo( Object o )
 51  
     {
 52  690
         ArtifactVersion otherVersion = (ArtifactVersion) o;
 53  
 
 54  690
         int result = getMajorVersion() - otherVersion.getMajorVersion();
 55  690
         if ( result == 0 )
 56  
         {
 57  504
             result = getMinorVersion() - otherVersion.getMinorVersion();
 58  
         }
 59  690
         if ( result == 0 )
 60  
         {
 61  217
             result = getIncrementalVersion() - otherVersion.getIncrementalVersion();
 62  
         }
 63  690
         if ( result == 0 )
 64  
         {
 65  204
             if ( qualifier != null )
 66  
             {
 67  57
                 String otherQualifier = otherVersion.getQualifier();
 68  
 
 69  57
                 if ( otherQualifier != null )
 70  
                 {
 71  46
                     if ( ( qualifier.length() > otherQualifier.length() )
 72  
                          && qualifier.startsWith( otherQualifier ) )
 73  
                     {
 74  
                         // here, the longer one that otherwise match is considered older
 75  5
                         result = -1;
 76  
                     }
 77  41
                     else if ( ( qualifier.length() < otherQualifier.length() )
 78  
                               && otherQualifier.startsWith( qualifier ) )
 79  
                     {
 80  
                         // here, the longer one that otherwise match is considered older
 81  5
                         result = 1;
 82  
                     }
 83  
                     else
 84  
                     {
 85  36
                         result = qualifier.compareTo( otherQualifier );
 86  
                     }
 87  
                 }
 88  
                 else
 89  
                 {
 90  
                     // otherVersion has no qualifier but we do - that's newer
 91  11
                     result = -1;
 92  
                 }
 93  57
             }
 94  147
             else if ( otherVersion.getQualifier() != null )
 95  
             {
 96  
                 // otherVersion has a qualifier but we don't, we're newer
 97  12
                 result = 1;
 98  
             }
 99  
             else
 100  
             {
 101  135
                 result = getBuildNumber() - otherVersion.getBuildNumber();
 102  
             }
 103  
         }
 104  690
         return result;
 105  
     }
 106  
 
 107  
     public int getMajorVersion()
 108  
     {
 109  1438
         return majorVersion != null ? majorVersion.intValue() : 0;
 110  
     }
 111  
 
 112  
     public int getMinorVersion()
 113  
     {
 114  1066
         return minorVersion != null ? minorVersion.intValue() : 0;
 115  
     }
 116  
 
 117  
     public int getIncrementalVersion()
 118  
     {
 119  492
         return incrementalVersion != null ? incrementalVersion.intValue() : 0;
 120  
     }
 121  
 
 122  
     public int getBuildNumber()
 123  
     {
 124  328
         return buildNumber != null ? buildNumber.intValue() : 0;
 125  
     }
 126  
 
 127  
     public String getQualifier()
 128  
     {
 129  264
         return qualifier;
 130  
     }
 131  
 
 132  
     public final void parseVersion( String version )
 133  
     {
 134  1416
         unparsed = version;
 135  
 
 136  1416
         int index = version.indexOf( "-" );
 137  
 
 138  
         String part1;
 139  1416
         String part2 = null;
 140  
 
 141  1416
         if ( index < 0 )
 142  
         {
 143  1255
             part1 = version;
 144  
         }
 145  
         else
 146  
         {
 147  161
             part1 = version.substring( 0, index );
 148  161
             part2 = version.substring( index + 1 );
 149  
         }
 150  
 
 151  1416
         if ( part2 != null )
 152  
         {
 153  
             try
 154  
             {
 155  161
                 if ( ( part2.length() == 1 ) || !part2.startsWith( "0" ) )
 156  
                 {
 157  160
                     buildNumber = Integer.valueOf( part2 );
 158  
                 }
 159  
                 else
 160  
                 {
 161  1
                     qualifier = part2;
 162  
                 }
 163  
             }
 164  131
             catch ( NumberFormatException e )
 165  
             {
 166  131
                 qualifier = part2;
 167  30
             }
 168  
         }
 169  
 
 170  1416
         if ( ( part1.indexOf( "." ) < 0 ) && !part1.startsWith( "0" ) )
 171  
         {
 172  
             try
 173  
             {
 174  40
                 majorVersion = Integer.valueOf( part1 );
 175  
             }
 176  2
             catch ( NumberFormatException e )
 177  
             {
 178  
                 // qualifier is the whole version, including "-"
 179  2
                 qualifier = version;
 180  2
                 buildNumber = null;
 181  40
             }
 182  
         }
 183  
         else
 184  
         {
 185  1376
             boolean fallback = false;
 186  
 
 187  1376
             StringTokenizer tok = new StringTokenizer( part1, "." );
 188  
             try
 189  
             {
 190  1376
                 majorVersion = getNextIntegerToken( tok );
 191  1375
                 if ( tok.hasMoreTokens() )
 192  
                 {
 193  1375
                     minorVersion = getNextIntegerToken( tok );
 194  
                 }
 195  1372
                 if ( tok.hasMoreTokens() )
 196  
                 {
 197  67
                     incrementalVersion = getNextIntegerToken( tok );
 198  
                 }
 199  1369
                 if ( tok.hasMoreTokens() )
 200  
                 {
 201  3
                     fallback = true;
 202  
                 }
 203  
 
 204  
                 // string tokenzier won't detect these and ignores them
 205  1369
                 if ( part1.indexOf( ".." ) >= 0 || part1.startsWith( "." ) || part1.endsWith( "." ) )
 206  
                 {
 207  4
                     fallback = true;
 208  
                 }
 209  
             }
 210  7
             catch ( NumberFormatException e )
 211  
             {
 212  7
                 fallback = true;
 213  1369
             }
 214  
 
 215  1376
             if ( fallback )
 216  
             {
 217  
                 // qualifier is the whole version, including "-"
 218  14
                 qualifier = version;
 219  14
                 majorVersion = null;
 220  14
                 minorVersion = null;
 221  14
                 incrementalVersion = null;
 222  14
                 buildNumber = null;
 223  
             }
 224  
         }
 225  1416
     }
 226  
 
 227  
     private static Integer getNextIntegerToken( StringTokenizer tok )
 228  
     {
 229  2818
         String s = tok.nextToken();
 230  2818
         if ( ( s.length() > 1 ) && s.startsWith( "0" ) )
 231  
         {
 232  6
             throw new NumberFormatException( "Number part has a leading 0: '" + s + "'" );
 233  
         }
 234  2812
         return Integer.valueOf( s );
 235  
     }
 236  
 
 237  
     public String toString()
 238  
     {
 239  1183
         return unparsed;
 240  
     }
 241  
 
 242  
     public boolean equals( Object other )
 243  
     {
 244  27
         if ( this == other )
 245  
         {
 246  2
             return true;
 247  
         }
 248  
 
 249  25
         if ( false == ( other instanceof ArtifactVersion ) )
 250  
         {
 251  0
             return false;
 252  
         }
 253  
 
 254  25
         return 0 == compareTo( other );
 255  
     }
 256  
 
 257  
     public int hashCode()
 258  
     {
 259  6
         int result = 1229;
 260  
 
 261  6
         result = 1223 * result + getMajorVersion();
 262  6
         result = 1223 * result + getMinorVersion();
 263  6
         result = 1223 * result + getIncrementalVersion();
 264  6
         result = 1223 * result + getBuildNumber();
 265  
 
 266  6
         if ( null != getQualifier() )
 267  
         {
 268  2
             result = 1223 * result + getQualifier().hashCode();
 269  
         }
 270  
 
 271  6
         return result;
 272  
     }
 273  
 }