source: other-projects/trunk/gs3-release-maker/apache-ant-1.6.5/src/main/org/apache/tools/ant/taskdefs/optional/extension/DeweyDecimal.java@ 14627

Last change on this file since 14627 was 14627, checked in by oranfry, 17 years ago

initial import of the gs3-release-maker

File size: 6.9 KB
Line 
1/*
2 * Copyright 2002,2004-2005 The Apache Software Foundation
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 *
16 */
17package org.apache.tools.ant.taskdefs.optional.extension;
18
19import java.util.StringTokenizer;
20
21/**
22 * Utility class to contain version numbers in "Dewey Decimal"
23 * syntax. Numbers in the "Dewey Decimal" syntax consist of positive
24 * decimal integers separated by periods ".". For example, "2.0" or
25 * "1.2.3.4.5.6.7". This allows an extensible number to be used to
26 * represent major, minor, micro, etc versions. The version number
27 * must begin with a number.
28 *
29 */
30public final class DeweyDecimal {
31 /** Array of components that make up DeweyDecimal */
32 private int[] components;
33
34 /**
35 * Construct a DeweyDecimal from an array of integer components.
36 *
37 * @param components an array of integer components.
38 */
39 public DeweyDecimal(final int[] components) {
40 this.components = new int[components.length];
41
42 for (int i = 0; i < components.length; i++) {
43 this.components[i] = components[i];
44 }
45 }
46
47 /**
48 * Construct a DeweyDecimal from string in DeweyDecimal format.
49 *
50 * @param string the string in dewey decimal format
51 * @exception NumberFormatException if string is malformed
52 */
53 public DeweyDecimal(final String string)
54 throws NumberFormatException {
55 final StringTokenizer tokenizer = new StringTokenizer(string, ".", true);
56 final int size = tokenizer.countTokens();
57
58 components = new int[ (size + 1) / 2 ];
59
60 for (int i = 0; i < components.length; i++) {
61 final String component = tokenizer.nextToken();
62 if (component.equals("")) {
63 throw new NumberFormatException("Empty component in string");
64 }
65
66 components[ i ] = Integer.parseInt(component);
67
68 //Strip '.' token
69 if (tokenizer.hasMoreTokens()) {
70 tokenizer.nextToken();
71
72 //If it ended in a dot, throw an exception
73 if (!tokenizer.hasMoreTokens()) {
74 throw new NumberFormatException("DeweyDecimal ended in a '.'");
75 }
76 }
77 }
78 }
79
80 /**
81 * Return number of components in <code>DeweyDecimal</code>.
82 *
83 * @return the number of components in dewey decimal
84 */
85 public int getSize() {
86 return components.length;
87 }
88
89 /**
90 * Return the component at specified index.
91 *
92 * @param index the index of components
93 * @return the value of component at index
94 */
95 public int get(final int index) {
96 return components[ index ];
97 }
98
99 /**
100 * Return <code>true</code> if this <code>DeweyDecimal</code> is
101 * equal to the other <code>DeweyDecimal</code>.
102 *
103 * @param other the other DeweyDecimal
104 * @return true if equal to other DeweyDecimal, false otherwise
105 */
106 public boolean isEqual(final DeweyDecimal other) {
107 final int max = Math.max(other.components.length, components.length);
108
109 for (int i = 0; i < max; i++) {
110 final int component1 = (i < components.length) ? components[ i ] : 0;
111 final int component2 = (i < other.components.length) ? other.components[ i ] : 0;
112
113 if (component2 != component1) {
114 return false;
115 }
116 }
117
118 return true; // Exact match
119 }
120
121 /**
122 * Return <code>true</code> if this <code>DeweyDecimal</code> is
123 * less than the other <code>DeweyDecimal</code>.
124 *
125 * @param other the other DeweyDecimal
126 * @return true if less than other DeweyDecimal, false otherwise
127 */
128 public boolean isLessThan(final DeweyDecimal other) {
129 return !isGreaterThanOrEqual(other);
130 }
131
132 /**
133 * Return <code>true</code> if this <code>DeweyDecimal</code> is
134 * less than or equal to the other <code>DeweyDecimal</code>.
135 *
136 * @param other the other DeweyDecimal
137 * @return true if less than or equal to other DeweyDecimal, false otherwise
138 */
139 public boolean isLessThanOrEqual(final DeweyDecimal other) {
140 return !isGreaterThan(other);
141 }
142
143 /**
144 * Return <code>true</code> if this <code>DeweyDecimal</code> is
145 * greater than the other <code>DeweyDecimal</code>.
146 *
147 * @param other the other DeweyDecimal
148 * @return true if greater than other DeweyDecimal, false otherwise
149 */
150 public boolean isGreaterThan(final DeweyDecimal other) {
151 final int max = Math.max(other.components.length, components.length);
152
153 for (int i = 0; i < max; i++) {
154 final int component1 = (i < components.length) ? components[ i ] : 0;
155 final int component2 = (i < other.components.length) ? other.components[ i ] : 0;
156
157 if (component2 > component1) {
158 return false;
159 }
160 if (component2 < component1) {
161 return true;
162 }
163 }
164
165 return false; // Exact match
166 }
167
168 /**
169 * Return <code>true</code> if this <code>DeweyDecimal</code> is
170 * greater than or equal to the other <code>DeweyDecimal</code>.
171 *
172 * @param other the other DeweyDecimal
173 * @return true if greater than or equal to other DeweyDecimal, false otherwise
174 */
175 public boolean isGreaterThanOrEqual(final DeweyDecimal other) {
176 final int max = Math.max(other.components.length, components.length);
177
178 for (int i = 0; i < max; i++) {
179 final int component1 = (i < components.length) ? components[ i ] : 0;
180 final int component2 = (i < other.components.length) ? other.components[ i ] : 0;
181
182 if (component2 > component1) {
183 return false;
184 }
185 if (component2 < component1) {
186 return true;
187 }
188 }
189
190 return true; // Exact match
191 }
192
193 /**
194 * Return string representation of <code>DeweyDecimal</code>.
195 *
196 * @return the string representation of DeweyDecimal.
197 */
198 public String toString() {
199 final StringBuffer sb = new StringBuffer();
200
201 for (int i = 0; i < components.length; i++) {
202 if (i != 0) {
203 sb.append('.');
204 }
205 sb.append(components[ i ]);
206 }
207
208 return sb.toString();
209 }
210}
Note: See TracBrowser for help on using the repository browser.