001: // ========================================================================
002: // Copyright 2002-2005 Mort Bay Consulting Pty. Ltd.
003: // ------------------------------------------------------------------------
004: // Licensed under the Apache License, Version 2.0 (the "License");
005: // you may not use this file except in compliance with the License.
006: // You may obtain a copy of the License at
007: // http://www.apache.org/licenses/LICENSE-2.0
008: // Unless required by applicable law or agreed to in writing, software
009: // distributed under the License is distributed on an "AS IS" BASIS,
010: // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
011: // See the License for the specific language governing permissions and
012: // limitations under the License.
013: // ========================================================================
014: package org.mortbay.start;
015:
016: /**
017: * Utility class for parsing and comparing version strings.
018: * JDK 1.1 compatible.
019: * @author Jan Hlavatý
020: */
021:
022: public class Version {
023:
024: int _version = 0;
025: int _revision = 0;
026: int _subrevision = 0;
027: String _suffix = "";
028:
029: public Version() {
030: }
031:
032: public Version(String version_string) {
033: parse(version_string);
034: }
035:
036: /**
037: * parses version string in the form version[.revision[.subrevision[extension]]]
038: * into this instance.
039: */
040: public void parse(String version_string) {
041: _version = 0;
042: _revision = 0;
043: _subrevision = 0;
044: _suffix = "";
045: int pos = 0;
046: int startpos = 0;
047: int endpos = version_string.length();
048: while ((pos < endpos)
049: && Character.isDigit(version_string.charAt(pos))) {
050: pos++;
051: }
052: _version = Integer.parseInt(version_string.substring(startpos,
053: pos));
054: if ((pos < endpos) && version_string.charAt(pos) == '.') {
055: startpos = ++pos;
056: while ((pos < endpos)
057: && Character.isDigit(version_string.charAt(pos))) {
058: pos++;
059: }
060: _revision = Integer.parseInt(version_string.substring(
061: startpos, pos));
062: }
063: if ((pos < endpos) && version_string.charAt(pos) == '.') {
064: startpos = ++pos;
065: while ((pos < endpos)
066: && Character.isDigit(version_string.charAt(pos))) {
067: pos++;
068: }
069: _subrevision = Integer.parseInt(version_string.substring(
070: startpos, pos));
071: }
072: if (pos < endpos) {
073: _suffix = version_string.substring(pos);
074: }
075: }
076:
077: /**
078: * @return string representation of this version
079: */
080: public String toString() {
081: StringBuffer sb = new StringBuffer(10);
082: sb.append(_version);
083: sb.append('.');
084: sb.append(_revision);
085: sb.append('.');
086: sb.append(_subrevision);
087: sb.append(_suffix);
088: return sb.toString();
089: }
090:
091: // java.lang.Comparable is Java 1.2! Cannot use it
092: /**
093: * Compares with other version. Does not take extension into account,
094: * as there is no reliable way to order them.
095: * @return -1 if this is older version that other,
096: * 0 if its same version,
097: * 1 if it's newer version than other
098: */
099: public int compare(Version other) {
100: if (other == null)
101: throw new NullPointerException("other version is null");
102: if (this ._version < other._version)
103: return -1;
104: if (this ._version > other._version)
105: return 1;
106: if (this ._revision < other._revision)
107: return -1;
108: if (this ._revision > other._revision)
109: return 1;
110: if (this ._subrevision < other._subrevision)
111: return -1;
112: if (this ._subrevision > other._subrevision)
113: return 1;
114: return 0;
115: }
116:
117: /**
118: * Check whether this verion is in range of versions specified
119: */
120: public boolean isInRange(Version low, Version high) {
121: return (compare(low) >= 0 && compare(high) <= 0);
122: }
123: }
|