View Javadoc

1   package org.codehaus.modello.model;
2   
3   /*
4    * Copyright (c) 2004, Codehaus.org
5    *
6    * Permission is hereby granted, free of charge, to any person obtaining a copy of
7    * this software and associated documentation files (the "Software"), to deal in
8    * the Software without restriction, including without limitation the rights to
9    * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
10   * of the Software, and to permit persons to whom the Software is furnished to do
11   * so, subject to the following conditions:
12   *
13   * The above copyright notice and this permission notice shall be included in all
14   * copies or substantial portions of the Software.
15   *
16   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22   * SOFTWARE.
23   */
24  
25  import org.codehaus.modello.ModelloRuntimeException;
26  import org.codehaus.plexus.util.StringUtils;
27  
28  /**
29   * A version string is on the form <major>.<minor>.<micro>.
30   *
31   * @author <a href="mailto:trygvis@inamo.no">Trygve Laugst&oslash;l</a>
32   * @author <a href="mailto:evenisse@codehaus.org">Emmanuel Venisse</a>
33   */
34  public class Version
35      implements Comparable<Version>
36  {
37      public static final Version INFINITE = new Version( "32767.32767.32767" );
38  
39      private short major;
40  
41      private short minor;
42  
43      private short micro;
44  
45      public Version( String version )
46      {
47          if ( version == null )
48          {
49              throw new ModelloRuntimeException( "Syntax error in the version field: Missing. " );
50          }
51  
52          String[] splittedVersion = StringUtils.split( version.trim(), "." );
53  
54          if ( splittedVersion.length > 3 )
55          {
56              throw new ModelloRuntimeException(
57                  "Syntax error in the <version> field: The field must be at more 3 parts long (major, minor and micro). Was: '"
58                  + version + "'." );
59          }
60  
61          String majorString = splittedVersion[0];
62  
63          String minorString = "0";
64  
65          String microString = "0";
66  
67          if ( splittedVersion.length > 1 )
68          {
69              minorString = splittedVersion[1];
70  
71              if ( splittedVersion.length > 2 )
72              {
73                  microString = splittedVersion[2];
74  
75              }
76          }
77  
78          try
79          {
80              major = Short.parseShort( majorString );
81  
82              minor = Short.parseShort( minorString );
83  
84              micro = Short.parseShort( microString );
85          }
86          catch ( NumberFormatException e )
87          {
88              throw new ModelloRuntimeException( "Invalid version string: '" + version + "'." );
89          }
90      }
91  
92      public int getMajor()
93      {
94          return major;
95      }
96  
97      public int getMinor()
98      {
99          return minor;
100     }
101 
102     public int getMicro()
103     {
104         return micro;
105     }
106 
107     /**
108      * Returns true if <code>this</code> is greater that <code>other</code>.
109      *
110      * @param other the other {@link Version}
111      * @return {@code true} if this instance is greater than other instance, otherwise {@code false}
112      */
113     public boolean greaterThan( Version other )
114     {
115         if ( this.major != other.major )
116         {
117             return major > other.major;
118         }
119 
120         if ( this.minor != other.minor )
121         {
122             return this.minor > other.minor;
123         }
124 
125         if ( this.micro != other.micro )
126         {
127             return this.micro > other.micro;
128         }
129 
130         return false;
131     }
132 
133     /**
134      * Returns true if <code>this</code> is greater or equals than <code>other</code>.
135      *
136      * @param other the other {@link Version}
137      * @return {@code true} if this instance is greater or equals than other instance, otherwise {@code false}
138      */
139     public boolean greaterOrEqualsThan( Version other )
140     {
141         if ( this.major != other.major )
142         {
143             return major >= other.major;
144         }
145 
146         if ( this.minor != other.minor )
147         {
148             return this.minor >= other.minor;
149         }
150 
151         if ( this.micro != other.micro )
152         {
153             return this.micro >= other.micro;
154         }
155 
156         return false;
157     }
158 
159     /**
160      * Returns true if <code>this</code> is lesser than <code>other</code>.
161      *
162      * @param other the other {@link Version}
163      * @return {@code true} if this instance is lesser than other instance, otherwise {@code false}
164      */
165     public boolean lesserThan( Version other )
166     {
167         if ( this.major != other.major )
168         {
169             return major < other.major;
170         }
171 
172         if ( this.minor != other.minor )
173         {
174             return this.minor < other.minor;
175         }
176 
177         if ( this.micro != other.micro )
178         {
179             return this.micro < other.micro;
180         }
181 
182         return false;
183     }
184 
185     /**
186      * Returns true if <code>this</code> is lesser or equals that <code>other</code>.
187      *
188      * @param other the other {@link Version}
189      * @return {@code true} if this instance is lesser or equals than other instance, otherwise {@code false}
190      */
191     public boolean lesserOrEqualsThan( Version other )
192     {
193         if ( this.major != other.major )
194         {
195             return major <= other.major;
196         }
197 
198         if ( this.minor != other.minor )
199         {
200             return this.minor <= other.minor;
201         }
202 
203         if ( this.micro != other.micro )
204         {
205             return this.micro <= other.micro;
206         }
207 
208         return false;
209     }
210 
211     public boolean inside( VersionRange range )
212     {
213         if ( range.getFromVersion().equals( this ) )
214         {
215             return true;
216         }
217         else if ( ( this.greaterThan( range.getFromVersion() ) ) && ( this.lesserThan( range.getToVersion() ) ) )
218         {
219             return true;
220         }
221         else if ( this.equals( range.getFromVersion() ) || this.equals( range.getToVersion() ) )
222         {
223             return true;
224         }
225 
226         return false;
227     }
228 
229     // ----------------------------------------------------------------------
230     // Object overrides
231     // ----------------------------------------------------------------------
232 
233     public boolean equals( Object object )
234     {
235         if ( !( object instanceof Version ) )
236         {
237             return false;
238         }
239 
240         Version other = (Version) object;
241 
242         return this.major == other.major && this.minor == other.minor && this.micro == other.micro;
243     }
244 
245     public int hashCode()
246     {
247         return toString( "", null ).hashCode();
248     }
249 
250     public String toString()
251     {
252         return toString( "", "." );
253     }
254 
255     public String toString( String prefix, String separator )
256     {
257         return prefix + major + separator + minor + separator + micro;
258     }
259 
260     public int compareTo( Version otherVersion )
261     {
262         if ( greaterThan( otherVersion ) )
263         {
264             return +1;
265         }
266         else if ( equals( otherVersion ) )
267         {
268             return 0;
269         }
270         else
271         {
272             return -1;
273         }
274     }
275 }