1 8 package com.ibm.icu.impl; 9 10 import java.util.ArrayList ; 11 import java.util.Collection ; 12 import java.util.Comparator ; 13 import java.util.HashMap ; 14 import java.util.Iterator ; 15 import java.util.List ; 16 import java.util.Map ; 17 import java.util.SortedSet ; 18 19 23 import com.ibm.icu.text.Transliterator; 24 import com.ibm.icu.text.UTF16; 25 import com.ibm.icu.text.UnicodeSet; 26 import com.ibm.icu.text.UnicodeSetIterator; 27 28 31 public final class CollectionUtilities { 32 33 public static String join(Object [] array, String separator) { 34 StringBuffer result = new StringBuffer (); 35 for (int i = 0; i < array.length; ++i) { 36 if (i != 0) result.append(separator); 37 result.append(array[i]); 38 } 39 return result.toString(); 40 } 41 42 public static String join(Collection collection, String separator) { 43 StringBuffer result = new StringBuffer (); 44 boolean first = true; 45 for (Iterator it = collection.iterator(); it.hasNext();) { 46 if (first) first = false; 47 else result.append(separator); 48 result.append(it.next()); 49 } 50 return result.toString(); 51 } 52 53 56 public static Map asMap(Object [][] source, Map target, boolean reverse) { 57 int from = 0, to = 1; 58 if (reverse) { 59 from = 1; to = 0; 60 } 61 for (int i = 0; i < source.length; ++i) { 62 target.put(source[i][from], source[i][to]); 63 } 64 return target; 65 } 66 67 public static Collection addAll(Iterator source, Collection target) { 68 while (source.hasNext()) { 69 target.add(source.next()); 70 } 71 return target; } 73 74 public static int size(Iterator source) { 75 int result = 0; 76 while (source.hasNext()) { 77 source.next(); 78 ++result; 79 } 80 return result; 81 } 82 83 84 public static Map asMap(Object [][] source) { 85 return asMap(source, new HashMap (), false); 86 } 87 88 91 public static Map removeAll(Map m, Collection itemsToRemove) { 92 for (Iterator it = itemsToRemove.iterator(); it.hasNext();) { 93 Object item = it.next(); 94 m.remove(item); 95 } 96 return m; 97 } 98 99 public Object getFirst(Collection c) { 100 Iterator it = c.iterator(); 101 if (!it.hasNext()) return null; 102 return it.next(); 103 } 104 105 public static Object getBest(Collection c, Comparator comp, int direction) { 106 Iterator it = c.iterator(); 107 if (!it.hasNext()) return null; 108 Object bestSoFar = it.next(); 109 if (direction < 0) { 110 while (it.hasNext()) { 111 Object item = it.next(); 112 int compValue = comp.compare(item, bestSoFar); 113 if (comp.compare(item, bestSoFar) < 0) { 114 bestSoFar = item; 115 } 116 } 117 } else { 118 while (it.hasNext()) { 119 Object item = it.next(); 120 int compValue = comp.compare(item, bestSoFar); 121 if (comp.compare(item, bestSoFar) > 0) { 122 bestSoFar = item; 123 } 124 } 125 } 126 return bestSoFar; 127 } 128 129 public interface ObjectMatcher { 130 133 boolean matches(Object o); 134 } 135 136 public static class InverseMatcher implements ObjectMatcher { 137 ObjectMatcher other; 138 public ObjectMatcher set(ObjectMatcher toInverse) { 139 other = toInverse; 140 return this; 141 } 142 public boolean matches(Object value) { 143 return !other.matches(value); 144 } 145 } 146 147 public static Collection removeAll(Collection c, ObjectMatcher f) { 148 for (Iterator it = c.iterator(); it.hasNext();) { 149 Object item = it.next(); 150 if (f.matches(item)) it.remove(); 151 } 152 return c; 153 } 154 155 public static Collection retainAll(Collection c, ObjectMatcher f) { 156 for (Iterator it = c.iterator(); it.hasNext();) { 157 Object item = it.next(); 158 if (!f.matches(item)) it.remove(); 159 } 160 return c; 161 } 162 163 public static boolean containsSome(Collection a, Collection b) { 164 if (a.size() == 0 || b.size() == 0) return false; 166 if (a == b) return true; 168 if (a instanceof SortedSet && b instanceof SortedSet ) { 169 SortedSet aa = (SortedSet ) a; 170 SortedSet bb = (SortedSet ) b; 171 aa.containsAll(null); 172 Comparator bbc = bb.comparator(); 173 Comparator aac = aa.comparator(); 174 if (bbc == null) { 175 if (aac == null) { 176 Iterator ai = aa.iterator(); 177 Iterator bi = bb.iterator(); 178 Comparable ao = (Comparable ) ai.next(); Comparable bo = (Comparable ) bi.next(); 180 while (true) { 181 int rel = ao.compareTo(bo); 182 if (rel < 0) { 183 if (!ai.hasNext()) return false; 184 ao = (Comparable ) ai.next(); 185 } else if (rel > 0) { 186 if (!bi.hasNext()) return false; 187 bo = (Comparable ) bi.next(); 188 } else { 189 return true; 190 } 191 } 192 } 193 } else if (bbc.equals(a)) { 194 Iterator ai = aa.iterator(); 195 Iterator bi = bb.iterator(); 196 Object ao = ai.next(); Object bo = bi.next(); 198 while (true) { 199 int rel = aac.compare(ao, bo); 200 if (rel < 0) { 201 if (!ai.hasNext()) return false; 202 ao = ai.next(); 203 } else if (rel > 0) { 204 if (!bi.hasNext()) return false; 205 bo = bi.next(); 206 } else { 207 return true; 208 } 209 } 210 } 211 } 212 for (Iterator it = a.iterator(); it.hasNext();) { 213 if (b.contains(it.next())) return true; 214 } 215 return false; 216 } 217 218 public static boolean containsAll(Collection a, Collection b) { 219 if (a == b) return true; 221 if (b.size() == 0) return true; 222 if (a.size() == 0) return false; 223 224 if (a instanceof SortedSet && b instanceof SortedSet ) { 225 SortedSet aa = (SortedSet ) a; 226 SortedSet bb = (SortedSet ) b; 227 Comparator bbc = bb.comparator(); 228 Comparator aac = aa.comparator(); 229 if (bbc == null) { 230 if (aac == null) { 231 Iterator ai = aa.iterator(); 232 Iterator bi = bb.iterator(); 233 Comparable ao = (Comparable ) ai.next(); Comparable bo = (Comparable ) bi.next(); 235 while (true) { 236 int rel = ao.compareTo(bo); 237 if (rel == 0) { 238 if (!bi.hasNext()) return true; 239 if (!ai.hasNext()) return false; 240 bo = (Comparable ) bi.next(); 241 ao = (Comparable ) ai.next(); 242 } else if (rel < 0) { 243 if (!ai.hasNext()) return false; 244 ao = (Comparable ) ai.next(); 245 } else { 246 return false; 247 } 248 } 249 } 250 } else if (bbc.equals(a)) { 251 Iterator ai = aa.iterator(); 252 Iterator bi = bb.iterator(); 253 Object ao = ai.next(); Object bo = bi.next(); 255 while (true) { 256 int rel = aac.compare(ao, bo); 257 if (rel == 0) { 258 if (!bi.hasNext()) return true; 259 if (!ai.hasNext()) return false; 260 bo = bi.next(); 261 ao = ai.next(); 262 } else if (rel < 0) { 263 if (!ai.hasNext()) return false; 264 ao = ai.next(); 265 } else { 266 return false; 267 } 268 } 269 } 270 } 271 return a.containsAll(b); 272 } 273 274 public static boolean containsNone(Collection a, Collection b) { 275 return !containsSome(a, b); 276 } 277 278 281 public static final int 282 ALL_EMPTY = 0, 283 NOT_A_SUPERSET_B = 1, 284 NOT_A_DISJOINT_B = 2, 285 NOT_A_SUBSET_B = 4, 286 NOT_A_EQUALS_B = NOT_A_SUBSET_B | NOT_A_SUPERSET_B, 287 A_PROPER_SUBSET_OF_B = NOT_A_DISJOINT_B | NOT_A_SUPERSET_B, 288 A_PROPER_SUPERSET_B = NOT_A_SUBSET_B | NOT_A_DISJOINT_B, 289 A_PROPER_OVERLAPS_B = NOT_A_SUBSET_B | NOT_A_DISJOINT_B | NOT_A_SUPERSET_B; 290 291 304 public static int getContainmentRelation(Collection a, Collection b) { 305 if (a.size() == 0) { 306 return (b.size() == 0) ? ALL_EMPTY : NOT_A_SUPERSET_B; 307 } else if (b.size() == 0) { 308 return NOT_A_SUBSET_B; 309 } 310 int result = 0; 311 for (Iterator it = a.iterator(); result != 6 && it.hasNext();) { 315 result |= (b.contains(it.next())) ? NOT_A_DISJOINT_B : NOT_A_SUBSET_B; 316 } 317 for (Iterator it = b.iterator(); (result & 3) != 3 && it.hasNext();) { 318 result |= (a.contains(it.next())) ? NOT_A_DISJOINT_B : NOT_A_SUPERSET_B; 319 } 320 return result; 321 } 322 323 public static String remove(String source, UnicodeSet removals) { 324 StringBuffer result = new StringBuffer (); 325 int cp; 326 for (int i = 0; i < source.length(); i += UTF16.getCharCount(cp)) { 327 cp = UTF16.charAt(source, i); 328 if (!removals.contains(cp)) UTF16.append(result, cp); 329 } 330 return result.toString(); 331 } 332 333 384 public static String prettyPrint(UnicodeSet uset, boolean compressRanges, UnicodeSet toQuote, Transliterator quoter, 385 Comparator ordering, Comparator spaceComparator) { 386 PrettyPrinter pp = new PrettyPrinter().setCompressRanges(compressRanges); 387 if (toQuote != null) pp.setToQuote(toQuote); 388 if (ordering != null) pp.setOrdering(ordering); 389 if (spaceComparator != null) pp.setSpaceComparator(spaceComparator); 390 return pp.toPattern(uset); 391 } 392 393 public static class MultiComparator implements Comparator { 394 private Comparator [] comparators; 395 396 public MultiComparator (Comparator [] comparators) { 397 this.comparators = comparators; 398 } 399 400 405 public int compare(Object arg0, Object arg1) { 406 for (int i = 0; i < comparators.length; ++i) { 407 int result = comparators[i].compare(arg0, arg1); 408 if (result == 0) continue; 409 if (result > 0) return i+1; 410 return -(i+1); 411 } 412 return 0; 413 } 414 } 415 416 422 public static UnicodeSet flatten(UnicodeSet exemplar1) { 423 UnicodeSet result = new UnicodeSet(); 424 boolean gotString = false; 425 for (UnicodeSetIterator it = new UnicodeSetIterator(exemplar1); it.nextRange();) { 426 if (it.codepoint == it.IS_STRING) { 427 result.addAll(it.string); 428 gotString = true; 429 } else { 430 result.add(it.codepoint, it.codepointEnd); 431 } 432 } 433 if (gotString) exemplar1.set(result); 434 return exemplar1; 435 } 436 437 440 public static abstract class FilteredIterator implements Iterator { 441 private Iterator baseIterator; 442 private static final Object EMPTY = new Object (); 443 private static final Object DONE = new Object (); 444 private Object nextObject = EMPTY; 445 public FilteredIterator set(Iterator baseIterator) { 446 this.baseIterator = baseIterator; 447 return this; 448 } 449 public void remove() { 450 throw new UnsupportedOperationException ("Doesn't support removal"); 451 } 452 public Object next() { 453 Object result = nextObject; 454 nextObject = EMPTY; 455 return result; 456 } 457 public boolean hasNext() { 458 if (nextObject == DONE) return false; 459 if (nextObject != EMPTY) return true; 460 while (baseIterator.hasNext()) { 461 nextObject = baseIterator.next(); 462 if (isIncluded(nextObject)) { 463 return true; 464 } 465 } 466 nextObject = DONE; 467 return false; 468 } 469 abstract public boolean isIncluded(Object item); 470 } 471 472 public static class PrefixIterator extends FilteredIterator { 473 private String prefix; 474 public PrefixIterator set(Iterator baseIterator, String prefix) { 475 super.set(baseIterator); 476 this.prefix = prefix; 477 return this; 478 } 479 public boolean isIncluded(Object item) { 480 return ((String )item).startsWith(prefix); 481 } 482 } 483 484 } 498 | Popular Tags |