You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

DeweyDecimal.java 8.0 kB

11 years ago
10 years ago
11 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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. import java.util.StringTokenizer;
  20. /**
  21. * Utility class to contain version numbers in "Dewey Decimal"
  22. * syntax. Numbers in the "Dewey Decimal" syntax consist of positive
  23. * decimal integers separated by periods ".". For example, "2.0" or
  24. * "1.2.3.4.5.6.7". This allows an extensible number to be used to
  25. * represent major, minor, micro, etc versions. The version number
  26. * must begin with a number.
  27. *
  28. */
  29. public class DeweyDecimal implements Comparable<DeweyDecimal> {
  30. /** Array of components that make up DeweyDecimal */
  31. private final int[] components;
  32. /**
  33. * Construct a DeweyDecimal from an array of integer components.
  34. *
  35. * @param components an array of integer components.
  36. */
  37. public DeweyDecimal(final int[] components) {
  38. this.components = new int[components.length];
  39. System.arraycopy(components, 0, this.components, 0, components.length);
  40. }
  41. /**
  42. * Construct a DeweyDecimal from string in DeweyDecimal format.
  43. *
  44. * @param string the string in dewey decimal format
  45. * @exception NumberFormatException if string is malformed
  46. */
  47. public DeweyDecimal(final String string)
  48. throws NumberFormatException {
  49. final StringTokenizer tokenizer = new StringTokenizer(string, ".", true);
  50. final int size = tokenizer.countTokens();
  51. components = new int[ (size + 1) / 2 ];
  52. for (int i = 0; i < components.length; i++) {
  53. final String component = tokenizer.nextToken();
  54. if (component.length() == 0) {
  55. throw new NumberFormatException("Empty component in string");
  56. }
  57. components[ i ] = Integer.parseInt(component);
  58. //Strip '.' token
  59. if (tokenizer.hasMoreTokens()) {
  60. tokenizer.nextToken();
  61. //If it ended in a dot, throw an exception
  62. if (!tokenizer.hasMoreTokens()) {
  63. throw new NumberFormatException("DeweyDecimal ended in a '.'");
  64. }
  65. }
  66. }
  67. }
  68. /**
  69. * Return number of components in <code>DeweyDecimal</code>.
  70. *
  71. * @return the number of components in dewey decimal
  72. */
  73. public int getSize() {
  74. return components.length;
  75. }
  76. /**
  77. * Return the component at specified index.
  78. *
  79. * @param index the index of components
  80. * @return the value of component at index
  81. */
  82. public int get(final int index) {
  83. return components[ index ];
  84. }
  85. /**
  86. * Return <code>true</code> if this <code>DeweyDecimal</code> is
  87. * equal to the other <code>DeweyDecimal</code>.
  88. *
  89. * @param other the other DeweyDecimal
  90. * @return true if equal to other DeweyDecimal, false otherwise
  91. */
  92. public boolean isEqual(final DeweyDecimal other) {
  93. final int max = Math.max(other.components.length, components.length);
  94. for (int i = 0; i < max; i++) {
  95. final int component1 = (i < components.length) ? components[ i ] : 0;
  96. final int component2 = (i < other.components.length) ? other.components[ i ] : 0;
  97. if (component2 != component1) {
  98. return false;
  99. }
  100. }
  101. return true; // Exact match
  102. }
  103. /**
  104. * Return <code>true</code> if this <code>DeweyDecimal</code> is
  105. * less than the other <code>DeweyDecimal</code>.
  106. *
  107. * @param other the other DeweyDecimal
  108. * @return true if less than other DeweyDecimal, false otherwise
  109. */
  110. public boolean isLessThan(final DeweyDecimal other) {
  111. return !isGreaterThanOrEqual(other);
  112. }
  113. /**
  114. * Return <code>true</code> if this <code>DeweyDecimal</code> is
  115. * less than or equal to the other <code>DeweyDecimal</code>.
  116. *
  117. * @param other the other DeweyDecimal
  118. * @return true if less than or equal to other DeweyDecimal, false otherwise
  119. */
  120. public boolean isLessThanOrEqual(final DeweyDecimal other) {
  121. return !isGreaterThan(other);
  122. }
  123. /**
  124. * Return <code>true</code> if this <code>DeweyDecimal</code> is
  125. * greater than the other <code>DeweyDecimal</code>.
  126. *
  127. * @param other the other DeweyDecimal
  128. * @return true if greater than other DeweyDecimal, false otherwise
  129. */
  130. public boolean isGreaterThan(final DeweyDecimal other) {
  131. final int max = Math.max(other.components.length, components.length);
  132. for (int i = 0; i < max; i++) {
  133. final int component1 = (i < components.length) ? components[ i ] : 0;
  134. final int component2 = (i < other.components.length) ? other.components[ i ] : 0;
  135. if (component2 > component1) {
  136. return false;
  137. }
  138. if (component2 < component1) {
  139. return true;
  140. }
  141. }
  142. return false; // Exact match
  143. }
  144. /**
  145. * Return <code>true</code> if this <code>DeweyDecimal</code> is
  146. * greater than or equal to the other <code>DeweyDecimal</code>.
  147. *
  148. * @param other the other DeweyDecimal
  149. * @return true if greater than or equal to other DeweyDecimal, false otherwise
  150. */
  151. public boolean isGreaterThanOrEqual(final DeweyDecimal other) {
  152. final int max = Math.max(other.components.length, components.length);
  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. if (component2 > component1) {
  157. return false;
  158. }
  159. if (component2 < component1) {
  160. return true;
  161. }
  162. }
  163. return true; // Exact match
  164. }
  165. /**
  166. * Return string representation of <code>DeweyDecimal</code>.
  167. *
  168. * @return the string representation of DeweyDecimal.
  169. */
  170. @Override public String toString() {
  171. final StringBuffer sb = new StringBuffer();
  172. for (int i = 0; i < components.length; i++) {
  173. if (i != 0) {
  174. sb.append('.');
  175. }
  176. sb.append(components[ i ]);
  177. }
  178. return sb.toString();
  179. }
  180. /**
  181. * Compares this DeweyDecimal with another one.
  182. *
  183. * @param other another DeweyDecimal to compare with
  184. * @return result
  185. * @see java.lang.Comparable#compareTo(Object)
  186. */
  187. public int compareTo(DeweyDecimal other) {
  188. final int max = Math.max(other.components.length, components.length);
  189. for (int i = 0; i < max; i++) {
  190. final int component1 = (i < components.length) ? components[ i ] : 0;
  191. final int component2 = (i < other.components.length) ? other.components[ i ] : 0;
  192. if (component1 != component2) {
  193. return component1 - component2;
  194. }
  195. }
  196. return 0;
  197. }
  198. @Override public int hashCode() {
  199. return toString().hashCode();
  200. }
  201. @Override public boolean equals(Object o) {
  202. return o instanceof DeweyDecimal && isEqual((DeweyDecimal) o);
  203. }
  204. }