KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openide > modules > SpecificationVersion


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19 package org.openide.modules;
20
21
22 // THIS CLASS OUGHT NOT USE NbBundle NOR org.openide CLASSES
23
// OUTSIDE OF openide-util.jar! UI AND FILESYSTEM/DATASYSTEM
24
// INTERACTIONS SHOULD GO ELSEWHERE.
25
import java.util.*;
26
27
28 /** Utility class representing a specification version.
29  * @author Jesse Glick
30  * @since 1.24
31  */

32 public final class SpecificationVersion implements Comparable JavaDoc {
33     // Might be a bit wasteful of memory, but many SV's are created during
34
// startup, so best to not have to reparse them each time!
35
// In fact sharing the int arrays might save a bit of memory overall,
36
// since it is unusual for a module to be deleted.
37
private static final Map<String JavaDoc,int[]> parseCache = new HashMap<String JavaDoc,int[]>(200);
38     private final int[] digits;
39
40     /** Parse from string. Must be Dewey-decimal. */
41     public SpecificationVersion(String JavaDoc version) throws NumberFormatException JavaDoc {
42         synchronized (parseCache) {
43             int[] d = (int[]) parseCache.get(version);
44
45             if (d == null) {
46                 d = parse(version);
47                 parseCache.put(version.intern(), d);
48             }
49
50             digits = d;
51         }
52     }
53
54     private static int[] parse(String JavaDoc version) throws NumberFormatException JavaDoc {
55         StringTokenizer tok = new StringTokenizer(version, ".", true); // NOI18N
56

57         int len = tok.countTokens();
58         if ((len % 2) == 0) {
59             throw new NumberFormatException JavaDoc("Even number of pieces in a spec version: `" + version + "'"); // NOI18N
60
}
61         int[] digits = new int[len / 2 + 1];
62         int i = 0;
63
64         boolean expectingNumber = true;
65
66         while (tok.hasMoreTokens()) {
67             if (expectingNumber) {
68                 expectingNumber = false;
69
70                 int piece = Integer.parseInt(tok.nextToken());
71
72                 if (piece < 0) {
73                     throw new NumberFormatException JavaDoc("Spec version component <0: " + piece); // NOI18N
74
}
75
76                 digits[i++] = piece;
77             } else {
78                 if (!".".equals(tok.nextToken())) { // NOI18N
79
throw new NumberFormatException JavaDoc("Expected dot in spec version: `" + version + "'"); // NOI18N
80
}
81
82                 expectingNumber = true;
83             }
84         }
85         return digits;
86     }
87
88     /** Perform a Dewey-decimal comparison. */
89     public int compareTo(Object JavaDoc o) {
90         int[] od = ((SpecificationVersion) o).digits;
91         int len1 = digits.length;
92         int len2 = od.length;
93         int max = Math.max(len1, len2);
94
95         for (int i = 0; i < max; i++) {
96             int d1 = ((i < len1) ? digits[i] : 0);
97             int d2 = ((i < len2) ? od[i] : 0);
98
99             if (d1 != d2) {
100                 return d1 - d2;
101             }
102         }
103
104         return 0;
105     }
106
107     /** Overridden to compare contents. */
108     public boolean equals(Object JavaDoc o) {
109         if (!(o instanceof SpecificationVersion)) {
110             return false;
111         }
112
113         return Arrays.equals(digits, ((SpecificationVersion) o).digits);
114     }
115
116     /** Overridden to hash by contents. */
117     public int hashCode() {
118         int hash = 925295;
119         int len = digits.length;
120
121         for (int i = 0; i < len; i++) {
122             hash ^= (digits[i] << i);
123         }
124
125         return hash;
126     }
127
128     /** String representation (Dewey-decimal). */
129     public String JavaDoc toString() {
130         StringBuilder JavaDoc buf = new StringBuilder JavaDoc((digits.length * 3) + 1);
131
132         for (int i = 0; i < digits.length; i++) {
133             if (i > 0) {
134                 buf.append('.'); // NOI18N
135
}
136
137             buf.append(digits[i]);
138         }
139
140         return buf.toString();
141     }
142 }
143
Popular Tags