/*
* Copyright 2001-2006 C:1 Financial Services GmbH
*
* This software is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License Version 2.1, as published by the Free Software Foundation.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA
*/
package de.finix.contelligent.core;
import java.util.ArrayList;
import java.util.StringTokenizer;
/**
* This class represents a version number in the form of Major.Minor.Patch. This
* is used to compare package version numbers.
*/
public class VersionNumber implements Comparable {
public VersionNumber(String number) throws IllegalArgumentException {
version = parse(number);
}
public int getMajor() {
return version[0];
}
public int getMinor() {
if (version.length == 1) {
return 0;
}
return version[1];
}
public int getPatch() {
if (version.length == 2) {
return 0;
}
return version[2];
}
protected int[] parse(String number) throws IllegalArgumentException {
StringTokenizer tokenizer = new StringTokenizer(number, ".");
ArrayList tmp = new ArrayList(3);
while (tokenizer.hasMoreTokens()) {
String token = tokenizer.nextToken();
try {
tmp.add(new Integer(token));
} catch (NumberFormatException e) {
throw new IllegalArgumentException("'" + number + "' is not a valid version number");
}
}
if ((tmp.size() > 3) || (tmp.size() == 0)) {
throw new IllegalArgumentException("'" + number + "' is not a valid version number");
}
int[] answer = new int[tmp.size()];
for (int i = 0; i < tmp.size(); i++) {
answer[i] = ((Integer) tmp.get(i)).intValue();
}
return answer;
}
public int compareTo(Object object) {
VersionNumber other = (VersionNumber) object;
// compare major
if (getMajor() < other.getMajor()) {
return -1;
}
if (getMajor() > other.getMajor()) {
return 1;
}
// equal major numbers --> compare minor
if (getMinor() < other.getMinor()) {
return -1;
}
if (getMinor() > other.getMinor()) {
return 1;
}
// equal major and minor numbers --> compare patch
if (getPatch() < other.getPatch()) {
return -1;
}
if (getPatch() > other.getPatch()) {
return 1;
}
// we are equal
return 0;
}
public String toString() {
return getMajor() + "." + getMinor() + "." + getPatch();
}
public boolean equals(Object obj) {
return compareTo(obj) == 0;
}
private int[] version;
}
|