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
111      */
112     public boolean greaterThan( Version other )
113     {
114         if ( this.major != other.major )
115         {
116             return major > other.major;
117         }
118 
119         if ( this.minor != other.minor )
120         {
121             return this.minor > other.minor;
122         }
123 
124         if ( this.micro != other.micro )
125         {
126             return this.micro > other.micro;
127         }
128 
129         return false;
130     }
131 
132     /**
133      * Returns true if <code>this</code> is greater or equals that <code>other</code>.
134      *
135      * @param other
136      */
137     public boolean greaterOrEqualsThan( Version other )
138     {
139         if ( this.major != other.major )
140         {
141             return major >= other.major;
142         }
143 
144         if ( this.minor != other.minor )
145         {
146             return this.minor >= other.minor;
147         }
148 
149         if ( this.micro != other.micro )
150         {
151             return this.micro >= other.micro;
152         }
153 
154         return false;
155     }
156 
157     /**
158      * Returns true if <code>this</code> is lesser that <code>other</code>.
159      *
160      * @param other
161      */
162     public boolean lesserThan( Version other )
163     {
164         if ( this.major != other.major )
165         {
166             return major < other.major;
167         }
168 
169         if ( this.minor != other.minor )
170         {
171             return this.minor < other.minor;
172         }
173 
174         if ( this.micro != other.micro )
175         {
176             return this.micro < other.micro;
177         }
178 
179         return false;
180     }
181 
182     /**
183      * Returns true if <code>this</code> is lesser or equals that <code>other</code>.
184      *
185      * @param other
186      */
187     public boolean lesserOrEqualsThan( Version other )
188     {
189         if ( this.major != other.major )
190         {
191             return major <= other.major;
192         }
193 
194         if ( this.minor != other.minor )
195         {
196             return this.minor <= other.minor;
197         }
198 
199         if ( this.micro != other.micro )
200         {
201             return this.micro <= other.micro;
202         }
203 
204         return false;
205     }
206 
207     public boolean inside( VersionRange range )
208     {
209         if ( range.getFromVersion().equals( this ) )
210         {
211             return true;
212         }
213         else if ( ( this.greaterThan( range.getFromVersion() ) ) && ( this.lesserThan( range.getToVersion() ) ) )
214         {
215             return true;
216         }
217         else if ( this.equals( range.getFromVersion() ) || this.equals( range.getToVersion() ) )
218         {
219             return true;
220         }
221 
222         return false;
223     }
224 
225     // ----------------------------------------------------------------------
226     // Object overrides
227     // ----------------------------------------------------------------------
228 
229     public boolean equals( Object object )
230     {
231         if ( !( object instanceof Version ) )
232         {
233             return false;
234         }
235 
236         Version other = (Version) object;
237 
238         return this.major == other.major && this.minor == other.minor && this.micro == other.micro;
239     }
240 
241     public int hashCode()
242     {
243         return toString( "", null ).hashCode();
244     }
245 
246     public String toString()
247     {
248         return toString( "", "." );
249     }
250 
251     public String toString( String prefix, String separator )
252     {
253         return prefix + major + separator + minor + separator + micro;
254     }
255 
256     public int compareTo( Version otherVersion )
257     {
258         if ( greaterThan( otherVersion ) )
259         {
260             return +1;
261         }
262         else if ( equals( otherVersion ) )
263         {
264             return 0;
265         }
266         else
267         {
268             return -1;
269         }
270     }
271 }