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 7.0 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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. import java.util.Arrays;
  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. */
  30. public class DeweyDecimal {
  31. /** Array of components that make up DeweyDecimal */
  32. private int[] components;
  33. /**
  34. * Construct a DeweyDecimal from an array of integer components.
  35. *
  36. * @param components an array of integer components.
  37. */
  38. public DeweyDecimal(final int[] components) {
  39. this.components = new int[components.length];
  40. System.arraycopy(components, 0, this.components, 0, components.length);
  41. }
  42. /**
  43. * Construct a DeweyDecimal from string in DeweyDecimal format.
  44. *
  45. * @param string the string in dewey decimal format
  46. * @exception NumberFormatException if string is malformed
  47. */
  48. public DeweyDecimal(final String string)
  49. throws NumberFormatException {
  50. final StringTokenizer tokenizer = new StringTokenizer(string, ".", true);
  51. final int size = tokenizer.countTokens();
  52. components = new int[ (size + 1) / 2 ];
  53. for (int i = 0; i < components.length; i++) {
  54. final String component = tokenizer.nextToken();
  55. if (component.equals("")) {
  56. throw new NumberFormatException("Empty component in string");
  57. }
  58. components[ i ] = Integer.parseInt(component);
  59. //Strip '.' token
  60. if (tokenizer.hasMoreTokens()) {
  61. tokenizer.nextToken();
  62. //If it ended in a dot, throw an exception
  63. if (!tokenizer.hasMoreTokens()) {
  64. throw new NumberFormatException("DeweyDecimal ended in a '.'");
  65. }
  66. }
  67. }
  68. }
  69. /**
  70. * Return number of components in <code>DeweyDecimal</code>.
  71. *
  72. * @return the number of components in dewey decimal
  73. */
  74. public int getSize() {
  75. return components.length;
  76. }
  77. /**
  78. * Return the component at specified index.
  79. *
  80. * @param index the index of components
  81. * @return the value of component at index
  82. */
  83. public int get(final int index) {
  84. return components[ index ];
  85. }
  86. /**
  87. * Return <code>true</code> if this <code>DeweyDecimal</code> is
  88. * equal to the other <code>DeweyDecimal</code>.
  89. *
  90. * @param other the other DeweyDecimal
  91. * @return true if equal to other DeweyDecimal, false otherwise
  92. */
  93. public boolean isEqual(final DeweyDecimal other) {
  94. final int max = Math.max(other.components.length, components.length);
  95. for (int i = 0; i < max; i++) {
  96. final int component1 = (i < components.length) ? components[ i ] : 0;
  97. final int component2 = (i < other.components.length) ? other.components[ i ] : 0;
  98. if (component2 != component1) {
  99. return false;
  100. }
  101. }
  102. return true; // Exact match
  103. }
  104. /**
  105. * Return <code>true</code> if this <code>DeweyDecimal</code> is
  106. * less than the other <code>DeweyDecimal</code>.
  107. *
  108. * @param other the other DeweyDecimal
  109. * @return true if less than other DeweyDecimal, false otherwise
  110. */
  111. public boolean isLessThan(final DeweyDecimal other) {
  112. return !isGreaterThanOrEqual(other);
  113. }
  114. /**
  115. * Return <code>true</code> if this <code>DeweyDecimal</code> is
  116. * less than or equal to the other <code>DeweyDecimal</code>.
  117. *
  118. * @param other the other DeweyDecimal
  119. * @return true if less than or equal to other DeweyDecimal, false otherwise
  120. */
  121. public boolean isLessThanOrEqual(final DeweyDecimal other) {
  122. return !isGreaterThan(other);
  123. }
  124. /**
  125. * Return <code>true</code> if this <code>DeweyDecimal</code> is
  126. * greater than the other <code>DeweyDecimal</code>.
  127. *
  128. * @param other the other DeweyDecimal
  129. * @return true if greater than other DeweyDecimal, false otherwise
  130. */
  131. public boolean isGreaterThan(final DeweyDecimal other) {
  132. final int max = Math.max(other.components.length, components.length);
  133. for (int i = 0; i < max; i++) {
  134. final int component1 = (i < components.length) ? components[ i ] : 0;
  135. final int component2 = (i < other.components.length) ? other.components[ i ] : 0;
  136. if (component2 > component1) {
  137. return false;
  138. }
  139. if (component2 < component1) {
  140. return true;
  141. }
  142. }
  143. return false; // Exact match
  144. }
  145. /**
  146. * Return <code>true</code> if this <code>DeweyDecimal</code> is
  147. * greater than or equal to the other <code>DeweyDecimal</code>.
  148. *
  149. * @param other the other DeweyDecimal
  150. * @return true if greater than or equal to other DeweyDecimal, false otherwise
  151. */
  152. public boolean isGreaterThanOrEqual(final DeweyDecimal other) {
  153. final int max = Math.max(other.components.length, components.length);
  154. for (int i = 0; i < max; i++) {
  155. final int component1 = (i < components.length) ? components[ i ] : 0;
  156. final int component2 = (i < other.components.length) ? other.components[ i ] : 0;
  157. if (component2 > component1) {
  158. return false;
  159. }
  160. if (component2 < component1) {
  161. return true;
  162. }
  163. }
  164. return true; // Exact match
  165. }
  166. /**
  167. * Return string representation of <code>DeweyDecimal</code>.
  168. *
  169. * @return the string representation of DeweyDecimal.
  170. */
  171. public String toString() {
  172. final StringBuffer sb = new StringBuffer();
  173. for (int i = 0; i < components.length; i++) {
  174. if (i != 0) {
  175. sb.append('.');
  176. }
  177. sb.append(components[ i ]);
  178. }
  179. return sb.toString();
  180. }
  181. }