1 18 package org.apache.tools.ant.util; 19 20 import java.util.StringTokenizer ; 21 22 31 public class DeweyDecimal { 32 33 34 private int[] components; 35 36 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 55 public DeweyDecimal(final String string) 56 throws NumberFormatException { 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 component = tokenizer.nextToken(); 64 if (component.equals("")) { 65 throw new NumberFormatException ("Empty component in string"); 66 } 67 68 components[ i ] = Integer.parseInt(component); 69 70 if (tokenizer.hasMoreTokens()) { 72 tokenizer.nextToken(); 73 74 if (!tokenizer.hasMoreTokens()) { 76 throw new NumberFormatException ("DeweyDecimal ended in a '.'"); 77 } 78 } 79 } 80 } 81 82 87 public int getSize() { 88 return components.length; 89 } 90 91 97 public int get(final int index) { 98 return components[ index ]; 99 } 100 101 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; } 122 123 130 public boolean isLessThan(final DeweyDecimal other) { 131 return !isGreaterThanOrEqual(other); 132 } 133 134 141 public boolean isLessThanOrEqual(final DeweyDecimal other) { 142 return !isGreaterThan(other); 143 } 144 145 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; } 169 170 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; } 194 195 200 public String toString() { 201 final StringBuffer sb = new StringBuffer (); 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 |