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 implements Comparable<Version> {
35      public static final Version INFINITE = new Version("32767.32767.32767");
36  
37      private short major;
38  
39      private short minor;
40  
41      private short micro;
42  
43      public Version(String version) {
44          if (version == null) {
45              throw new ModelloRuntimeException("Syntax error in the version field: Missing. ");
46          }
47  
48          String[] splittedVersion = StringUtils.split(version.trim(), ".");
49  
50          if (splittedVersion.length > 3) {
51              throw new ModelloRuntimeException(
52                      "Syntax error in the <version> field: The field must be at more 3 parts long (major, minor and micro). Was: '"
53                              + version + "'.");
54          }
55  
56          String majorString = splittedVersion[0];
57  
58          String minorString = "0";
59  
60          String microString = "0";
61  
62          if (splittedVersion.length > 1) {
63              minorString = splittedVersion[1];
64  
65              if (splittedVersion.length > 2) {
66                  microString = splittedVersion[2];
67              }
68          }
69  
70          try {
71              major = Short.parseShort(majorString);
72  
73              minor = Short.parseShort(minorString);
74  
75              micro = Short.parseShort(microString);
76          } catch (NumberFormatException e) {
77              throw new ModelloRuntimeException("Invalid version string: '" + version + "'.");
78          }
79      }
80  
81      public int getMajor() {
82          return major;
83      }
84  
85      public int getMinor() {
86          return minor;
87      }
88  
89      public int getMicro() {
90          return micro;
91      }
92  
93      /**
94       * Returns true if <code>this</code> is greater that <code>other</code>.
95       *
96       * @param other the other {@link Version}
97       * @return {@code true} if this instance is greater than other instance, otherwise {@code false}
98       */
99      public boolean greaterThan(Version other) {
100         if (this.major != other.major) {
101             return major > other.major;
102         }
103 
104         if (this.minor != other.minor) {
105             return this.minor > other.minor;
106         }
107 
108         if (this.micro != other.micro) {
109             return this.micro > other.micro;
110         }
111 
112         return false;
113     }
114 
115     /**
116      * Returns true if <code>this</code> is greater or equals than <code>other</code>.
117      *
118      * @param other the other {@link Version}
119      * @return {@code true} if this instance is greater or equals than other instance, otherwise {@code false}
120      */
121     public boolean greaterOrEqualsThan(Version other) {
122         if (this.major != other.major) {
123             return major >= other.major;
124         }
125 
126         if (this.minor != other.minor) {
127             return this.minor >= other.minor;
128         }
129 
130         if (this.micro != other.micro) {
131             return this.micro >= other.micro;
132         }
133 
134         return false;
135     }
136 
137     /**
138      * Returns true if <code>this</code> is lesser than <code>other</code>.
139      *
140      * @param other the other {@link Version}
141      * @return {@code true} if this instance is lesser than other instance, otherwise {@code false}
142      */
143     public boolean lesserThan(Version other) {
144         if (this.major != other.major) {
145             return major < other.major;
146         }
147 
148         if (this.minor != other.minor) {
149             return this.minor < other.minor;
150         }
151 
152         if (this.micro != other.micro) {
153             return this.micro < other.micro;
154         }
155 
156         return false;
157     }
158 
159     /**
160      * Returns true if <code>this</code> is lesser or equals that <code>other</code>.
161      *
162      * @param other the other {@link Version}
163      * @return {@code true} if this instance is lesser or equals than other instance, otherwise {@code false}
164      */
165     public boolean lesserOrEqualsThan(Version other) {
166         if (this.major != other.major) {
167             return major <= other.major;
168         }
169 
170         if (this.minor != other.minor) {
171             return this.minor <= other.minor;
172         }
173 
174         if (this.micro != other.micro) {
175             return this.micro <= other.micro;
176         }
177 
178         return false;
179     }
180 
181     public boolean inside(VersionRange range) {
182         if (range.getFromVersion().equals(this)) {
183             return true;
184         } else if ((this.greaterThan(range.getFromVersion())) && (this.lesserThan(range.getToVersion()))) {
185             return true;
186         } else if (this.equals(range.getFromVersion()) || this.equals(range.getToVersion())) {
187             return true;
188         }
189 
190         return false;
191     }
192 
193     // ----------------------------------------------------------------------
194     // Object overrides
195     // ----------------------------------------------------------------------
196 
197     public boolean equals(Object object) {
198         if (!(object instanceof Version)) {
199             return false;
200         }
201 
202         Version other = (Version) object;
203 
204         return this.major == other.major && this.minor == other.minor && this.micro == other.micro;
205     }
206 
207     public int hashCode() {
208         return toString("", null).hashCode();
209     }
210 
211     public String toString() {
212         return toString("", ".");
213     }
214 
215     public String toString(String prefix, String separator) {
216         return prefix + major + separator + minor + separator + micro;
217     }
218 
219     public int compareTo(Version otherVersion) {
220         if (greaterThan(otherVersion)) {
221             return +1;
222         } else if (equals(otherVersion)) {
223             return 0;
224         } else {
225             return -1;
226         }
227     }
228 }