View Javadoc

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   */
29  public class DefaultArtifactVersion
30      implements ArtifactVersion
31  {
32      private Integer majorVersion;
33  
34      private Integer minorVersion;
35  
36      private Integer incrementalVersion;
37  
38      private Integer buildNumber;
39  
40      private String qualifier;
41  
42      private ComparableVersion comparable;
43  
44      public DefaultArtifactVersion( String version )
45      {
46          parseVersion( version );
47      }
48  
49      @Override
50      public int hashCode()
51      {
52          return 11 + comparable.hashCode();
53      }
54  
55      @Override
56      public boolean equals( Object other )
57      {
58          if ( this == other )
59          {
60              return true;
61          }
62  
63          if ( !( other instanceof ArtifactVersion ) )
64          {
65              return false;
66          }
67  
68          return compareTo( (ArtifactVersion) other ) == 0;
69      }
70  
71      public int compareTo( ArtifactVersion otherVersion )
72      {
73          if ( otherVersion instanceof DefaultArtifactVersion )
74          {
75              return this.comparable.compareTo( ( (DefaultArtifactVersion) otherVersion).comparable );
76          }
77          else
78          {
79              return compareTo( new DefaultArtifactVersion( otherVersion.toString() ) );
80          }
81      }
82  
83      public int getMajorVersion()
84      {
85          return majorVersion != null ? majorVersion : 0;
86      }
87  
88      public int getMinorVersion()
89      {
90          return minorVersion != null ? minorVersion : 0;
91      }
92  
93      public int getIncrementalVersion()
94      {
95          return incrementalVersion != null ? incrementalVersion : 0;
96      }
97  
98      public int getBuildNumber()
99      {
100         return buildNumber != null ? buildNumber : 0;
101     }
102 
103     public String getQualifier()
104     {
105         return qualifier;
106     }
107 
108     public final void parseVersion( String version )
109     {
110         comparable = new ComparableVersion( version );
111 
112         int index = version.indexOf( "-" );
113 
114         String part1;
115         String part2 = null;
116 
117         if ( index < 0 )
118         {
119             part1 = version;
120         }
121         else
122         {
123             part1 = version.substring( 0, index );
124             part2 = version.substring( index + 1 );
125         }
126 
127         if ( part2 != null )
128         {
129             try
130             {
131                 if ( ( part2.length() == 1 ) || !part2.startsWith( "0" ) )
132                 {
133                     buildNumber = Integer.valueOf( part2 );
134                 }
135                 else
136                 {
137                     qualifier = part2;
138                 }
139             }
140             catch ( NumberFormatException e )
141             {
142                 qualifier = part2;
143             }
144         }
145 
146         if ( ( part1.indexOf( "." ) < 0 ) && !part1.startsWith( "0" ) )
147         {
148             try
149             {
150                 majorVersion = Integer.valueOf( part1 );
151             }
152             catch ( NumberFormatException e )
153             {
154                 // qualifier is the whole version, including "-"
155                 qualifier = version;
156                 buildNumber = null;
157             }
158         }
159         else
160         {
161             boolean fallback = false;
162 
163             StringTokenizer tok = new StringTokenizer( part1, "." );
164             try
165             {
166                 majorVersion = getNextIntegerToken( tok );
167                 if ( tok.hasMoreTokens() )
168                 {
169                     minorVersion = getNextIntegerToken( tok );
170                 }
171                 if ( tok.hasMoreTokens() )
172                 {
173                     incrementalVersion = getNextIntegerToken( tok );
174                 }
175                 if ( tok.hasMoreTokens() )
176                 {
177                     fallback = true;
178                 }
179 
180                 // string tokenzier won't detect these and ignores them
181                 if ( part1.indexOf( ".." ) >= 0 || part1.startsWith( "." ) || part1.endsWith( "." ) )
182                 {
183                     fallback = true;
184                 }
185             }
186             catch ( NumberFormatException e )
187             {
188                 fallback = true;
189             }
190 
191             if ( fallback )
192             {
193                 // qualifier is the whole version, including "-"
194                 qualifier = version;
195                 majorVersion = null;
196                 minorVersion = null;
197                 incrementalVersion = null;
198                 buildNumber = null;
199             }
200         }
201     }
202 
203     private static Integer getNextIntegerToken( StringTokenizer tok )
204     {
205         String s = tok.nextToken();
206         if ( ( s.length() > 1 ) && s.startsWith( "0" ) )
207         {
208             throw new NumberFormatException( "Number part has a leading 0: '" + s + "'" );
209         }
210         return Integer.valueOf( s );
211     }
212 
213     @Override
214     public String toString()
215     {
216         StringBuilder buf = new StringBuilder();
217         if ( majorVersion != null )
218         {
219             buf.append( majorVersion );
220         }
221         if ( minorVersion != null )
222         {
223             buf.append( "." );
224             buf.append( minorVersion );
225         }
226         if ( incrementalVersion != null )
227         {
228             buf.append( "." );
229             buf.append( incrementalVersion );
230         }
231         if ( buildNumber != null )
232         {
233             buf.append( "-" );
234             buf.append( buildNumber );
235         }
236         else if ( qualifier != null )
237         {
238             if ( buf.length() > 0 )
239             {
240                 buf.append( "-" );
241             }
242             buf.append( qualifier );
243         }
244         return buf.toString();
245     }
246 }