View Javadoc

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