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