]> gitweb.fperrin.net Git - Dictionary.git/blob - jars/icu4j-4_4_2-src/main/tests/core/src/com/ibm/icu/dev/test/util/XEquivalenceMap.java
go
[Dictionary.git] / jars / icu4j-4_4_2-src / main / tests / core / src / com / ibm / icu / dev / test / util / XEquivalenceMap.java
1 /*\r
2  *******************************************************************************\r
3  * Copyright (C) 1996-2008, International Business Machines Corporation and    *\r
4  * others. All Rights Reserved.                                                *\r
5  *******************************************************************************\r
6  */\r
7 package com.ibm.icu.dev.test.util;\r
8 \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
14 \r
15 /**\r
16  * Everything that maps to the same value is part of the same equivalence class\r
17  * @author davis\r
18  *\r
19  */\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
27         source_Set.clear();\r
28         return this;\r
29     }\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
36         }\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
40         s.add(source);\r
41         source_Set.put(source, s);\r
42         return this;\r
43     }\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
48     }\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
53     }\r
54     public Object getTarget(Object source) {\r
55         return source_target.get(source);\r
56     }\r
57     public Set getSources(Object target) {\r
58         Set s = (Set) target_sourceSet.get(target);\r
59         return Collections.unmodifiableSet(s);\r
60     }\r
61     public Iterator iterator() {\r
62         MyIterator result = new MyIterator();\r
63         result.target_sourceSet_iterator = target_sourceSet.keySet().iterator();\r
64         return result;\r
65     }\r
66     public int size() {\r
67         return target_sourceSet.size();\r
68     }\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
73         }\r
74         public boolean hasNext() {\r
75             return target_sourceSet_iterator.hasNext();\r
76         }\r
77         public Object next() {\r
78             return getSources(target_sourceSet_iterator.next());\r
79         }\r
80     }\r
81 }