001: /*
002: * MCS Media Computer Software Copyright (c) 2005 by MCS
003: * -------------------------------------- Created on 16.01.2004 by w.klaas
004: *
005: * Licensed under the Apache License, Version 2.0 (the "License"); you may not
006: * use this file except in compliance with the License. You may obtain a copy of
007: * the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
013: * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
014: * License for the specific language governing permissions and limitations under
015: * the License.
016: */
017: package de.mcs.utils;
018:
019: /**
020: * this is a littel class to help versioning. Version could be build from a
021: * string with the following syntax: major.minor.release.build
022: *
023: * @author w.klaas
024: */
025: public class Version {
026:
027: /** major version. */
028: private int major;
029:
030: /** minor version. */
031: private int minor;
032:
033: /** release version. */
034: private int release;
035:
036: /** build version. */
037: private int build;
038:
039: /**
040: * constructor for empty version.
041: */
042: public Version() {
043: major = -1;
044: minor = -1;
045: release = -1;
046: build = -1;
047: }
048:
049: /**
050: * Constructor for creating a version object from an string.
051: *
052: * @param version
053: * string to convert
054: */
055: public Version(final String version) {
056: this ();
057: parseString(version);
058: }
059:
060: /**
061: * parsing a string into this version class.
062: *
063: * @param version
064: * string to parse
065: */
066: public final void parseString(final String version) {
067: String[] versionvalues = version.split("\\.");
068: for (int i = 0; i < versionvalues.length; i++) {
069: String string = versionvalues[i];
070: switch (i) {
071: case 0:
072: major = Integer.parseInt(string);
073: break;
074: case 1:
075: minor = Integer.parseInt(string);
076: break;
077: case 2:
078: release = Integer.parseInt(string);
079: break;
080: case 3:
081: build = Integer.parseInt(string);
082: break;
083: default:
084: break;
085: }
086: }
087: }
088:
089: /**
090: * constructor from another version class.
091: *
092: * @param version
093: * version to use
094: */
095: public Version(final Version version) {
096: this ();
097: this .major = version.major;
098: this .minor = version.minor;
099: this .release = version.release;
100: this .build = version.build;
101: }
102:
103: /** increment the major field. */
104: public final void incrementMajor() {
105: major++;
106: }
107:
108: /** increment the minor field. */
109: public final void incrementMinor() {
110: minor++;
111: }
112:
113: /** incementing one the release field. */
114: public final void incrementRelease() {
115: release++;
116: }
117:
118: /** increment the build field. */
119: public final void incrementBuild() {
120: build++;
121: }
122:
123: /** decrement the major field. */
124: public final void decrementMajor() {
125: major--;
126: }
127:
128: /** decrement the minor field. */
129: public final void decrementMinor() {
130: minor--;
131: }
132:
133: /** decrement the release field. */
134: public final void decrementRelease() {
135: release--;
136: }
137:
138: /** decrement the build field. */
139: public final void decrementBuild() {
140: build--;
141: }
142:
143: /**
144: * is this version geater than the destination version?
145: *
146: * @param dest
147: * destination version
148: * @return <code>true</code> if this version is greater than the
149: * destination version, else <code>false</code>
150: */
151: public final boolean greaterThan(final Version dest) {
152: if (major == dest.major) {
153: if (minor == dest.minor) {
154: if (release == dest.release) {
155: return build > dest.build;
156: } else {
157: return release > dest.release;
158: }
159: } else {
160: return minor > dest.minor;
161: }
162: } else {
163: return major > dest.major;
164: }
165: }
166:
167: /**
168: * is this version lesser than the destination version?
169: *
170: * @param dest
171: * destination version
172: * @return <code>true</code> if this version is lesser than the
173: * destination version, else <code>false</code>
174: */
175: public final boolean lesserThan(final Version dest) {
176: return !equals(dest) && !greaterThan(dest);
177: }
178:
179: /**
180: * is this version equal to the destination version?
181: *
182: * @param arg0
183: * destination version
184: * @return <code>true</code> if this version is eqaul than the destination
185: * version, else <code>false</code>
186: */
187: public final boolean equals(final Object arg0) {
188: if (arg0 instanceof Version) {
189: Version dest = (Version) arg0;
190: if (major == dest.major) {
191: if (minor == dest.minor) {
192: if (release == dest.release) {
193: return build == dest.build;
194: } else {
195: return false;
196: }
197: } else {
198: return false;
199: }
200: } else {
201: return false;
202: }
203: } else {
204: return false;
205: }
206: }
207:
208: /**
209: * converting this version to an version string.
210: *
211: * @return String
212: */
213: public final String toString() {
214: StringBuffer stringBuffer = new StringBuffer(20);
215: stringBuffer.append(major);
216: if (minor >= 0) {
217: stringBuffer.append(".").append(minor);
218: if (release >= 0) {
219: stringBuffer.append(".").append(release);
220: if (build >= 0) {
221: stringBuffer.append(".").append(build);
222: }
223: }
224: }
225: return stringBuffer.toString();
226: }
227:
228: /**
229: * Main methode for testing purpose only.
230: *
231: * @param args
232: * commandline arguments
233: */
234: public static void main(final String[] args) {
235: Version version = new Version("2.1.2.97");
236: Version version1 = new Version("2.1.2.96");
237: System.out.print("Version " + version.toString() + " > "
238: + version1.toString() + ": ");
239: System.out.println(version.greaterThan(version1));
240:
241: System.out.print("Version " + version.toString() + " < "
242: + version1.toString() + ": ");
243: System.out.println(version.lesserThan(version1));
244:
245: System.out.print("Version " + version.toString() + " = "
246: + version1.toString() + ": ");
247: System.out.println(version.equals(version1));
248:
249: System.out.print("Version " + version.toString()
250: + " = \"String\": ");
251: System.out.println(version.equals("String"));
252:
253: version1 = new Version(version.toString());
254: System.out.print("Version " + version.toString() + " > "
255: + version1.toString() + ": ");
256: System.out.println(version.greaterThan(version1));
257:
258: System.out.print("Version " + version.toString() + " < "
259: + version1.toString() + ": ");
260: System.out.println(version.lesserThan(version1));
261:
262: System.out.print("Version " + version.toString() + " = "
263: + version1.toString() + ": ");
264: System.out.println(version.equals(version1));
265:
266: version = new Version("2.1.97");
267: version1 = new Version("2.1.96");
268: System.out.print("Version " + version.toString() + " > "
269: + version1.toString() + ": ");
270: System.out.println(version.greaterThan(version1));
271: }
272:
273: /**
274: * getting the build field.
275: *
276: * @return int
277: */
278: public final int getBuild() {
279: return build;
280: }
281:
282: /**
283: * getting the major field.
284: *
285: * @return int
286: */
287: public final int getMajor() {
288: return major;
289: }
290:
291: /**
292: * getting the minor field.
293: *
294: * @return int
295: */
296: public final int getMinor() {
297: return minor;
298: }
299:
300: /**
301: * getting the release field.
302: *
303: * @return int
304: */
305: public final int getRelease() {
306: return release;
307: }
308:
309: /**
310: * getting the hashcode.
311: *
312: * @return int
313: */
314: public final int hashCode() {
315: int hash = major ^ minor ^ release ^ build;
316: return hash;
317: }
318:
319: /**
320: * @param aBuild
321: * setting the build number.
322: */
323: public final void setBuild(final int aBuild) {
324: this .build = aBuild;
325: }
326:
327: /**
328: * @param aMajor
329: * setting the major number.
330: */
331: public final void setMajor(final int aMajor) {
332: this .major = aMajor;
333: }
334:
335: /**
336: * @param aMinor
337: * setting the minor number.
338: */
339: public final void setMinor(final int aMinor) {
340: this .minor = aMinor;
341: }
342:
343: /**
344: * @param aRelease
345: * setting the release number.
346: */
347: public final void setRelease(final int aRelease) {
348: this.release = aRelease;
349: }
350:
351: }
|