2 **********************************************************************
\r
3 * Copyright (c) 2002-2006, International Business Machines
\r
4 * Corporation and others. All Rights Reserved.
\r
5 **********************************************************************
\r
7 **********************************************************************
\r
9 package com.ibm.icu.dev.test.util;
\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
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
25 * TODO To change the template for this generated type comment go to
\r
26 * Window - Preferences - Java - Code Style - Code Templates
\r
28 public class Relation {
\r
30 private CollectionFactory subcollection;
\r
32 public Relation(Map mainMap, CollectionFactory subcollection) {
\r
34 if (subcollection == null) subcollection = new CollectionMaker(null);
\r
35 this.subcollection = subcollection;
\r
38 public void clear() {
\r
41 public boolean containsKey(Object key) {
\r
42 return m.containsKey(key);
\r
44 public boolean containsValue(Object value) {
\r
45 return m.containsValue(value);
\r
47 public Set entrySet() {
\r
48 return m.entrySet();
\r
50 public boolean equals(Object obj) {
\r
51 return m.equals(obj);
\r
53 public int hashCode() {
\r
54 return m.hashCode();
\r
56 public boolean isEmpty() {
\r
59 public Object remove(Object key) {
\r
60 return m.remove(key);
\r
65 public String toString() {
\r
66 return m.toString();
\r
68 public Set keySet() {
\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
79 public Collection values() {
\r
82 public Collection get(Object key, Collection output) {
\r
83 output.addAll((Collection)m.get(key));
\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
91 public Iterator iterator() {
\r
92 return m.keySet().iterator();
\r
94 public interface CollectionFactory {
\r
99 * This is just temporary, and may change!!
\r
102 * TODO To change the template for this generated type comment go to
\r
103 * Window - Preferences - Java - Code Style - Code Templates
\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
110 public CollectionMaker(int type) {
\r
113 public CollectionMaker(Comparator comparator) {
\r
114 this.comparator = comparator;
\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