KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tools > ant > util > DeweyDecimal


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  */

18 package org.apache.tools.ant.util;
19
20 import java.util.StringTokenizer JavaDoc;
21
22 /**
23  * Utility class to contain version numbers in "Dewey Decimal"
24  * syntax. Numbers in the "Dewey Decimal" syntax consist of positive
25  * decimal integers separated by periods ".". For example, "2.0" or
26  * "1.2.3.4.5.6.7". This allows an extensible number to be used to
27  * represent major, minor, micro, etc versions. The version number
28  * must begin with a number.
29  *
30  */

31 public class DeweyDecimal {
32
33     /** Array of components that make up DeweyDecimal */
34     private int[] components;
35
36     /**
37      * Construct a DeweyDecimal from an array of integer components.
38      *
39      * @param components an array of integer components.
40      */

41     public DeweyDecimal(final int[] components) {
42         this.components = new int[components.length];
43
44         for (int i = 0; i < components.length; i++) {
45             this.components[i] = components[i];
46         }
47     }
48
49     /**
50      * Construct a DeweyDecimal from string in DeweyDecimal format.
51      *
52      * @param string the string in dewey decimal format
53      * @exception NumberFormatException if string is malformed
54      */

55     public DeweyDecimal(final String JavaDoc string)
56         throws NumberFormatException JavaDoc {
57         final StringTokenizer tokenizer = new StringTokenizer(string, ".", true);
58         final int size = tokenizer.countTokens();
59
60         components = new int[ (size + 1) / 2 ];
61
62         for (int i = 0; i < components.length; i++) {
63             final String JavaDoc component = tokenizer.nextToken();
64             if (component.equals("")) {
65                 throw new NumberFormatException JavaDoc("Empty component in string");
66             }
67
68             components[ i ] = Integer.parseInt(component);
69
70             //Strip '.' token
71
if (tokenizer.hasMoreTokens()) {
72                 tokenizer.nextToken();
73
74                 //If it ended in a dot, throw an exception
75
if (!tokenizer.hasMoreTokens()) {
76                     throw new NumberFormatException JavaDoc("DeweyDecimal ended in a '.'");
77                 }
78             }
79         }
80     }
81
82     /**
83      * Return number of components in <code>DeweyDecimal</code>.
84      *
85      * @return the number of components in dewey decimal
86      */

87     public int getSize() {
88         return components.length;
89     }
90
91     /**
92      * Return the component at specified index.
93      *
94      * @param index the index of components
95      * @return the value of component at index
96      */

97     public int get(final int index) {
98         return components[ index ];
99     }
100
101     /**
102      * Return <code>true</code> if this <code>DeweyDecimal</code> is
103      * equal to the other <code>DeweyDecimal</code>.
104      *
105      * @param other the other DeweyDecimal
106      * @return true if equal to other DeweyDecimal, false otherwise
107      */

108     public boolean isEqual(final DeweyDecimal other) {
109         final int max = Math.max(other.components.length, components.length);
110
111         for (int i = 0; i < max; i++) {
112             final int component1 = (i < components.length) ? components[ i ] : 0;
113             final int component2 = (i < other.components.length) ? other.components[ i ] : 0;
114
115             if (component2 != component1) {
116                 return false;
117             }
118         }
119
120         return true; // Exact match
121
}
122
123     /**
124      * Return <code>true</code> if this <code>DeweyDecimal</code> is
125      * less than the other <code>DeweyDecimal</code>.
126      *
127      * @param other the other DeweyDecimal
128      * @return true if less than other DeweyDecimal, false otherwise
129      */

130     public boolean isLessThan(final DeweyDecimal other) {
131         return !isGreaterThanOrEqual(other);
132     }
133
134     /**
135      * Return <code>true</code> if this <code>DeweyDecimal</code> is
136      * less than or equal to the other <code>DeweyDecimal</code>.
137      *
138      * @param other the other DeweyDecimal
139      * @return true if less than or equal to other DeweyDecimal, false otherwise
140      */

141     public boolean isLessThanOrEqual(final DeweyDecimal other) {
142         return !isGreaterThan(other);
143     }
144
145     /**
146      * Return <code>true</code> if this <code>DeweyDecimal</code> is
147      * greater than the other <code>DeweyDecimal</code>.
148      *
149      * @param other the other DeweyDecimal
150      * @return true if greater than other DeweyDecimal, false otherwise
151      */

152     public boolean isGreaterThan(final DeweyDecimal other) {
153         final int max = Math.max(other.components.length, components.length);
154
155         for (int i = 0; i < max; i++) {
156             final int component1 = (i < components.length) ? components[ i ] : 0;
157             final int component2 = (i < other.components.length) ? other.components[ i ] : 0;
158
159             if (component2 > component1) {
160                 return false;
161             }
162             if (component2 < component1) {
163                 return true;
164             }
165         }
166
167         return false; // Exact match
168
}
169
170     /**
171      * Return <code>true</code> if this <code>DeweyDecimal</code> is
172      * greater than or equal to the other <code>DeweyDecimal</code>.
173      *
174      * @param other the other DeweyDecimal
175      * @return true if greater than or equal to other DeweyDecimal, false otherwise
176      */

177     public boolean isGreaterThanOrEqual(final DeweyDecimal other) {
178         final int max = Math.max(other.components.length, components.length);
179
180         for (int i = 0; i < max; i++) {
181             final int component1 = (i < components.length) ? components[ i ] : 0;
182             final int component2 = (i < other.components.length) ? other.components[ i ] : 0;
183
184             if (component2 > component1) {
185                 return false;
186             }
187             if (component2 < component1) {
188                 return true;
189             }
190         }
191
192         return true; // Exact match
193
}
194
195     /**
196      * Return string representation of <code>DeweyDecimal</code>.
197      *
198      * @return the string representation of DeweyDecimal.
199      */

200     public String JavaDoc toString() {
201         final StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
202
203         for (int i = 0; i < components.length; i++) {
204             if (i != 0) {
205                 sb.append('.');
206             }
207             sb.append(components[ i ]);
208         }
209
210         return sb.toString();
211     }
212 }
213
Popular Tags