KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > opensugar > cube > packageAdmin > GoRoCoVersion


1 /*
2  * JEFFREE: Java(TM) Embedded Framework FREE
3  * Copyright (C) 1999-2003 - Opensugar
4  *
5  * The contents of this file are subject to the Jeffree Public License,
6  * as defined by the file JEFFREE_LICENSE.TXT
7  *
8  * You may not use this file except in compliance with the License.
9  * You may obtain a copy of the License on the Objectweb web site
10  * (www.objectweb.org).
11  *
12  * Software distributed under the License is distributed on an "AS IS" basis,
13  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
14  * the specific terms governing rights and limitations under the License.
15  *
16  * The Original Code is JEFFREE, including the java package com.opensugar.cube,
17  * released January 1, 2003.
18  *
19  * The Initial Developer of the Original Code is Opensugar.
20  * The Original Code is Copyright Opensugar.
21  * All Rights Reserved.
22  *
23  * Initial developer(s): Pierre Scokaert (Opensugar)
24  * Contributor(s):
25  */

26
27 package com.opensugar.cube.packageAdmin;
28
29 import java.util.StringTokenizer JavaDoc;
30
31 // This class represents a package version of type Generation.Revision.Correction
32
// and formatted as '<int>[.<int>[.<int>]]'.
33
public class GoRoCoVersion {
34
35    // Undefined version:
36
// generation = null
37
// revision = Integer.MIN_VALUE
38
// correction = Integer.MIN_VALUE
39

40    // If revision or correction not defined, their value is Integer.MIN_VALUE
41

42    // the version generation
43
private Integer JavaDoc generation = null;
44    // the version revision (if any)
45
private Integer JavaDoc revision = new Integer JavaDoc( Integer.MIN_VALUE );
46    // the version correction (if any)
47
private Integer JavaDoc correction = new Integer JavaDoc( Integer.MIN_VALUE );
48
49    // Value can be null (for an undefined version)
50
public GoRoCoVersion( String JavaDoc value ) {
51       if ( value != null ) {
52          // If version string is wrapped in quotes, remove the quotes.
53
if ( ( value.startsWith( "\"" ) && value.endsWith( "\"" ) ) || ( value.startsWith( "'" ) && value.endsWith( "'" ) ) ) {
54             value = value.substring( 1, value.length() - 1 );
55          }
56          // Split the version string into its dot separated parts.
57
StringTokenizer JavaDoc tokens = new StringTokenizer JavaDoc( value, "." );
58          try {
59             // The version string contains at least one part which is the generation.
60
generation = Integer.valueOf( tokens.nextToken() );
61             // The version string may also contain a revision part.
62
if ( tokens.hasMoreTokens() ) {
63                revision = Integer.valueOf( tokens.nextToken() );
64             }
65             // Finally, it may contain one last part which is the correction.
66
if ( tokens.hasMoreTokens() ) {
67                correction = Integer.valueOf( tokens.nextToken() );
68             }
69             // If the version string has more than three dot separated parts, it is an invalid
70
// version string.
71
if ( tokens.hasMoreTokens() ) {
72                // ignore further tokens
73
//throw new IllegalArgumentException( "Invalid version string: too many tokens" );
74
}
75          }
76          catch( NumberFormatException JavaDoc e ) {
77             // If any one of the version string's dot separated parts is not an integer, the version
78
// string is ininvalid
79
// consider version is null
80
generation = null;
81             revision = new Integer JavaDoc( Integer.MIN_VALUE );
82             correction = new Integer JavaDoc( Integer.MIN_VALUE );
83          }
84       }
85    }
86
87    // Return this version's generation.
88
protected Integer JavaDoc getGeneration() {
89       return generation;
90    }
91
92    // Return this version's revision.
93
protected Integer JavaDoc getRevision() {
94       return revision;
95    }
96
97    // Return this version's correction.
98
protected Integer JavaDoc getCorrection() {
99       return correction;
100    }
101
102    // Implementation of the Comparable interface's compareTo method.
103
// Compares this version to another version, returning zero if the two versions are
104
// equal, a negative integer if this version is smaller than the version to compare
105
// to, and a positive integer otherwise.
106
//
107
// Note that the value returned gives no indication of how different the versions are;
108
// it just gives an indication of which one is larger.
109
public int compareTo( Object JavaDoc obj ) {
110       GoRoCoVersion versionToCompareTo = (GoRoCoVersion)obj;
111
112       if ( isUndefined() && versionToCompareTo.isUndefined() ) {
113          // if both versions are undefined, the versions are considered equal
114
return 0;
115       }
116       else if ( isUndefined() || versionToCompareTo.isUndefined() ) {
117          // if either of the versions (but not both) is undefined, this version
118
// is considered larger
119
return 1;
120       }
121
122       Integer JavaDoc generation2 = versionToCompareTo.getGeneration();
123       Integer JavaDoc revision2 = versionToCompareTo.getRevision();
124       Integer JavaDoc correction2 = versionToCompareTo.getCorrection();
125
126       // Compare generations.
127
int n = generation.intValue() - generation2.intValue();
128       if ( n == 0 ) {
129          // If generations are equal, compare revisions.
130
n = revision.intValue() - revision2.intValue();
131          if ( n == 0 ) {
132             // If generations and revisions are equal, compare corrections.
133
return correction.intValue() - correction2.intValue();
134          }
135          return n;
136       }
137       return n;
138    }
139
140    // Determine if this version is equal to another version.
141
public boolean equals( Object JavaDoc obj ) {
142       if ( compareTo( obj ) == 0 ) {
143          return true;
144       }
145       return false;
146    }
147
148    // Return a string representation of this version.
149
public String JavaDoc toString() {
150       if ( isUndefined() ) {
151          return "Undefined";
152       }
153       else {
154          StringBuffer JavaDoc sb = new StringBuffer JavaDoc( generation.toString() );
155          // A negative revision value means a revision is not specified in this version.
156
if ( revision.intValue() >= 0 ) {
157             sb.append( "." + revision.toString() );
158             // A negative correction value means a correction is not specified in this version.
159
if ( correction.intValue() >= 0 ) {
160                sb.append( "." + correction.toString() );
161             }
162          }
163          return sb.toString();
164       }
165    }
166
167    // Determine if this version is undefined.
168
public boolean isUndefined() {
169       if ( generation == null ) {
170          return true;
171       }
172       return false;
173    }
174
175 }
Popular Tags