From fb4d3e8b6505b74f177fe04d30719133d8d0bdc6 Mon Sep 17 00:00:00 2001 From: Markus Scherer Date: Mon, 25 Apr 2016 23:49:11 +0000 Subject: [PATCH] ICU-12450 move com.ibm.icu.dev.util.ArrayComparator, SortedBag, Tabber, VariableReplacer, Visitor, XEquivalenceClass, XEquivalenceMap to org.unicode.cldr.util X-SVN-Rev: 38649 --- .../com/ibm/icu/dev/util/ArrayComparator.java | 76 ---- .../src/com/ibm/icu/dev/util/SortedBag.java | 142 -------- .../src/com/ibm/icu/dev/util/Tabber.java | 239 ------------- .../ibm/icu/dev/util/VariableReplacer.java | 44 --- .../src/com/ibm/icu/dev/util/Visitor.java | 133 ------- .../ibm/icu/dev/util/XEquivalenceClass.java | 331 ------------------ .../com/ibm/icu/dev/util/XEquivalenceMap.java | 142 -------- 7 files changed, 1107 deletions(-) delete mode 100644 icu4j/main/tests/framework/src/com/ibm/icu/dev/util/ArrayComparator.java delete mode 100644 icu4j/main/tests/framework/src/com/ibm/icu/dev/util/SortedBag.java delete mode 100644 icu4j/main/tests/framework/src/com/ibm/icu/dev/util/Tabber.java delete mode 100644 icu4j/main/tests/framework/src/com/ibm/icu/dev/util/VariableReplacer.java delete mode 100644 icu4j/main/tests/framework/src/com/ibm/icu/dev/util/Visitor.java delete mode 100644 icu4j/main/tests/framework/src/com/ibm/icu/dev/util/XEquivalenceClass.java delete mode 100644 icu4j/main/tests/framework/src/com/ibm/icu/dev/util/XEquivalenceMap.java diff --git a/icu4j/main/tests/framework/src/com/ibm/icu/dev/util/ArrayComparator.java b/icu4j/main/tests/framework/src/com/ibm/icu/dev/util/ArrayComparator.java deleted file mode 100644 index 8127e81c379..00000000000 --- a/icu4j/main/tests/framework/src/com/ibm/icu/dev/util/ArrayComparator.java +++ /dev/null @@ -1,76 +0,0 @@ -/* - ******************************************************************************* - * Copyright (C) 2002-2012, International Business Machines Corporation and * - * others. All Rights Reserved. * - ******************************************************************************* - */ -package com.ibm.icu.dev.util; -import java.util.Comparator; - -public class ArrayComparator implements Comparator { - public static final Comparator COMPARABLE = new Comparator() { - public int compare(Object o1, Object o2) { - return ((Comparable)o1).compareTo(o2); - } - }; - private Comparator[] comparators; - private int[] reordering; - - public ArrayComparator (Comparator[] comparators, int[] reordering) { - this.comparators = comparators; - this.reordering = reordering; - if (this.reordering == null) { - this.reordering = new int[comparators.length]; - for (int i = 0; i < this.reordering.length; ++i) { - this.reordering[i] = i; - } - } else { - if (this.reordering.length != this.comparators.length) { - throw new IllegalArgumentException("comparator and reordering lengths must match"); - } - } - } - - public ArrayComparator (Comparator... comparators) { - this(comparators,null); - } - - /* 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 a0, Object a1) { - Object[] arg0 = (Object[]) a0; - Object[] arg1 = (Object[]) a1; - for (int j = 0; j < comparators.length; ++j) { - int i = reordering[j]; - Comparator comp = comparators[i]; - if (comp == null) continue; - int result = comp.compare(arg0[i], arg1[i]); - if (result == 0) continue; - if (result > 0) return i+1; - return -(i+1); - } - return 0; - } - - static class CatchExceptionComparator implements Comparator { - private Comparator other; - - public CatchExceptionComparator(Comparator other) { - this.other = other; - } - - public int compare(Object arg0, Object arg1) throws RuntimeException { - try { - return other.compare(arg0, arg1); - } catch (RuntimeException e) { - System.out.println("Arg0: " + arg0); - System.out.println("Arg1: " + arg1); - throw e; - } - } - } - -} \ No newline at end of file diff --git a/icu4j/main/tests/framework/src/com/ibm/icu/dev/util/SortedBag.java b/icu4j/main/tests/framework/src/com/ibm/icu/dev/util/SortedBag.java deleted file mode 100644 index 77ae52fb36d..00000000000 --- a/icu4j/main/tests/framework/src/com/ibm/icu/dev/util/SortedBag.java +++ /dev/null @@ -1,142 +0,0 @@ -/* -********************************************************************** -* Copyright (c) 2002-2012, International Business Machines -* Corporation and others. All Rights Reserved. -********************************************************************** -* Author: Mark Davis -********************************************************************** -*/package com.ibm.icu.dev.util; - -import java.util.Collection; -import java.util.Comparator; -import java.util.HashSet; -import java.util.Iterator; -import java.util.Map; -import java.util.Set; -import java.util.TreeMap; - -/** - * A collection that is like a sorted set, except that it allows - * multiple elements that compare as equal - * @author medavis - */ -// TODO replace use of Set with a collection that takes an Equator -public class SortedBag implements Collection { - /** - * A map of sets, where each corresponds to one sorted element. - * The sets are never empty - */ - private Map m; - private int size; - - public SortedBag(Comparator c) { - m = new TreeMap(c); - } - public boolean add(Object s) { - Set o = (Set)m.get(s); - if (o == null) { - o = new HashSet(1); - m.put(s,o); - } - boolean result = o.add(s); - if (result) size++; - return result; - } - public Iterator iterator() { - return new MyIterator(); - } - static Iterator EMPTY_ITERATOR = new HashSet().iterator(); - private class MyIterator implements Iterator { - private Iterator mapIterator = m.keySet().iterator(); - private Iterator setIterator = null; - - private MyIterator() { - mapIterator = m.keySet().iterator(); - setIterator = getSetIterator(); - } - private Iterator getSetIterator() { - if (!mapIterator.hasNext()) return EMPTY_ITERATOR; - return ((Set)m.get(mapIterator.next())).iterator(); - } - public boolean hasNext() { - return setIterator.hasNext() || mapIterator.hasNext(); - } - public Object next() { - if (!setIterator.hasNext()) { - setIterator = getSetIterator(); - } - return setIterator.next(); - } - public void remove() { - throw new UnsupportedOperationException(); - } - } - /** - * - */ - public void clear() { - m.clear(); - } - public int size() { - return size; - } - public boolean isEmpty() { - return size == 0; - } - public boolean contains(Object o) { - Set set = (Set)m.get(o); - if (set == null) return false; - return set.contains(o); - } - public Object[] toArray() { - return toArray(new Object[size]); - } - public Object[] toArray(Object[] a) { - int count = 0; - for (Iterator it = iterator(); it.hasNext();) { - a[count++] = it.next(); - } - return a; - } - /* (non-Javadoc) - * @see java.util.Collection#remove(java.lang.Object) - */ - public boolean remove(Object o) { - Set set = (Set)m.get(o); - if (set == null) return false; - if (!set.remove(o)) return false; - if (set.size() == 0) m.remove(o); - size--; - return true; - } - public boolean containsAll(Collection c) { - for (Iterator it = c.iterator(); it.hasNext(); ) { - if (!contains(it.next())) return false; - } - return true; - } - public boolean addAll(Collection c) { - boolean result = false; - for (Iterator it = c.iterator(); it.hasNext(); ) { - if (add(it.next())) result = true; - } - return result; - } - public boolean removeAll(Collection c) { - boolean result = false; - for (Iterator it = c.iterator(); it.hasNext(); ) { - if (remove(it.next())) result = true; - } - return result; - } - public boolean retainAll(Collection c) { - // WARNING: this may not work if the comparator does not distinguish - // all items that are equals(). - Set stuffToRemove = new HashSet(); // have to do this since iterator may not allow removal! - for (Iterator it = iterator(); it.hasNext(); ) { - Object item = it.next(); - if (!c.contains(item)) stuffToRemove.add(item); - } - return removeAll(stuffToRemove); - } -} diff --git a/icu4j/main/tests/framework/src/com/ibm/icu/dev/util/Tabber.java b/icu4j/main/tests/framework/src/com/ibm/icu/dev/util/Tabber.java deleted file mode 100644 index 3e42d697526..00000000000 --- a/icu4j/main/tests/framework/src/com/ibm/icu/dev/util/Tabber.java +++ /dev/null @@ -1,239 +0,0 @@ - -/* - ******************************************************************************* - * Copyright (C) 2002-2012, International Business Machines Corporation and * - * others. All Rights Reserved. * - ******************************************************************************* - */ -package com.ibm.icu.dev.util; - -import java.util.ArrayList; -import java.util.List; - -public abstract class Tabber { - public static final byte LEFT = 0, CENTER = 1, RIGHT = 2; - private static final String[] ALIGNMENT_NAMES = {"Left", "Center", "Right"}; - - /** - * Repeats a string n times - * @param source - * @param times - */ - // TODO - optimize repeats using doubling? - public static String repeat(String source, int times) { - if (times <= 0) return ""; - if (times == 1) return source; - StringBuffer result = new StringBuffer(); - for (; times > 0; --times) { - result.append(source); - } - return result.toString(); - } - - public String process(String source) { - StringBuffer result = new StringBuffer(); - int lastPos = 0; - for (int count = 0; lastPos < source.length(); ++count) { - int pos = source.indexOf('\t', lastPos); - if (pos < 0) pos = source.length(); - process_field(count, source, lastPos, pos, result); - lastPos = pos+1; - } - return prefix + result.toString() + postfix; - } - - private String prefix = ""; - private String postfix = ""; - - public abstract void process_field(int count, String source, int start, int limit, StringBuffer output); - - public Tabber clear() { - return this; - } - - public static class MonoTabber extends Tabber { - int minGap = 0; - - private List stops = new ArrayList(); - private List types = new ArrayList(); - - public Tabber clear() { - stops.clear(); - types.clear(); - minGap = 0; - return this; - } - - public String toString() { - StringBuffer buffer = new StringBuffer(); - for (int i = 0; i < stops.size(); ++i) { - if (i != 0) buffer.append("; "); - buffer - .append(ALIGNMENT_NAMES[((Integer)types.get(i)).intValue()]) - .append(",") - .append(stops.get(i)); - } - return buffer.toString(); - } - - /** - * Adds tab stop and how to align the text UP TO that stop - * @param tabPos - * @param type - */ - public MonoTabber addAbsolute(int tabPos, int type) { - stops.add(new Integer(tabPos)); - types.add(new Integer(type)); - return this; - } - - /** - * Adds relative tab stop and how to align the text UP TO that stop - */ - public Tabber add(int fieldWidth, byte type) { - int last = getStop(stops.size()-1); - stops.add(new Integer(last + fieldWidth)); - types.add(new Integer(type)); - return this; - } - - public int getStop(int fieldNumber) { - if (fieldNumber < 0) return 0; - if (fieldNumber >= stops.size()) fieldNumber = stops.size() - 1; - return ((Integer)stops.get(fieldNumber)).intValue(); - } - public int getType(int fieldNumber) { - if (fieldNumber < 0) return LEFT; - if (fieldNumber >= stops.size()) return LEFT; - return ((Integer)types.get(fieldNumber)).intValue(); - } - /* - public String process(String source) { - StringBuffer result = new StringBuffer(); - int lastPos = 0; - int count = 0; - for (count = 0; lastPos < source.length() && count < stops.size(); count++) { - int pos = source.indexOf('\t', lastPos); - if (pos < 0) pos = source.length(); - String piece = source.substring(lastPos, pos); - int stopPos = getStop(count); - if (result.length() < stopPos) { - result.append(repeat(" ", stopPos - result.length())); - // TODO fix type - } - result.append(piece); - lastPos = pos+1; - } - if (lastPos < source.length()) { - result.append(source.substring(lastPos)); - } - return result.toString(); - } - */ - - public void process_field(int count, String source, int start, int limit, StringBuffer output) { - String piece = source.substring(start, limit); - int startPos = getStop(count-1); - int endPos = getStop(count) - minGap; - int type = getType(count); - switch (type) { - case LEFT: - break; - case RIGHT: - startPos = endPos - piece.length(); - break; - case CENTER: - startPos = (startPos + endPos - piece.length() + 1)/2; - break; - } - - int gap = startPos - output.length(); - if (count != 0 && gap < minGap) gap = minGap; - if (gap > 0) output.append(repeat(" ", gap)); - output.append(piece); - } - - } - - public static Tabber NULL_TABBER = new Tabber() { - public void process_field(int count, String source, int start, int limit, StringBuffer output) { - if (count > 0) output.append( "\t"); - output.append(source.substring(start, limit)); - } - }; - - public static class HTMLTabber extends Tabber { - private List parameters = new ArrayList(); - private String element = "td"; - { - setPrefix(""); - setPostfix(""); - } - public HTMLTabber setParameters(int count, String params) { - // fill in - while (parameters.size() <= count) { - parameters.add(null); - } - parameters.set(count,params); - return this; - } - - public String getElement() { - return element; - } - - public HTMLTabber setElement(String element) { - this.element = element; - return this; - } - - public void process_field(int count, String source, int start, int limit, StringBuffer output) { - output.append("<" + element); - String params = null; - if (count < parameters.size()) { - params = parameters.get(count); - } - if (params != null) { - output.append(' '); - output.append(params); - } - output.append(">"); - output.append(source.substring(start, limit)); - // TODO Quote string - output.append(""); - } - } - /** - */ - public String getPostfix() { - return postfix; - } - - /** - */ - public String getPrefix() { - return prefix; - } - - /** - * @param string - */ - public Tabber setPostfix(String string) { - postfix = string; - return this; - } - - /** - * @param string - */ - public Tabber setPrefix(String string) { - prefix = string; - return this; - } - - public Tabber add(int i, byte left2) { - // does nothing unless overridden - return this; - } - -} diff --git a/icu4j/main/tests/framework/src/com/ibm/icu/dev/util/VariableReplacer.java b/icu4j/main/tests/framework/src/com/ibm/icu/dev/util/VariableReplacer.java deleted file mode 100644 index c3fa9384027..00000000000 --- a/icu4j/main/tests/framework/src/com/ibm/icu/dev/util/VariableReplacer.java +++ /dev/null @@ -1,44 +0,0 @@ -/* - ******************************************************************************* - * Copyright (C) 2002-2012, International Business Machines Corporation and * - * others. All Rights Reserved. * - ******************************************************************************* - */ -package com.ibm.icu.dev.util; - -import java.util.Collections; -import java.util.Iterator; -import java.util.Map; -import java.util.TreeMap; - -public class VariableReplacer { - // simple implementation for now - private Map m = new TreeMap(Collections.reverseOrder()); - - // TODO - fix to do streams also, clean up implementation - - public VariableReplacer add(String variable, String value) { - m.put(variable, value); - return this; - } - public String replace(String source) { - String oldSource; - do { - oldSource = source; - for (Iterator it = m.keySet().iterator(); it.hasNext();) { - String variable = (String) it.next(); - String value = (String) m.get(variable); - source = replaceAll(source, variable, value); - } - } while (!source.equals(oldSource)); - return source; - } - public String replaceAll(String source, String key, String value) { - while (true) { - int pos = source.indexOf(key); - if (pos < 0) return source; - source = source.substring(0,pos) + value + source.substring(pos+key.length()); - } - } -} - diff --git a/icu4j/main/tests/framework/src/com/ibm/icu/dev/util/Visitor.java b/icu4j/main/tests/framework/src/com/ibm/icu/dev/util/Visitor.java deleted file mode 100644 index 077101e3b1b..00000000000 --- a/icu4j/main/tests/framework/src/com/ibm/icu/dev/util/Visitor.java +++ /dev/null @@ -1,133 +0,0 @@ -/* - ******************************************************************************* - * Copyright (C) 2002-2012, International Business Machines Corporation and * - * others. All Rights Reserved. * - ******************************************************************************* - */ -package com.ibm.icu.dev.util; - -import java.util.Collection; -import java.util.Iterator; -import java.util.Map; - -import com.ibm.icu.text.UnicodeSet; -import com.ibm.icu.text.UnicodeSetIterator; - -public abstract class Visitor { - - public void doAt(Object item) { - if (item instanceof Collection) { - doAt((Collection) item); - } else if (item instanceof Map) { - doAt((Map) item); - } else if (item instanceof Object[]) { - doAt((Object[]) item); - } else if (item instanceof UnicodeSet) { - doAt((UnicodeSet) item); - } else { - doSimpleAt(item); - } - } - - public int count(Object item) { - if (item instanceof Collection) { - return ((Collection) item).size(); - } else if (item instanceof Map) { - return ((Map) item).size(); - } else if (item instanceof Object[]) { - return ((Object[]) item).length; - } else if (item instanceof UnicodeSet) { - return ((UnicodeSet) item).size(); - } else { - return 1; - } - } - - // the default implementation boxing - - public void doAt(int o) { - doSimpleAt(new Integer(o)); - } - public void doAt(double o) { - doSimpleAt(new Double(o)); - } - public void doAt(char o) { - doSimpleAt(new Character(o)); - } - - // for subclassing - - protected void doAt (Collection c) { - if (c.size() == 0) doBefore(c, null); - Iterator it = c.iterator(); - boolean first = true; - Object last = null; - while (it.hasNext()) { - Object item = it.next(); - if (first) { - doBefore(c, item); - first = false; - } else { - doBetween(c, last, item); - } - doAt(last=item); - } - doAfter(c, last); - } - - protected void doAt (Map c) { - doAt(c.entrySet()); - } - - protected void doAt (UnicodeSet c) { - if (c.size() == 0) doBefore(c, null); - UnicodeSetIterator it = new UnicodeSetIterator(c); - boolean first = true; - Object last = null; - Object item; - CodePointRange cpr0 = new CodePointRange(); - CodePointRange cpr1 = new CodePointRange(); - CodePointRange cpr; - - while(it.nextRange()) { - if (it.codepoint == UnicodeSetIterator.IS_STRING) { - item = it.string; - } else { - cpr = last == cpr0 ? cpr1 : cpr0; // make sure we don't override last - cpr.codepoint = it.codepoint; - cpr.codepointEnd = it.codepointEnd; - item = cpr; - } - if (!first) { - doBefore(c, item); - first = true; - } else { - doBetween(c, last, item); - } - doAt(last = item); - } - doAfter(c, last); - } - - protected void doAt (Object[] c) { - doBefore(c, c.length == 0 ? null : c[0]); - Object last = null; - for (int i = 0; i < c.length; ++i) { - if (i != 0) doBetween(c, last, c[i]); - doAt(last = c[i]); - } - doAfter(c, last); - } - - public static class CodePointRange{ - public int codepoint, codepointEnd; - } - - // ===== MUST BE OVERRIDEN ===== - - abstract protected void doBefore(Object container, Object item); - abstract protected void doBetween(Object container, Object lastItem, Object nextItem); - abstract protected void doAfter(Object container, Object item); - abstract protected void doSimpleAt(Object o); - -} \ No newline at end of file diff --git a/icu4j/main/tests/framework/src/com/ibm/icu/dev/util/XEquivalenceClass.java b/icu4j/main/tests/framework/src/com/ibm/icu/dev/util/XEquivalenceClass.java deleted file mode 100644 index 449ac210010..00000000000 --- a/icu4j/main/tests/framework/src/com/ibm/icu/dev/util/XEquivalenceClass.java +++ /dev/null @@ -1,331 +0,0 @@ -/* - ******************************************************************************* - * Copyright (C) 1996-2013, International Business Machines Corporation and * - * others. All Rights Reserved. * - ******************************************************************************* - */ -package com.ibm.icu.dev.util; - -import java.util.ArrayList; -import java.util.Collections; -import java.util.HashMap; -import java.util.HashSet; -import java.util.Iterator; -import java.util.List; -import java.util.Map; -import java.util.Set; - -import com.ibm.icu.text.Transform; - -public class XEquivalenceClass implements Iterable { - private static final String ARROW = "\u2192"; - - public SetMaker getSetMaker() { - return setMaker; - } - - // quick test - static public void main(String[] args) { - XEquivalenceClass foo1 = new XEquivalenceClass(1); - String[][] tests = {{"b","a1"}, {"b", "c"}, {"a1", "c"}, {"d", "e"}, {"e", "f"}, {"c", "d"}}; - for (int i = 0; i < tests.length; ++i) { - System.out.println("Adding: " + tests[i][0] + ", " + tests[i][1]); - foo1.add(tests[i][0], tests[i][1], new Integer(i)); - for (String item : foo1.getExplicitItems()) { - System.out.println("\t" + item + ";\t" + foo1.getSample(item) + ";\t" + foo1.getEquivalences(item)); - List> reasons = foo1.getReasons(item, foo1.getSample(item)); - if (reasons != null) { - System.out.println("\t\t" + XEquivalenceClass.toString(reasons, null)); - } - } - } - } - - private Map> toPartitionSet = new HashMap(); - private Map>> obj_obj_reasons = new HashMap(); - private R defaultReason; - private SetMaker setMaker; - - public interface SetMaker { - Set make(); - } - - /** - * empty, as if just created - */ - public XEquivalenceClass clear(R defaultReasonArg) { - toPartitionSet.clear(); - obj_obj_reasons.clear(); - this.defaultReason = defaultReasonArg; - return this; - } - - /** - * Create class - * - */ - public XEquivalenceClass() { - } - - /** - * Create class with comparator, and default reason. - * - */ - public XEquivalenceClass(R defaultReason) { - this.defaultReason = defaultReason; - } - - /** - * Create class with comparator, and default reason. - * - */ - public XEquivalenceClass(R defaultReason, SetMaker setMaker) { - this.defaultReason = defaultReason; - this.setMaker = setMaker; - } - - /** - * Add two equivalent items, with NO_REASON for the reason. - */ - public XEquivalenceClass add(T a, T b) { - return add(a,b,null); - } - - /** - * Add two equivalent items, with NO_REASON for the reason. - */ - public XEquivalenceClass add(T a, T b, R reason) { - return add(a,b,reason, reason); - } - - /** - * Add two equivalent items, plus a reason. The reason is only used for getReasons - */ - public XEquivalenceClass add(T a, T b, R reasonAB, R reasonBA) { - if (a.equals(b)) return this; - if (reasonAB == null) reasonAB = defaultReason; - if (reasonBA == null) reasonBA = defaultReason; - addReason(a,b,reasonAB); - addReason(b,a,reasonBA); - SetaPartitionSet = toPartitionSet.get(a); - SetbPartitionSet = toPartitionSet.get(b); - if (aPartitionSet == null) { - if (bPartitionSet == null) { // both null, set up bSet - bPartitionSet = setMaker != null ? setMaker.make() : new HashSet(); - bPartitionSet.add(b); - toPartitionSet.put(b, bPartitionSet); - } - bPartitionSet.add(a); - toPartitionSet.put(a, bPartitionSet); - } else if (bPartitionSet == null) { // aSet is not null, bSet null - aPartitionSet.add(b); - toPartitionSet.put(b, aPartitionSet); - } else if (aPartitionSet != bPartitionSet) { // both non-null, not equal, merge. Equality check ok here - aPartitionSet.addAll(bPartitionSet); - // remap every x that had x => bPartitionSet - for (T item : bPartitionSet) { - toPartitionSet.put(item, aPartitionSet); - } - } - return this; - } - - /** - * Add all the information from the other class - * - */ - public XEquivalenceClass addAll(XEquivalenceClass other) { - // For now, does the simple, not optimized version - for (T a : other.obj_obj_reasons.keySet()) { - Map> obj_reasons = other.obj_obj_reasons.get(a); - for (T b : obj_reasons.keySet()) { - Set reasons = obj_reasons.get(b); - for (R reason: reasons) { - add(a, b, reason); - } - } - } - return this; - } - - /** - * - */ - private void addReason(T a, T b, R reason) { - Map> obj_reasons = obj_obj_reasons.get(a); - if (obj_reasons == null) obj_obj_reasons.put(a, obj_reasons = new HashMap()); - Set reasons = obj_reasons.get(b); - if (reasons == null) obj_reasons.put(b, reasons = new HashSet()); - reasons.add(reason); - } - - /** - * Returns a set of all the explicit items in the equivalence set. (Any non-explicit items only - * have themselves as equivalences.) - * - */ - public Set getExplicitItems() { - return Collections.unmodifiableSet(toPartitionSet.keySet()); - } - - /** - * Returns an unmodifiable set of all the equivalent objects - * - */ - public SetgetEquivalences(T a) { - Set aPartitionSet = toPartitionSet.get(a); - if (aPartitionSet == null) { // manufacture an equivalence - aPartitionSet = new HashSet(); - aPartitionSet.add(a); - } - return Collections.unmodifiableSet(aPartitionSet); - } - - public boolean hasEquivalences(T a) { - return toPartitionSet.get(a) != null; - } - - public Set> getEquivalenceSets() { - Set> result = new HashSet>(); - for (T item : toPartitionSet.keySet()) { - Set partition = toPartitionSet.get(item); - result.add(Collections.unmodifiableSet(partition)); - } - return result; - } - /** - * returns true iff a is equivalent to b (or a.equals b) - * - */ - public boolean isEquivalent(T a, T b) { - if (a.equals(b)) return true; - SetaPartitionSet = toPartitionSet.get(a); - if (aPartitionSet == null) return false; - return aPartitionSet.contains(b); - } - - /** - * Gets a sample object in the equivalence set for a. - * - */ - public T getSample(T a) { - Set aPartitionSet = toPartitionSet.get(a); - if (aPartitionSet == null) return a; // singleton - return aPartitionSet.iterator().next(); - } - - public interface Filter { - boolean matches(T o); - } - - public T getSample(T a, Filter f) { - Set aPartitionSet = toPartitionSet.get(a); - if (aPartitionSet == null) return a; // singleton - for (T obj : aPartitionSet) { - if (f.matches(obj)) return obj; - } - return a; - } - - /** - * gets the set of all the samples, one from each equivalence class. - * - */ - public Set getSamples() { - Set seenAlready = new HashSet(); - Set result = new HashSet(); - for (T item : toPartitionSet.keySet()) { - if (seenAlready.contains(item)) continue; - Set partition = toPartitionSet.get(item); - result.add(partition.iterator().next()); - seenAlready.addAll(partition); - } - return result; - } - - public Iterator iterator() { - return getSamples().iterator(); - } - - public static class Linkage { - public Set reasons; - public T result; - - public Linkage(Set reasons, T result) { - this.reasons = reasons; - this.result = result; - } - public String toString() { - return reasons + (result == null ? "" : ARROW + result); - } - } - - public static String toString(List> others, Transform,String> itemTransform) { - StringBuffer result = new StringBuffer(); - for (Linkage item : others) { - result.append(itemTransform == null ? item.toString() : itemTransform.transform(item)); - } - return result.toString(); - } - - /** - * Returns a list of linkages, where each set of reasons to go from one obj to the next. The list does not include a and b themselves. - * The last linkage has a null result.
- * Returns null if there is no connection. - */ - public List> getReasons(T a, T b) { - // use dumb algorithm for getting shortest path - // don't bother with optimization - Set aPartitionSet = toPartitionSet.get(a); - Set bPartitionSet = toPartitionSet.get(b); - - // see if they connect - if (aPartitionSet == null || bPartitionSet == null || aPartitionSet != bPartitionSet || a.equals(b)) return null; - - ArrayList> list = new ArrayList>(); - list.add(new Linkage(null, a)); - ArrayList>> lists = new ArrayList>>(); - lists.add(list); - - // this will contain the results - Set sawLastTime = new HashSet(); - sawLastTime.add(a); - - // each time, we extend each lists by one (adding multiple other lists) - while (true) { // foundLists.size() == 0 - ArrayList extendedList = new ArrayList(); - SetsawThisTime = new HashSet(); - for (ArrayList> lista : lists) { - Linkage last = lista.get(lista.size()-1); - Map> obj_reasons = obj_obj_reasons.get(last.result); - for (T result : obj_reasons.keySet()) { - if (sawLastTime.contains(result)) { - continue; // skip since we have shorter - } - sawThisTime.add(result); - Set reasons = obj_reasons.get(result); - ArrayList> lista2 = (ArrayList>) lista.clone(); - lista2.add(new Linkage(reasons,result)); - extendedList.add(lista2); - if (result.equals(b)) { - // remove first and last - ArrayList> found = (ArrayList>) lista2.clone(); - found.remove(0); - found.get(found.size()-1).result = null; - return found; - } - } - } - lists = extendedList; - sawLastTime.addAll(sawThisTime); - } - // return foundLists; - } - - /** - * For debugging. - */ - public String toString() { - return getEquivalenceSets().toString(); - } -} \ No newline at end of file diff --git a/icu4j/main/tests/framework/src/com/ibm/icu/dev/util/XEquivalenceMap.java b/icu4j/main/tests/framework/src/com/ibm/icu/dev/util/XEquivalenceMap.java deleted file mode 100644 index 7d09a952800..00000000000 --- a/icu4j/main/tests/framework/src/com/ibm/icu/dev/util/XEquivalenceMap.java +++ /dev/null @@ -1,142 +0,0 @@ -/* - ******************************************************************************* - * Copyright (C) 1996-2012, International Business Machines Corporation and * - * others. All Rights Reserved. * - ******************************************************************************* - */ -package com.ibm.icu.dev.util; - -import java.util.Collections; -import java.util.HashMap; -import java.util.HashSet; -import java.util.Iterator; -import java.util.Map; -import java.util.Set; - -import com.ibm.icu.impl.Row; -import com.ibm.icu.impl.Row.R2; - -/** - * Everything that maps to the same value is part of the same equivalence class - * @author davis - * - */ -public class XEquivalenceMap implements Iterable> { - - Map>> source_target_reasons = new HashMap>>(); - - Map> target_sourceSet; - Map> source_Set = new HashMap>(); // not really needed: could go source-target-sourceset - - public XEquivalenceMap() { - this(new HashMap>()); - } - - public XEquivalenceMap(Map> storage) { - target_sourceSet = storage; - } - - public XEquivalenceMap clear() { - source_target_reasons.clear(); - target_sourceSet.clear(); - source_Set.clear(); - return this; - } - - public XEquivalenceMap add(K source, V target) { - return add(source, target, null); - } - - public XEquivalenceMap add(K source, V target, R reason) { - R2> target_reasons = source_target_reasons.get(source); - if (target_reasons == null) { - Set reasons = new HashSet(); - if (reason != null) { - reasons.add(reason); - } - target_reasons = Row.of(target, reasons); - source_target_reasons.put(source, target_reasons); - } else { - V otherTarget = target_reasons.get0(); - Set reasons = target_reasons.get1(); - if (otherTarget.equals(target)) { - if (reason != null) { - reasons.add(reason); - } - return this; - } - throw new IllegalArgumentException("Same source mapping to different targets: " - + source + " => " + otherTarget + " & " + target); - } - - Set s = target_sourceSet.get(target); - if (s == null) target_sourceSet.put(target, s = new HashSet()); - s.add(source); - source_Set.put(source, s); - return this; - } - - public Set getEquivalences (K source) { - Set s = source_Set.get(source); - if (s == null) return null; - return Collections.unmodifiableSet(s); - } - - public boolean areEquivalent (K source1, K source2) { - Set s = (Set) source_Set.get(source1); - if (s == null) return false; - return s.contains(source2); - } - - public V getTarget(K source) { - return source_target_reasons.get(source).get0(); - } - - public Set getReasons(K source) { - return Collections.unmodifiableSet(source_target_reasons.get(source).get1()); - } - - public Set getSources(V target) { - Set s = target_sourceSet.get(target); - return Collections.unmodifiableSet(s); - } - - public Iterator> iterator() { - return UnmodifiableIterator.from(target_sourceSet.values()); - } - - public int size() { - return target_sourceSet.size(); - } - - public boolean isEmpty() { - return target_sourceSet.isEmpty(); - } - - // Should be moved out on its own - public static class UnmodifiableIterator implements Iterator { - private Iterator source; - - public static UnmodifiableIterator from(Iterator source) { - UnmodifiableIterator result = new UnmodifiableIterator(); - result.source = source; - return result; - } - - public static UnmodifiableIterator from(Iterable source) { - return from(source.iterator()); - } - - public void remove() { - throw new UnsupportedOperationException(); - } - - public boolean hasNext() { - return source.hasNext(); - } - - public T next() { - return source.next(); - } - } -} \ No newline at end of file -- 2.40.0