View Javadoc

1   // ========================================================================
2   // Copyright (c) 2002-2009 Mort Bay Consulting Pty. Ltd.
3   // ------------------------------------------------------------------------
4   // All rights reserved. This program and the accompanying materials
5   // are made available under the terms of the Eclipse Public License v1.0
6   // and Apache License v2.0 which accompanies this distribution.
7   // The Eclipse Public License is available at 
8   // http://www.eclipse.org/legal/epl-v10.html
9   // The Apache License v2.0 is available at
10  // http://www.opensource.org/licenses/apache2.0.php
11  // You may elect to redistribute this code under either of these licenses. 
12  // ========================================================================
13  package org.eclipse.jetty.start;
14  
15  /**
16   * Utility class for parsing and comparing version strings.
17   * JDK 1.1 compatible.
18   * 
19   */
20   
21  public class Version {
22   
23      int _version = 0;
24      int _revision = 0;
25      int _subrevision = 0;
26      String _suffix = "";
27      
28      public Version() {
29      }
30      
31      public Version(String version_string) {
32          parse(version_string);
33      }
34      
35      /**
36       * parses version string in the form version[.revision[.subrevision[extension]]]
37       * into this instance.
38       */
39      public void parse(String version_string) {
40          _version = 0;
41          _revision = 0;
42          _subrevision = 0;
43          _suffix = "";
44          int pos = 0;
45          int startpos = 0;
46          int endpos = version_string.length();
47          while ( (pos < endpos) && Character.isDigit(version_string.charAt(pos))) {
48              pos++;
49          }
50          _version = Integer.parseInt(version_string.substring(startpos,pos));
51          if ((pos < endpos) && version_string.charAt(pos)=='.') {
52              startpos = ++pos;
53              while ( (pos < endpos) && Character.isDigit(version_string.charAt(pos))) {
54                  pos++;
55              }
56              _revision = Integer.parseInt(version_string.substring(startpos,pos));
57          }
58          if ((pos < endpos) && version_string.charAt(pos)=='.') {
59              startpos = ++pos;
60              while ( (pos < endpos) && Character.isDigit(version_string.charAt(pos))) {
61                  pos++;
62              }
63              _subrevision = Integer.parseInt(version_string.substring(startpos,pos));
64          }
65          if (pos < endpos) {
66              _suffix = version_string.substring(pos);
67          }
68      }
69      
70      /**
71       * @return string representation of this version
72       */
73      public String toString() {
74          StringBuffer sb = new StringBuffer(10);
75          sb.append(_version);
76          sb.append('.');
77          sb.append(_revision);
78          sb.append('.');
79          sb.append(_subrevision);
80          sb.append(_suffix);
81          return sb.toString();
82      }
83      
84      // java.lang.Comparable is Java 1.2! Cannot use it
85      /**
86       * Compares with other version. Does not take extension into account,
87       * as there is no reliable way to order them.
88       * @return -1 if this is older version that other,
89       *         0 if its same version,
90       *         1 if it's newer version than other
91       */
92      public int compare(Version other) {
93          if (other == null) throw new NullPointerException("other version is null");
94          if (this._version < other._version) return -1;
95          if (this._version > other._version) return 1;
96          if (this._revision < other._revision) return -1;
97          if (this._revision > other._revision) return 1;
98          if (this._subrevision < other._subrevision) return -1;
99          if (this._subrevision > other._subrevision) return 1;
100         return 0;
101     }
102     
103     /**
104      * Check whether this verion is in range of versions specified
105      */
106     public boolean isInRange(Version low, Version high) {
107         return (compare(low)>=0 && compare(high)<=0);
108     }
109 }