]> gitweb.fperrin.net Git - Dictionary.git/blob - jars/icu4j-4_2_1-src/src/com/ibm/icu/dev/test/util/Relation.java
icu4jsrc
[Dictionary.git] / jars / icu4j-4_2_1-src / src / com / ibm / icu / dev / test / util / Relation.java
1 /*\r
2 **********************************************************************\r
3 * Copyright (c) 2002-2006, International Business Machines\r
4 * Corporation and others.  All Rights Reserved.\r
5 **********************************************************************\r
6 * Author: Mark Davis\r
7 **********************************************************************\r
8 */\r
9 package com.ibm.icu.dev.test.util;\r
10 \r
11 import java.util.Collection;\r
12 import java.util.Comparator;\r
13 import java.util.HashSet;\r
14 import java.util.Iterator;\r
15 import java.util.Map;\r
16 import java.util.Set;\r
17 import java.util.TreeSet;\r
18 \r
19 /**\r
20  * A Relation is a set of mappings from keys to values.\r
21  * Unlike Map, there is not guaranteed to be a single value per key.\r
22  * The Map-like APIs return collections for values.\r
23  * @author medavis\r
24  *\r
25  * TODO To change the template for this generated type comment go to\r
26  * Window - Preferences - Java - Code Style - Code Templates\r
27  */\r
28 public class Relation {\r
29     private Map m;\r
30     private CollectionFactory subcollection;\r
31 \r
32     public Relation(Map mainMap, CollectionFactory subcollection) {\r
33         m = mainMap;\r
34         if (subcollection == null) subcollection = new CollectionMaker(null);\r
35         this.subcollection = subcollection;\r
36     }\r
37 \r
38     public void clear() {\r
39         m.clear();\r
40     }\r
41     public boolean containsKey(Object key) {\r
42         return m.containsKey(key);\r
43     }\r
44     public boolean containsValue(Object value) {\r
45         return m.containsValue(value);\r
46     }\r
47     public Set entrySet() {\r
48         return m.entrySet();\r
49     }\r
50     public boolean equals(Object obj) {\r
51         return m.equals(obj);\r
52     }\r
53     public int hashCode() {\r
54         return m.hashCode();\r
55     }\r
56     public boolean isEmpty() {\r
57         return m.isEmpty();\r
58     }\r
59     public Object remove(Object key) {\r
60         return m.remove(key);\r
61     }\r
62     public int size() {\r
63         return m.size();\r
64     }\r
65     public String toString() {\r
66         return m.toString();\r
67     }\r
68     public Set keySet() {\r
69         return m.keySet();\r
70     }\r
71     /*\r
72     public void addAll(Relation t) {\r
73         for (Iterator it = t.keySet().iterator(); it.hasNext();) {\r
74             Object key = it.next();\r
75             add(key, t.get(key));\r
76         }\r
77     }\r
78     */\r
79     public Collection values() {\r
80         return m.values();\r
81     }\r
82     public Collection get(Object key, Collection output) {\r
83         output.addAll((Collection)m.get(key));\r
84         return output;\r
85     }\r
86     public void add(Object key, Object value) {\r
87         Collection o = (Collection) m.get(key);\r
88         if (o == null) m.put(key, o = subcollection.make());\r
89         o.add(value);\r
90     }\r
91     public Iterator iterator() {\r
92         return m.keySet().iterator();\r
93     }\r
94     public interface CollectionFactory {\r
95         Collection make();\r
96     }\r
97 \r
98     /**\r
99      * This is just temporary, and may change!!\r
100      * @author medavis\r
101      *\r
102      * TODO To change the template for this generated type comment go to\r
103      * Window - Preferences - Java - Code Style - Code Templates\r
104      */\r
105     public static class CollectionMaker implements CollectionFactory {\r
106         public static final int HASH = 0, TREE = 1;\r
107         private Comparator comparator = null;\r
108         private int type = HASH;\r
109 \r
110         public CollectionMaker(int type) {\r
111             this.type = type;\r
112         }\r
113         public CollectionMaker(Comparator comparator) {\r
114             this.comparator = comparator;\r
115         }\r
116         public Collection make() {\r
117             if (comparator != null) return new TreeSet(comparator);\r
118             else if (type == HASH) return new HashSet();\r
119             else return new TreeSet();\r
120         }\r
121     }\r
122 }