2 *******************************************************************************
\r
3 * Copyright (C) 1996-2008, International Business Machines Corporation and *
\r
4 * others. All Rights Reserved. *
\r
5 *******************************************************************************
\r
7 package com.ibm.icu.dev.test.util;
\r
9 import java.util.Collections;
\r
10 import java.util.HashMap;
\r
11 import java.util.HashSet;
\r
12 import java.util.Iterator;
\r
13 import java.util.Set;
\r
16 * Everything that maps to the same value is part of the same equivalence class
\r
20 public class XEquivalenceMap {
\r
21 HashMap source_target = new HashMap();
\r
22 HashMap target_sourceSet = new HashMap();
\r
23 HashMap source_Set = new HashMap();
\r
24 public XEquivalenceMap clear() {
\r
25 source_target.clear();
\r
26 target_sourceSet.clear();
\r
30 public XEquivalenceMap add(Object source, Object target) {
\r
31 Object otherTarget = source_target.get(source);
\r
32 if (otherTarget != null) {
\r
33 if (otherTarget.equals(target)) return this;
\r
34 throw new IllegalArgumentException("Same source mapping to different targets: "
\r
35 + source + " => " + otherTarget + " & " + target);
\r
37 source_target.put(source, target);
\r
38 Set s = (Set) target_sourceSet.get(target);
\r
39 if (s == null) target_sourceSet.put(target, s = new HashSet());
\r
41 source_Set.put(source, s);
\r
44 public Set getEquivalences (Object source) {
\r
45 Set s = (Set) source_Set.get(source);
\r
46 if (s == null) return null;
\r
47 return Collections.unmodifiableSet(s);
\r
49 public boolean areEquivalent (Object source1, Object source2) {
\r
50 Set s = (Set) source_Set.get(source1);
\r
51 if (s == null) return false;
\r
52 return s.contains(source2);
\r
54 public Object getTarget(Object source) {
\r
55 return source_target.get(source);
\r
57 public Set getSources(Object target) {
\r
58 Set s = (Set) target_sourceSet.get(target);
\r
59 return Collections.unmodifiableSet(s);
\r
61 public Iterator iterator() {
\r
62 MyIterator result = new MyIterator();
\r
63 result.target_sourceSet_iterator = target_sourceSet.keySet().iterator();
\r
67 return target_sourceSet.size();
\r
69 private class MyIterator implements Iterator {
\r
70 private Iterator target_sourceSet_iterator;
\r
71 public void remove() {
\r
72 throw new UnsupportedOperationException();
\r
74 public boolean hasNext() {
\r
75 return target_sourceSet_iterator.hasNext();
\r
77 public Object next() {
\r
78 return getSources(target_sourceSet_iterator.next());
\r