Subversion Repositories DevTools

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
1615 mhunt 1
package com.erggroup.mass.ant;
2
 
3
import java.util.StringTokenizer;
4
 
5
/**
6
 * <p><code>PackageVersion</code> represents an identifying version of a 
7
 * package, using the MASS versioning scheme.
8
 * 
9
 * <p>The MASS package versioning scheme is as follows:
10
 * 
11
 * <blockquote>
12
 * <pre>
13
 *      xx.yy.zz.ppp
14
 *  
15
 *      where xx = major version 
16
 *            yy = minor version
17
 *            zz = patch version
18
 *           ppp = project abbreviation (default = mas)
19
 * </pre>
20
 * </blockquote>
21
 */
22
public class PackageVersion implements Comparable, Cloneable
23
{
24
	/**
25
	 * The major version of the package.
26
	 */
27
	private int major = 0;
28
 
29
	/**
30
	 * The minor version of the package.
31
	 */
32
	private int minor = 0;
33
 
34
	/**
35
	 * The patch version of the package.
36
	 */
37
	private int patch = 0;
38
 
39
	/**
40
	 * The scope (aka project abbreviation)
41
	 */
42
	private String scope = "";
43
 
44
	/**
45
	 * Constructs a PackageVersion with the default major / minor / patch versions,
46
	 * and an empty scope (project abbreviation).
47
	 */
48
	public PackageVersion()
49
	{
50
		major = 0;
51
		minor = 0;
52
		patch = 0;
53
		scope = "";
54
	}
55
 
56
	/**
57
	 * Constructs a PackageVersion with the specified version information.
58
	 * 
59
	 * @param major the major version of the PackageVersion
60
	 * @param minor the minor version of the PackageVersion
61
	 * @param patch the patch version of the PackageVersion
62
	 * @param scope the scope (project abbreviation) of the PackageVersion
63
	 */
64
	public PackageVersion( int major, int minor, int patch, String scope )
65
	{
66
		this.major = major;
67
		this.minor = minor;
68
		this.patch = patch;
69
		this.scope = scope;
70
	}
71
 
72
	/**
73
	 * Constructs a PackageVersion by parsing the provided string into 
74
	 * composite elements. 
75
	 * 
76
	 * @param version a non-null string containing a PackageVersion in the form
77
	 *        xx.yy.zz.ppp (see class documentation)
78
	 * 
79
	 * @throws NullPointerException if the version string provided is null.
80
	 * @throws NumberFormatException if the string is in an inappropriate form.
81
	 */
82
	public PackageVersion( String version ) throws NumberFormatException
83
	{
84
		if (version == null)
85
		{
86
			throw new NullPointerException();
87
		}
88
 
89
		StringTokenizer st = new StringTokenizer(version, ".");
90
		if (st.countTokens() == 4)
91
		{
92
			major = Integer.parseInt(st.nextToken());
93
			minor = Integer.parseInt(st.nextToken());
94
			patch = Integer.parseInt(st.nextToken());
95
			scope = st.nextToken();
96
		}
97
		else
98
		{
99
			throw new NumberFormatException("Invalid package version - '" + version + "'");
100
		}
101
	}
102
 
103
	/**
104
	 * Gets the value for the major version of this PackageVersion.
105
	 * 
106
	 * @return the major version of this PackageVersion.
107
	 */
108
	public int getMajor()
109
	{
110
		return major;
111
	}
112
 
113
	/**
114
	 * Gets the value for the minor version of this PackageVersion.
115
	 * 
116
	 * @return the minor version of this PackageVersion.
117
	 */
118
	public int getMinor()
119
	{
120
		return minor;
121
	}
122
 
123
	/**
124
	 * Gets the value for the patch version of this PackageVersion.
125
	 * 
126
	 * @return the patch version of this PackageVersion.
127
	 */
128
	public int getPatch()
129
	{
130
		return patch;
131
	}
132
 
133
	/**
134
	 * Gets the value for the scope (project abbreviation) of this 
135
	 * PackageVersion.
136
	 * 
137
	 * @return the scope of this PackageVersion.
138
	 */
139
	public String getScope()
140
	{
141
		return scope;
142
	}
143
 
144
	/**
145
	 * Sets the value of the major version of this PackageVersion.
146
	 * 
147
	 * @param newValue the new major version of this PackageVersion.
148
	 */
149
	public void setMajor( int newValue )
150
	{
151
		major = newValue;
152
	}
153
 
154
	/**
155
	 * Sets the value of the minor version of this PackageVersion.
156
	 * 
157
	 * @param newValue the new minor version of this PackageVersion.
158
	 */
159
	public void setMinor( int newValue )
160
	{
161
		minor = newValue;
162
	}
163
 
164
	/**
165
	 * Sets the value of the patch version of this PackageVersion.
166
	 * 
167
	 * @param newValue the new patch version of this PackageVersion.
168
	 */
169
	public void setPatch( int newValue )
170
	{
171
		patch = newValue;
172
	}
173
 
174
	/**
175
	 * Sets the value of the scope of this PackageVersion.
176
	 * 
177
	 * @param newValue the new scope of this PackageVersion.
178
	 */
179
	public void setScope( String newValue )
180
	{
181
		scope = newValue;
182
	}
183
 
184
	/**
185
	 * Obtain a string representation of this PackageVersion. This
186
	 * method provides the string in standard form.
187
	 * 
188
	 * @return the well-formed string representation of this PackageVersion.
189
	 */
190
	public String toString()
191
	{
192
		String stringValue = major + "." + minor + "." + patch;
193
		if ( scope != null && scope.length() > 0 )
194
		{
195
			stringValue = stringValue + "." + scope;
196
		}
197
		return stringValue;
198
	}
199
 
200
	/**
201
	 * Compares this PackageVersion to the specified object. The result is
202
	 * true if and only if the argument is not <code>null</code> and is a
203
	 * <code>PackageVersion</code> object that represents the same version
204
	 * as this object. 
205
	 * 
206
	 * @param obj the object to compare with.
207
	 * 
208
	 * @return <code>true</code> if the objects are the same, 
209
	 *         <code>false</code> otherwise.
210
	 */
211
	public boolean equals( Object obj )
212
	{
213
		if ( this == obj ) return true;
214
		if ( obj == null || getClass() != obj.getClass() ) return false;
215
 
216
		PackageVersion other = (PackageVersion)obj;
217
		if ( major != other.major ) return false;
218
		if ( minor != other.minor ) return false;
219
		if ( patch != other.patch ) return false;
220
		if ( scope == null )
221
		{
222
			if ( other.scope != null ) return false;
223
		}
224
		else if ( !scope.equals(other.scope)) return false;
225
		return true;
226
	}
227
 
228
	/**
229
	 * Returns a hash code representing this PackageVersion instance.
230
	 * 
231
	 * @return a hash code value for this object.
232
	 */
233
	public int hashCode()
234
	{
235
		int result = 0;
236
		result = 37*result + major;
237
		result = 37*result + minor;
238
		result = 37*result + patch;
239
		result = 37*result + (scope != null ? scope.hashCode() : 0);
240
		return result;
241
	}
242
 
243
	/**
244
	 * Compares this <code>PackageVersion</code> object to another object.
245
	 * If the object is a <code>PackageVersion</code>, this function 
246
	 * performs a numerical ranking based on major / minor / patch versions,
247
	 * followed by an alphanumerical ranking based on the scope.
248
	 * 
249
	 * @param obj the <code>Object</code> to be compared.
250
	 * 
251
	 * @throws <code>ClassCastException</code> if the argument is not a 
252
	 *         <code>PackageVersion</code>.
253
	 */
254
	public int compareTo( Object obj )
255
	{
256
		PackageVersion other = (PackageVersion)obj;
257
 
258
		if ( major > other.major ) return 1;
259
		if ( major < other.major ) return -1;
260
 
261
		if ( minor > other.minor ) return 1;
262
		if ( minor < other.minor ) return -1;
263
 
264
		if ( patch > other.patch ) return 1;
265
		if ( patch < other.patch ) return -1;
266
 
267
		if ( scope == null )
268
		{
269
			if ( other.scope != null )
270
				return -1;
271
			else
272
				return 0;
273
		}
274
 
275
		return scope.compareTo(other.scope);
276
	}
277
 
278
	/**
279
	 * Obtain a clone of this instance.
280
	 * 
281
	 * @return a clone of this instance.
282
	 */
283
	protected Object clone() throws CloneNotSupportedException
284
	{
285
		return super.clone();
286
	}
287
 
288
	/**
289
	 * Increment the major version by 1, and reset the minor and patch versions
290
	 * to 0.
291
	 */
292
	public void incrementMajor()
293
	{
294
		major++;
295
		minor = 0;
296
		patch = 0;
297
	}
298
 
299
	/**
300
	 * Increment the minor version by 1, and reset the patch version to 0. The 
301
	 * major version remains unaffected.
302
	 */
303
	public void incrementMinor()
304
	{
305
		minor++;
306
		patch = 0;
307
	}
308
 
309
	/**
310
	 * Increment the patch version by 1 The major and minor versions remain 
311
	 * unaffected.
312
	 */
313
	public void incrementPatch()
314
	{
315
		patch++;
316
	}
317
}