001: /*
002: * <copyright>
003: *
004: * Copyright 1997-2004 BBNT Solutions, LLC
005: * under sponsorship of the Defense Advanced Research Projects
006: * Agency (DARPA).
007: *
008: * You can redistribute this software and/or modify it under the
009: * terms of the Cougaar Open Source License as published on the
010: * Cougaar Open Source Website (www.cougaar.org).
011: *
012: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
013: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
014: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
015: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
016: * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
017: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
018: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
019: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
020: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
021: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
022: * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
023: *
024: * </copyright>
025: */
026: package org.cougaar.util;
027:
028: import java.util.Collection;
029: import java.util.Collections;
030: import java.util.HashMap;
031: import java.util.Iterator;
032: import java.util.Map;
033: import java.util.Set;
034:
035: /**
036: * KeyedSet is a custom collection which looks like a Set,
037: * but allows redefinition of the key to use. The default key
038: * is the identity operation.
039: **/
040:
041: public class KeyedSet implements Set {
042: protected Map inner;
043:
044: public KeyedSet() {
045: inner = new HashMap(89);
046: }
047:
048: public KeyedSet(int s) {
049: inner = new HashMap(s);
050: }
051:
052: public KeyedSet(Collection c) {
053: inner = new HashMap(c.size() * 2 + 1);
054: addAll(c);
055: }
056:
057: public KeyedSet makeSynchronized() {
058: inner = Collections.synchronizedMap(inner);
059: return this ;
060: }
061:
062: public void clear() {
063: inner.clear();
064: }
065:
066: public boolean contains(Object o) {
067: return inner.containsValue(o);
068: }
069:
070: public boolean containsAll(Collection c) {
071: return inner.values().containsAll(c);
072: }
073:
074: public boolean isEmpty() {
075: return inner.isEmpty();
076: }
077:
078: public Iterator iterator() {
079: return inner.values().iterator();
080: }
081:
082: public int size() {
083: return inner.size();
084: }
085:
086: public Object[] toArray() {
087: return inner.values().toArray();
088: }
089:
090: public Object[] toArray(Object[] a) {
091: return inner.values().toArray(a);
092: }
093:
094: public Set keySet() {
095: return inner.keySet();
096: }
097:
098: public int hashCode() {
099: return 7 + inner.hashCode();
100: }
101:
102: public boolean equals(Object o) {
103: if (this == o)
104: return true;
105: if (o instanceof CollectionDelegate) {
106: return inner.equals(((CollectionDelegate) o).inner);
107: } else {
108: return false;
109: }
110: }
111:
112: /** override this method to get a more useful key **/
113: protected Object getKey(Object o) {
114: return o;
115: }
116:
117: public boolean add(Object o) {
118: Object key = getKey(o);
119: if (key != null) {
120: return (inner.put(key, o) == o);
121: } else {
122: return false;
123: }
124: }
125:
126: public boolean addAll(Collection c) {
127: boolean hasChanged = false;
128: for (Iterator i = c.iterator(); i.hasNext();) {
129: if (add(i.next()))
130: hasChanged = true;
131: }
132: return hasChanged;
133: }
134:
135: public boolean remove(Object o) {
136: Object key = getKey(o);
137: if (key != null) {
138: return (inner.remove(key) != null);
139: } else {
140: return false;
141: }
142: }
143:
144: public boolean removeAll(Collection c) {
145: boolean hasChanged = false;
146: for (Iterator i = c.iterator(); i.hasNext();) {
147: if (remove(i.next()))
148: hasChanged = true;
149: }
150: return hasChanged;
151: }
152:
153: // implement some time
154: public boolean retainAll(Collection c) {
155: throw new RuntimeException("KeyedSet.retainAll not implemented");
156: }
157: }
|