View Javadoc

    1   // ========================================================================
    2   // Copyright 2002-2005 Mort Bay Consulting Pty. Ltd.
    3   // ------------------------------------------------------------------------
    4   // Licensed under the Apache License, Version 2.0 (the "License");
    5   // you may not use this file except in compliance with the License.
    6   // You may obtain a copy of the License at
    7   // http://www.apache.org/licenses/LICENSE-2.0
    8   // Unless required by applicable law or agreed to in writing, software
    9   // distributed under the License is distributed on an "AS IS" BASIS,
    10  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    11  // See the License for the specific language governing permissions and
    12  // limitations under the License.
    13  // ========================================================================
    14  package org.mortbay.start;
    15
    16  /**
    17   * Utility class for parsing and comparing version strings.
    18   * JDK 1.1 compatible.
    19   * @author Jan Hlavatý
    20   */
    21
    22  public class Version {
    23
    24      int _version = 0;
    25      int _revision = 0;
    26      int _subrevision = 0;
    27      String _suffix = "";
    28
    29      public Version() {
    30      }
    31
    32      public Version(String version_string) {
    33          parse(version_string);
    34      }
    35
    36      /**
    37       * parses version string in the form version[.revision[.subrevision[extension]]]
    38       * into this instance.
    39       */
    40      public void parse(String version_string) {
    41          _version = 0;
    42          _revision = 0;
    43          _subrevision = 0;
    44          _suffix = "";
    45          int pos = 0;
    46          int startpos = 0;
    47          int endpos = version_string.length();
    48          while ( (pos < endpos) && Character.isDigit(version_string.charAt(pos))) {
    49              pos++;
    50          }
    51          _version = Integer.parseInt(version_string.substring(startpos,pos));
    52          if ((pos < endpos) && version_string.charAt(pos)=='.') {
    53              startpos = ++pos;
    54              while ( (pos < endpos) && Character.isDigit(version_string.charAt(pos))) {
    55                  pos++;
    56              }
    57              _revision = Integer.parseInt(version_string.substring(startpos,pos));
    58          }
    59          if ((pos < endpos) && version_string.charAt(pos)=='.') {
    60              startpos = ++pos;
    61              while ( (pos < endpos) && Character.isDigit(version_string.charAt(pos))) {
    62                  pos++;
    63              }
    64              _subrevision = Integer.parseInt(version_string.substring(startpos,pos));
    65          }
    66          if (pos < endpos) {
    67              _suffix = version_string.substring(pos);
    68          }
    69      }
    70
    71      /**
    72       * @return string representation of this version
    73       */
    74      public String toString() {
    75          StringBuffer sb = new StringBuffer(10);
    76          sb.append(_version);
    77          sb.append('.');
    78          sb.append(_revision);
    79          sb.append('.');
    80          sb.append(_subrevision);
    81          sb.append(_suffix);
    82          return sb.toString();
    83      }
    84
    85      // java.lang.Comparable is Java 1.2! Cannot use it
    86      /**
    87       * Compares with other version. Does not take extension into account,
    88       * as there is no reliable way to order them.
    89       * @return -1 if this is older version that other,
    90       *         0 if its same version,
    91       *         1 if it's newer version than other
    92       */
    93      public int compare(Version other) {
    94          if (other == null) throw new NullPointerException("other version is null");
    95          if (this._version < other._version) return -1;
    96          if (this._version > other._version) return 1;
    97          if (this._revision < other._revision) return -1;
    98          if (this._revision > other._revision) return 1;
    99          if (this._subrevision < other._subrevision) return -1;
    100         if (this._subrevision > other._subrevision) return 1;
    101         return 0;
    102     }
    103
    104     /**
    105      * Check whether this verion is in range of versions specified
    106      */
    107     public boolean isInRange(Version low, Version high) {
    108         return (compare(low)>=0 && compare(high)<=0);
    109     }
    110 }