001: /*
002: **********************************************************************
003: * Copyright (c) 2002-2006, International Business Machines
004: * Corporation and others. All Rights Reserved.
005: **********************************************************************
006: * Author: Mark Davis
007: **********************************************************************
008: */
009: package com.ibm.icu.dev.test.util;
010:
011: import java.util.Collection;
012: import java.util.Comparator;
013: import java.util.HashSet;
014: import java.util.Iterator;
015: import java.util.Map;
016: import java.util.Set;
017: import java.util.TreeSet;
018:
019: /**
020: * A Relation is a set of mappings from keys to values.
021: * Unlike Map, there is not guaranteed to be a single value per key.
022: * The Map-like APIs return collections for values.
023: * @author medavis
024: *
025: * TODO To change the template for this generated type comment go to
026: * Window - Preferences - Java - Code Style - Code Templates
027: */
028: public class Relation {
029: private Map m;
030: private CollectionFactory subcollection;
031:
032: public Relation(Map mainMap, CollectionFactory subcollection) {
033: m = mainMap;
034: if (subcollection == null)
035: subcollection = new CollectionMaker(null);
036: this .subcollection = subcollection;
037: }
038:
039: public void clear() {
040: m.clear();
041: }
042:
043: public boolean containsKey(Object key) {
044: return m.containsKey(key);
045: }
046:
047: public boolean containsValue(Object value) {
048: return m.containsValue(value);
049: }
050:
051: public Set entrySet() {
052: return m.entrySet();
053: }
054:
055: public boolean equals(Object obj) {
056: return m.equals(obj);
057: }
058:
059: public int hashCode() {
060: return m.hashCode();
061: }
062:
063: public boolean isEmpty() {
064: return m.isEmpty();
065: }
066:
067: public Object remove(Object key) {
068: return m.remove(key);
069: }
070:
071: public int size() {
072: return m.size();
073: }
074:
075: public String toString() {
076: return m.toString();
077: }
078:
079: public Set keySet() {
080: return m.keySet();
081: }
082:
083: /*
084: public void addAll(Relation t) {
085: for (Iterator it = t.keySet().iterator(); it.hasNext();) {
086: Object key = it.next();
087: add(key, t.get(key));
088: }
089: }
090: */
091: public Collection values() {
092: return m.values();
093: }
094:
095: public Collection get(Object key, Collection output) {
096: output.addAll((Collection) m.get(key));
097: return output;
098: }
099:
100: public void add(Object key, Object value) {
101: Collection o = (Collection) m.get(key);
102: if (o == null)
103: m.put(key, o = subcollection.make());
104: o.add(value);
105: }
106:
107: public Iterator iterator() {
108: return m.keySet().iterator();
109: }
110:
111: public interface CollectionFactory {
112: Collection make();
113: }
114:
115: /**
116: * This is just temporary, and may change!!
117: * @author medavis
118: *
119: * TODO To change the template for this generated type comment go to
120: * Window - Preferences - Java - Code Style - Code Templates
121: */
122: public static class CollectionMaker implements CollectionFactory {
123: public static final int HASH = 0, TREE = 1;
124: private Comparator comparator = null;
125: private int type = HASH;
126:
127: public CollectionMaker(int type) {
128: this .type = type;
129: }
130:
131: public CollectionMaker(Comparator comparator) {
132: this .comparator = comparator;
133: }
134:
135: public Collection make() {
136: if (comparator != null)
137: return new TreeSet(comparator);
138: else if (type == HASH)
139: return new HashSet();
140: else
141: return new TreeSet();
142: }
143: }
144: }
|