KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jnlp > sample > util > VersionID


1 /*
2  * @(#)VersionID.java 1.7 05/11/17
3  *
4  * Copyright (c) 2006 Sun Microsystems, Inc. All Rights Reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are met:
8  *
9  * -Redistribution of source code must retain the above copyright notice, this
10  * list of conditions and the following disclaimer.
11  *
12  * -Redistribution in binary form must reproduce the above copyright notice,
13  * this list of conditions and the following disclaimer in the documentation
14  * and/or other materials provided with the distribution.
15  *
16  * Neither the name of Sun Microsystems, Inc. or the names of contributors may
17  * be used to endorse or promote products derived from this software without
18  * specific prior written permission.
19  *
20  * This software is provided "AS IS," without a warranty of any kind. ALL
21  * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING
22  * ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE
23  * OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN MIDROSYSTEMS, INC. ("SUN")
24  * AND ITS LICENSORS SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE
25  * AS A RESULT OF USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS
26  * DERIVATIVES. IN NO EVENT WILL SUN OR ITS LICENSORS BE LIABLE FOR ANY LOST
27  * REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL,
28  * INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY
29  * OF LIABILITY, ARISING OUT OF THE USE OF OR INABILITY TO USE THIS SOFTWARE,
30  * EVEN IF SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
31  *
32  * You acknowledge that this software is not designed, licensed or intended
33  * for use in the design, construction, operation or maintenance of any
34  * nuclear facility.
35  */

36
37 package jnlp.sample.util;
38
39 import java.util.ArrayList JavaDoc;
40 import java.util.Arrays JavaDoc;
41
42 /**
43  * VersionID contains a JNLP version ID.
44  *
45  * The VersionID also contains a prefix indicator that can
46  * be used when stored with a VersionString
47  *
48  */

49 public class VersionID implements Comparable JavaDoc {
50     private String JavaDoc[] _tuple; // Array of Integer or String objects
51
private boolean _usePrefixMatch; // star (*) prefix
52
private boolean _useGreaterThan; // plus (+) greather-than
53
private boolean _isCompound; // and (&) operator
54
private VersionID _rest; // remaining part after the &
55

56     /** Creates a VersionID object */
57     public VersionID(String JavaDoc str) {
58     _usePrefixMatch = false;
59     _useGreaterThan = false;
60     _isCompound = false;
61     if (str == null && str.length() == 0) {
62         _tuple = new String JavaDoc[0];
63         return;
64     }
65
66     // Check for compound
67
int amp = str.indexOf("&");
68     if (amp >= 0) {
69         _isCompound = true;
70         VersionID firstPart = new VersionID(str.substring(0, amp));
71         _rest = new VersionID(str.substring(amp+1));
72         _tuple = firstPart._tuple;
73         _usePrefixMatch = firstPart._usePrefixMatch;
74         _useGreaterThan = firstPart._useGreaterThan;
75     } else {
76         // Check for postfix
77
if (str.endsWith("+")) {
78             _useGreaterThan = true;
79             str = str.substring(0, str.length() - 1);
80         } else if (str.endsWith("*")) {
81             _usePrefixMatch = true;
82             str = str.substring(0, str.length() - 1);
83         }
84     
85         ArrayList JavaDoc list = new ArrayList JavaDoc();
86         int start = 0;
87         for(int i = 0; i < str.length(); i++) {
88             // Split at each separator character
89
if (".-_".indexOf(str.charAt(i)) != -1) {
90             if (start < i) {
91                 String JavaDoc value = str.substring(start, i);
92                 list.add(value);
93             }
94             start = i + 1;
95             }
96         }
97         if (start < str.length()) {
98         list.add(str.substring(start, str.length()));
99         }
100         _tuple = new String JavaDoc[list.size()];
101         _tuple = (String JavaDoc[])list.toArray(_tuple);
102     }
103     }
104     
105     /** Returns true if no flags are set */
106     public boolean isSimpleVersion() {
107     return !_useGreaterThan && !_usePrefixMatch && !_isCompound;
108     }
109     
110     /** Match 'this' versionID against vid.
111      * The _usePrefixMatch/_useGreaterThan flag is used to determine if a
112      * prefix match of an exact match should be performed
113      * if _isCompound, must match _rest also.
114      */

115     public boolean match(VersionID vid) {
116     if (_isCompound) {
117         if (!_rest.match(vid)) {
118         return false;
119         }
120     }
121     return (_usePrefixMatch) ? this.isPrefixMatch(vid) :
122         (_useGreaterThan) ? vid.isGreaterThanOrEqual(this) :
123         matchTuple(vid);
124     }
125
126     /** Compares if two version IDs are equal */
127     public boolean equals(Object JavaDoc o) {
128     if (matchTuple(o)) {
129          VersionID ov = (VersionID) o;
130          if (_rest == null || _rest.equals(ov._rest)) {
131         if ((_useGreaterThan == ov._useGreaterThan) &&
132             (_usePrefixMatch == ov._usePrefixMatch)) {
133             return true;
134         }
135         }
136     }
137     return false;
138     }
139
140     /** Compares if two version IDs are equal */
141     private boolean matchTuple(Object JavaDoc o) {
142     // Check for null and type
143
if (o == null || !(o instanceof VersionID)) return false;
144     VersionID vid = (VersionID)o;
145     
146     // Normalize arrays
147
String JavaDoc[] t1 = normalize(_tuple, vid._tuple.length);
148     String JavaDoc[] t2 = normalize(vid._tuple, _tuple.length);
149     
150     // Check contents
151
for(int i = 0; i < t1.length; i++) {
152         Object JavaDoc o1 = getValueAsObject(t1[i]);
153         Object JavaDoc o2 = getValueAsObject(t2[i]);
154         if (!o1.equals(o2)) return false;
155     }
156     return true;
157     }
158     
159     private Object JavaDoc getValueAsObject(String JavaDoc value) {
160     if (value.length() > 0 && value.charAt(0) != '-') {
161         try { return Integer.valueOf(value);
162         } catch(NumberFormatException JavaDoc nfe) { /* fall through */ }
163     }
164     return value;
165     }
166     
167     public boolean isGreaterThan(VersionID vid) {
168     return isGreaterThanOrEqualHelper(vid, false);
169     }
170     
171     public boolean isGreaterThanOrEqual(VersionID vid) {
172     return isGreaterThanOrEqualHelper(vid, true);
173     }
174     
175     /** Compares if 'this' is greater than vid */
176     private boolean isGreaterThanOrEqualHelper(VersionID vid,
177     boolean allowEqual) {
178
179     if (_isCompound) {
180         if (!_rest.isGreaterThanOrEqualHelper(vid, allowEqual)) {
181         return false;
182         }
183     }
184     // Normalize the two strings
185
String JavaDoc[] t1 = normalize(_tuple, vid._tuple.length);
186     String JavaDoc[] t2 = normalize(vid._tuple, _tuple.length);
187     
188     for(int i = 0; i < t1.length; i++) {
189         // Compare current element
190
Object JavaDoc e1 = getValueAsObject(t1[i]);
191         Object JavaDoc e2 = getValueAsObject(t2[i]);
192         if (e1.equals(e2)) {
193         // So far so good
194
} else {
195         if (e1 instanceof Integer JavaDoc && e2 instanceof Integer JavaDoc) {
196             return ((Integer JavaDoc)e1).intValue() > ((Integer JavaDoc)e2).intValue();
197         } else {
198             String JavaDoc s1 = t1[i].toString();
199             String JavaDoc s2 = t2[i].toString();
200             return s1.compareTo(s2) > 0;
201         }
202         
203         }
204     }
205     // If we get here, they are equal
206
return allowEqual;
207     }
208     
209     /** Checks if 'this' is a prefix of vid */
210     public boolean isPrefixMatch(VersionID vid) {
211
212     if (_isCompound) {
213         if (!_rest.isPrefixMatch(vid)) {
214         return false;
215         }
216     }
217     // Make sure that vid is at least as long as the prefix
218
String JavaDoc[] t2 = normalize(vid._tuple, _tuple.length);
219     
220     for(int i = 0; i < _tuple.length; i++) {
221         Object JavaDoc e1 = _tuple[i];
222         Object JavaDoc e2 = t2[i];
223         if (e1.equals(e2)) {
224         // So far so good
225
} else {
226         // Not a prefix
227
return false;
228         }
229     }
230     return true;
231     }
232     
233     /** Normalize an array to a certain lengh */
234     private String JavaDoc[] normalize(String JavaDoc[] list, int minlength) {
235     if (list.length < minlength) {
236         // Need to do padding
237
String JavaDoc[] newlist = new String JavaDoc[minlength];
238         System.arraycopy(list, 0, newlist, 0, list.length);
239         Arrays.fill(newlist, list.length, newlist.length, "0");
240         return newlist;
241     } else {
242         return list;
243     }
244     }
245     
246     public int compareTo(Object JavaDoc o) {
247     if (o == null || !(o instanceof VersionID)) return -1;
248     VersionID vid = (VersionID)o;
249     return equals(vid) ? 0 : (isGreaterThanOrEqual(vid) ? 1 : -1);
250     }
251     /** Show it as a string */
252     public String JavaDoc toString() {
253     StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
254     for(int i = 0; i < _tuple.length -1; i++) {
255         sb.append(_tuple[i]);
256         sb.append('.');
257     }
258     if (_tuple.length > 0 ) sb.append(_tuple[_tuple.length - 1]);
259     if (_usePrefixMatch) sb.append('+');
260     return sb.toString();
261     }
262 }
263
264
Popular Tags