KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mortbay > start > Version


1 // ========================================================================
2
// $Id: Version.java,v 1.2 2004/05/09 20:32:46 gregwilkins Exp $
3
// Copyright 2002-2004 Mort Bay Consulting Pty. Ltd.
4
// ------------------------------------------------------------------------
5
// Licensed under the Apache License, Version 2.0 (the "License");
6
// you may not use this file except in compliance with the License.
7
// You may obtain a copy of the License at
8
// http://www.apache.org/licenses/LICENSE-2.0
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14
// ========================================================================
15
package org.mortbay.start;
16
17 /**
18  * Utility class for parsing and comparing version strings.
19  * JDK 1.1 compatible.
20  * @author Jan Hlavatý
21  */

22  
23 public class Version {
24  
25     int _version = 0;
26     int _revision = 0;
27     int _subrevision = 0;
28     String JavaDoc _suffix = "";
29     
30     public Version() {
31     }
32     
33     public Version(String JavaDoc version_string) {
34         parse(version_string);
35     }
36     
37     /**
38      * parses version string in the form version[.revision[.subrevision[extension]]]
39      * into this instance.
40      */

41     public void parse(String JavaDoc version_string) {
42         _version = 0;
43         _revision = 0;
44         _subrevision = 0;
45         _suffix = "";
46         int pos = 0;
47         int startpos = 0;
48         int endpos = version_string.length();
49         while ( (pos < endpos) && Character.isDigit(version_string.charAt(pos))) {
50             pos++;
51         }
52         _version = Integer.parseInt(version_string.substring(startpos,pos));
53         if ((pos < endpos) && version_string.charAt(pos)=='.') {
54             startpos = ++pos;
55             while ( (pos < endpos) && Character.isDigit(version_string.charAt(pos))) {
56                 pos++;
57             }
58             _revision = Integer.parseInt(version_string.substring(startpos,pos));
59         }
60         if ((pos < endpos) && version_string.charAt(pos)=='.') {
61             startpos = ++pos;
62             while ( (pos < endpos) && Character.isDigit(version_string.charAt(pos))) {
63                 pos++;
64             }
65             _subrevision = Integer.parseInt(version_string.substring(startpos,pos));
66         }
67         if (pos < endpos) {
68             _suffix = version_string.substring(pos);
69         }
70     }
71     
72     /**
73      * @return string representation of this version
74      */

75     public String JavaDoc toString() {
76         StringBuffer JavaDoc sb = new StringBuffer JavaDoc(10);
77         sb.append(_version);
78         sb.append('.');
79         sb.append(_revision);
80         sb.append('.');
81         sb.append(_subrevision);
82         sb.append(_suffix);
83         return sb.toString();
84     }
85     
86     // java.lang.Comparable is Java 1.2! Cannot use it
87
/**
88      * Compares with other version. Does not take extension into account,
89      * as there is no reliable way to order them.
90      * @return -1 if this is older version that other,
91      * 0 if its same version,
92      * 1 if it's newer version than other
93      */

94     public int compare(Version other) {
95         if (other == null) throw new NullPointerException JavaDoc("other version is null");
96         if (this._version < other._version) return -1;
97         if (this._version > other._version) return 1;
98         if (this._revision < other._revision) return -1;
99         if (this._revision > other._revision) return 1;
100         if (this._subrevision < other._subrevision) return -1;
101         if (this._subrevision > other._subrevision) return 1;
102         return 0;
103     }
104     
105     /**
106      * Check whether this verion is in range of versions specified
107      */

108     public boolean isInRange(Version low, Version high) {
109         return (compare(low)>=0 && compare(high)<=0);
110     }
111 }
112
Popular Tags