ICU-6824 Clean up the Collection Utilities. Now only one file of that name... Also needed to add a couple of small items to UnicodeSet.

X-SVN-Rev: 26397
This commit is contained in:
Mark Davis 2009-07-20 23:05:23 +00:00
parent a06021f572
commit c361c76465
6 changed files with 78 additions and 73 deletions

View file

@ -18,7 +18,7 @@ import java.util.Map;
import java.util.Set;
import java.util.TreeSet;
import com.ibm.icu.impl.CollectionUtilities.MultiComparator;
import com.ibm.icu.impl.MultiComparator;
import com.ibm.icu.lang.UCharacter;
import com.ibm.icu.util.LocaleData;
import com.ibm.icu.util.ULocale;

View file

@ -1,43 +0,0 @@
/*
*******************************************************************************
* Copyright (C) 2008-2009, Google Inc, International Business Machines *
* Corporation and others. All Rights Reserved. *
*******************************************************************************
*/
package com.ibm.icu.impl;
import java.util.Comparator;
/**
* @author markdavis
*
*/
public class CollectionUtilities {
public static class MultiComparator<T> implements Comparator<T> {
private Comparator<T>[] comparators;
public MultiComparator (Comparator<T>[] comparators) {
this.comparators = comparators;
}
/* Lexigraphic compare. Returns the first difference
* @return zero if equal. Otherwise +/- (i+1)
* where i is the index of the first comparator finding a difference
* @see java.util.Comparator#compare(java.lang.Object, java.lang.Object)
*/
public int compare(T arg0, T arg1) {
for (int i = 0; i < comparators.length; ++i) {
int result = comparators[i].compare(arg0, arg1);
if (result == 0) {
continue;
}
if (result > 0) {
return i + 1;
}
return -(i + 1);
}
return 0;
}
}
}

View file

@ -0,0 +1,36 @@
/*
*******************************************************************************
* Copyright (C) 2009, International Business Machines Corporation and *
* others. All Rights Reserved. *
*******************************************************************************
*/
package com.ibm.icu.impl;
import java.util.Comparator;
public class MultiComparator<T> implements Comparator<T> {
private Comparator<T>[] comparators;
public MultiComparator (Comparator<T>... comparators) {
this.comparators = comparators;
}
/* Lexigraphic compare. Returns the first difference
* @return zero if equal. Otherwise +/- (i+1)
* where i is the index of the first comparator finding a difference
* @see java.util.Comparator#compare(java.lang.Object, java.lang.Object)
*/
public int compare(T arg0, T arg1) {
for (int i = 0; i < comparators.length; ++i) {
int result = comparators[i].compare(arg0, arg1);
if (result == 0) {
continue;
}
if (result > 0) {
return i + 1;
}
return -(i + 1);
}
return 0;
}
}

View file

@ -2714,11 +2714,30 @@ public class UnicodeSet extends UnicodeFilter implements Iterable<String>, Compa
* Add the contents of the UnicodeSet (as strings) into a collection.
* @param target collection to add into
* @return
* @stable ICU 2.8
* @draft ICU 4.4
*/
public <U extends Collection<String>> U addAllTo(U target) {
return addAllTo(this, target);
}
/**
* Add the contents of the UnicodeSet (as strings) into a collection.
* @param target collection to add into
* @return
* @draft ICU 4.4
*/
public String[] addAllTo(String[] target) {
return addAllTo(this, target);
}
/**
* Add the contents of the UnicodeSet (as strings) into an array.
* @draft ICU 4.4
*/
public static String[] toArray(UnicodeSet set) {
return addAllTo(set, new String[set.size()]);
}
/**
* Add the contents of the collection (as strings) into this UnicodeSet.
@ -4110,6 +4129,18 @@ public class UnicodeSet extends UnicodeFilter implements Iterable<String>, Compa
}
return target;
}
/**
* Utility for adding the contents of an iterable to a collection.
* @draft ICU 4.2
*/
public static <T> T[] addAllTo(Iterable<T> source, T[] target) {
int i = 0;
for (T item : source) {
target[i++] = item;
}
return target;
}
/**
* For iterating through the strings in the set. Example:

View file

@ -367,29 +367,6 @@ public final class CollectionUtilities {
}
}
public static class MultiComparator implements Comparator {
private Comparator[] comparators;
public MultiComparator (Comparator[] comparators) {
this.comparators = comparators;
}
/* Lexigraphic compare. Returns the first difference
* @return zero if equal. Otherwise +/- (i+1)
* where i is the index of the first comparator finding a difference
* @see java.util.Comparator#compare(java.lang.Object, java.lang.Object)
*/
public int compare(Object arg0, Object arg1) {
for (int i = 0; i < comparators.length; ++i) {
int result = comparators[i].compare(arg0, arg1);
if (result == 0) continue;
if (result > 0) return i+1;
return -(i+1);
}
return 0;
}
}
/**
* Modifies Unicode set to flatten the strings. Eg [abc{da}] => [abcd]
* Returns the set for chaining.
@ -469,4 +446,5 @@ public final class CollectionUtilities {
return matcher.reset((String)item).matches();
}
}
}

View file

@ -10,6 +10,7 @@ package com.ibm.icu.dev.test.util;
import java.util.Comparator;
import java.util.TreeSet;
import com.ibm.icu.impl.MultiComparator;
import com.ibm.icu.impl.Utility;
import com.ibm.icu.lang.UCharacter;
import com.ibm.icu.text.StringTransform;
@ -46,7 +47,6 @@ public class PrettyPrinter {
}
public PrettyPrinter setQuoter(StringTransform quoter) {
this.quoter = quoter;
return this; // for chaining
}
@ -72,7 +72,7 @@ public class PrettyPrinter {
* @return
*/
public PrettyPrinter setOrdering(Comparator ordering) {
this.ordering = ordering == null ? CODEPOINT_ORDER : new CollectionUtilities.MultiComparator(new Comparator[] {ordering, CODEPOINT_ORDER});
this.ordering = ordering == null ? CODEPOINT_ORDER : new com.ibm.icu.impl.MultiComparator(new Comparator[] {ordering, CODEPOINT_ORDER});
return this;
}
@ -98,12 +98,15 @@ public class PrettyPrinter {
* @param toQuote
*/
public PrettyPrinter setToQuote(UnicodeSet toQuote) {
toQuote = (UnicodeSet)toQuote.clone();
toQuote.addAll(PATTERN_WHITESPACE);
this.toQuote = toQuote;
if (toQuote != null) {
toQuote = (UnicodeSet)toQuote.clone();
toQuote.addAll(PATTERN_WHITESPACE);
this.toQuote = toQuote;
}
return this;
}
/**
* Get the pattern for a particular set.
* @param uset