001: /*
002: * Copyright 2001-2006 C:1 Financial Services GmbH
003: *
004: * This software is free software; you can redistribute it and/or
005: * modify it under the terms of the GNU Lesser General Public
006: * License Version 2.1, as published by the Free Software Foundation.
007: *
008: * This software is distributed in the hope that it will be useful,
009: * but WITHOUT ANY WARRANTY; without even the implied warranty of
010: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
011: * Lesser General Public License for more details.
012: *
013: * You should have received a copy of the GNU Lesser General Public
014: * License along with this library; if not, write to the Free Software
015: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA
016: */
017:
018: package de.finix.contelligent.core;
019:
020: import java.util.ArrayList;
021: import java.util.StringTokenizer;
022:
023: /**
024: * This class represents a version number in the form of Major.Minor.Patch. This
025: * is used to compare package version numbers.
026: */
027: public class VersionNumber implements Comparable {
028:
029: public VersionNumber(String number) throws IllegalArgumentException {
030: version = parse(number);
031: }
032:
033: public int getMajor() {
034: return version[0];
035: }
036:
037: public int getMinor() {
038: if (version.length == 1) {
039: return 0;
040: }
041: return version[1];
042: }
043:
044: public int getPatch() {
045: if (version.length == 2) {
046: return 0;
047: }
048: return version[2];
049: }
050:
051: protected int[] parse(String number)
052: throws IllegalArgumentException {
053: StringTokenizer tokenizer = new StringTokenizer(number, ".");
054: ArrayList tmp = new ArrayList(3);
055:
056: while (tokenizer.hasMoreTokens()) {
057: String token = tokenizer.nextToken();
058: try {
059: tmp.add(new Integer(token));
060: } catch (NumberFormatException e) {
061: throw new IllegalArgumentException("'" + number
062: + "' is not a valid version number");
063: }
064: }
065: if ((tmp.size() > 3) || (tmp.size() == 0)) {
066: throw new IllegalArgumentException("'" + number
067: + "' is not a valid version number");
068: }
069: int[] answer = new int[tmp.size()];
070:
071: for (int i = 0; i < tmp.size(); i++) {
072: answer[i] = ((Integer) tmp.get(i)).intValue();
073: }
074: return answer;
075: }
076:
077: public int compareTo(Object object) {
078: VersionNumber other = (VersionNumber) object;
079:
080: // compare major
081: if (getMajor() < other.getMajor()) {
082: return -1;
083: }
084: if (getMajor() > other.getMajor()) {
085: return 1;
086: }
087: // equal major numbers --> compare minor
088: if (getMinor() < other.getMinor()) {
089: return -1;
090: }
091: if (getMinor() > other.getMinor()) {
092: return 1;
093: }
094: // equal major and minor numbers --> compare patch
095: if (getPatch() < other.getPatch()) {
096: return -1;
097: }
098: if (getPatch() > other.getPatch()) {
099: return 1;
100: }
101: // we are equal
102: return 0;
103: }
104:
105: public String toString() {
106: return getMajor() + "." + getMinor() + "." + getPatch();
107: }
108:
109: public boolean equals(Object obj) {
110: return compareTo(obj) == 0;
111: }
112:
113: private int[] version;
114: }
|