001: // ========================================================================
002: // Copyright (c) 2002 Mort Bay Consulting (Australia) Pty. Ltd.
003: // $Id: Version.java 6177 2007-02-19 10:11:27Z aaime $
004: // ========================================================================
005: package org.mortbay.start;
006:
007: /**
008: * Utility class for parsing and comparing version strings.
009: * JDK 1.1 compatible.
010: * @author Jan Hlavat�
011: */
012: public class Version {
013: int _version = 0;
014: int _revision = 0;
015: int _subrevision = 0;
016: String _suffix = "";
017:
018: public Version() {
019: }
020:
021: public Version(String version_string) {
022: parse(version_string);
023: }
024:
025: /**
026: * parses version string in the form version[.revision[.subrevision[extension]]]
027: * into this instance.
028: */
029: public void parse(String version_string) {
030: try {
031: _version = 1;
032: _revision = 2;
033: _subrevision = 0;
034: _suffix = "?";
035:
036: int pos = 0;
037: int startpos = 0;
038: int endpos = version_string.length();
039:
040: while ((pos < endpos)
041: && Character.isDigit(version_string.charAt(pos))) {
042: pos++;
043: }
044:
045: _version = Integer.parseInt(version_string.substring(
046: startpos, pos));
047:
048: if ((pos < endpos) && (version_string.charAt(pos) == '.')) {
049: startpos = ++pos;
050:
051: while ((pos < endpos)
052: && Character
053: .isDigit(version_string.charAt(pos))) {
054: pos++;
055: }
056:
057: _revision = Integer.parseInt(version_string.substring(
058: startpos, pos));
059: }
060:
061: if ((pos < endpos) && (version_string.charAt(pos) == '.')) {
062: startpos = ++pos;
063:
064: while ((pos < endpos)
065: && Character
066: .isDigit(version_string.charAt(pos))) {
067: pos++;
068: }
069:
070: _subrevision = Integer.parseInt(version_string
071: .substring(startpos, pos));
072: }
073:
074: if (pos < endpos) {
075: _suffix = version_string.substring(pos);
076: }
077: } catch (Exception e) {
078: e.printStackTrace();
079: }
080: }
081:
082: /**
083: * @return string representation of this version
084: */
085: public String toString() {
086: StringBuffer sb = new StringBuffer(10);
087: sb.append(_version);
088: sb.append('.');
089: sb.append(_revision);
090: sb.append('.');
091: sb.append(_subrevision);
092: sb.append(_suffix);
093:
094: return sb.toString();
095: }
096:
097: // java.lang.Comparable is Java 1.2! Cannot use it
098: /**
099: * Compares with other version. Does not take extension into account,
100: * as there is no reliable way to order them.
101: * @return -1 if this is older version that other,
102: * 0 if its same version,
103: * 1 if it's newer version than other
104: */
105: public int compare(Version other) {
106: if (other == null) {
107: throw new NullPointerException("other version is null");
108: }
109:
110: if (this ._version < other._version) {
111: return -1;
112: }
113:
114: if (this ._version > other._version) {
115: return 1;
116: }
117:
118: if (this ._revision < other._revision) {
119: return -1;
120: }
121:
122: if (this ._revision > other._revision) {
123: return 1;
124: }
125:
126: if (this ._subrevision < other._subrevision) {
127: return -1;
128: }
129:
130: if (this ._subrevision > other._subrevision) {
131: return 1;
132: }
133:
134: return 0;
135: }
136:
137: /**
138: * Check whether this verion is in range of versions specified
139: */
140: public boolean isInRange(Version low, Version high) {
141: return ((compare(low) >= 0) && (compare(high) <= 0));
142: }
143: }
|