001: /*
002: * hgcommons 7
003: * Hammurapi Group Common Library
004: * Copyright (C) 2003 Hammurapi Group
005: *
006: * This program is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2 of the License, or (at your option) any later version.
010: *
011: * This program is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
019: *
020: * URL: http://www.hammurapi.biz/hammurapi-biz/ef/xmenu/hammurapi-group/products/products/hgcommons/index.html
021: * e-Mail: support@hammurapi.biz
022: */
023: package biz.hammurapi.util;
024:
025: import java.util.StringTokenizer;
026:
027: /**
028: * Represents 3-digit version
029: * @author Pavel Vlasov
030: * @version $Revision: 1.2 $
031: */
032: public final class Version implements Comparable {
033: private int major;
034: private int minor;
035: private int micro;
036:
037: /**
038: * Create a new instance of a <code>Version</code> object with the
039: * specified version numbers.
040: *
041: * @param major Major version number.
042: * @param minor Minor version number.
043: * @param rev Micro version number.
044: */
045: public Version(int major, int minor, int micro) {
046: this .major = major;
047: this .minor = minor;
048: this .micro = micro;
049: }
050:
051: public Version(String version) {
052: StringTokenizer st = new StringTokenizer(version, ".");
053: if (st.countTokens() == 3) {
054: major = Integer.parseInt(st.nextToken());
055: minor = Integer.parseInt(st.nextToken());
056: micro = Integer.parseInt(st.nextToken());
057: } else {
058: throw new IllegalArgumentException(
059: "Invalid version format: " + version);
060: }
061: }
062:
063: /**
064: *
065: * @param other
066: * @return
067: */
068: public boolean equals(Object o) {
069: if (o instanceof Version) {
070: Version v = (Version) o;
071: return v.major == major && v.minor == minor
072: && v.micro == micro;
073: } else {
074: return super .equals(o);
075: }
076: }
077:
078: /**
079: * Overload toString to report version correctly.
080: *
081: * @return the dot seperated version string
082: */
083: public String toString() {
084: return major + "." + minor + "." + micro;
085: }
086:
087: public int compareTo(Object o) {
088: if (o instanceof Version) {
089: Version v = (Version) o;
090: if (v.major == major) {
091: if (v.minor == minor) {
092: return micro - v.micro;
093: } else {
094: return minor - v.minor;
095: }
096: } else {
097: return major - v.major;
098: }
099: } else {
100: return hashCode() - o.hashCode();
101: }
102: }
103:
104: public int hashCode() {
105: return major ^ minor ^ micro;
106: }
107:
108: public int getMajor() {
109: return major;
110: }
111:
112: public int getMicro() {
113: return micro;
114: }
115:
116: public int getMinor() {
117: return minor;
118: }
119: }
|