1 //
2 // ========================================================================
3 // Copyright (c) 1995-2014 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. JDK 1.1 compatible.
23 *
24 */
25
26 public class Version
27 {
28
29 int _version = 0;
30 int _revision = 0;
31 int _subrevision = 0;
32 String _suffix = "";
33
34 public Version()
35 {
36 }
37
38 public Version(String version_string)
39 {
40 parse(version_string);
41 }
42
43 // java.lang.Comparable is Java 1.2! Cannot use it
44 /**
45 * Compares with other version. Does not take extension into account, as there is no reliable way to order them.
46 *
47 * @return -1 if this is older version that other, 0 if its same version, 1 if it's newer version than other
48 */
49 public int compare(Version other)
50 {
51 if (other == null)
52 {
53 throw new NullPointerException("other version is null");
54 }
55 if (this._version < other._version)
56 {
57 return -1;
58 }
59 if (this._version > other._version)
60 {
61 return 1;
62 }
63 if (this._revision < other._revision)
64 {
65 return -1;
66 }
67 if (this._revision > other._revision)
68 {
69 return 1;
70 }
71 if (this._subrevision < other._subrevision)
72 {
73 return -1;
74 }
75 if (this._subrevision > other._subrevision)
76 {
77 return 1;
78 }
79 return 0;
80 }
81
82 /**
83 * Check whether this verion is in range of versions specified
84 */
85 public boolean isInRange(Version low, Version high)
86 {
87 return ((compare(low) >= 0) && (compare(high) <= 0));
88 }
89
90 /**
91 * parses version string in the form version[.revision[.subrevision[extension]]] into this instance.
92 */
93 public void parse(String version_string)
94 {
95 _version = 0;
96 _revision = 0;
97 _subrevision = 0;
98 _suffix = "";
99 int pos = 0;
100 int startpos = 0;
101 int endpos = version_string.length();
102 while ((pos < endpos) && Character.isDigit(version_string.charAt(pos)))
103 {
104 pos++;
105 }
106 _version = Integer.parseInt(version_string.substring(startpos,pos));
107 if ((pos < endpos) && (version_string.charAt(pos) == '.'))
108 {
109 startpos = ++pos;
110 while ((pos < endpos) && Character.isDigit(version_string.charAt(pos)))
111 {
112 pos++;
113 }
114 _revision = Integer.parseInt(version_string.substring(startpos,pos));
115 }
116 if ((pos < endpos) && (version_string.charAt(pos) == '.'))
117 {
118 startpos = ++pos;
119 while ((pos < endpos) && Character.isDigit(version_string.charAt(pos)))
120 {
121 pos++;
122 }
123 _subrevision = Integer.parseInt(version_string.substring(startpos,pos));
124 }
125 if (pos < endpos)
126 {
127 _suffix = version_string.substring(pos);
128 }
129 }
130
131 /**
132 * @return string representation of this version
133 */
134 @Override
135 public String toString()
136 {
137 StringBuffer sb = new StringBuffer(10);
138 sb.append(_version);
139 sb.append('.');
140 sb.append(_revision);
141 sb.append('.');
142 sb.append(_subrevision);
143 sb.append(_suffix);
144 return sb.toString();
145 }
146 }