001: // You can redistribute this software and/or modify it under the terms of
002: // the Ozone Library License version 1 published by ozone-db.org.
003: //
004: // The original code and portions created by SMB are
005: // Copyright (C) 1997-@year@ by SMB GmbH. All rights reserved.
006: //
007: // $Id: DxMultiMap.java,v 1.1 2001/12/18 10:31:30 per_nyfelt Exp $
008:
009: package org.ozoneDB.DxLib;
010:
011: import java.io.*;
012:
013: /**
014: *
015: *
016: * @author <a href="http://www.softwarebuero.de/">SMB</a>
017: * @version $Revision: 1.1 $Date: 2001/12/18 10:31:30 $
018: */
019: public class DxMultiMap extends DxAbstractCollection implements DxMap,
020: Externalizable {
021:
022: final static long serialVersionUID = 1L;
023:
024: protected transient DxMap map;
025: protected transient DxCollection containerFactory;
026: protected transient int multiItemCount = 0;
027:
028: public DxMultiMap() {
029: map = new DxHashMap();
030: containerFactory = new DxArrayBag(8);
031: }
032:
033: public DxMultiMap(DxMap _map, DxCollection _containerFactory) {
034: map = _map;
035: containerFactory = _containerFactory;
036: }
037:
038: public Object clone() {
039: try {
040: DxMultiMap answer = (DxMultiMap) getClass().newInstance();
041: answer.map = (DxMap) map.getClass().newInstance();
042: answer.containerFactory = (DxCollection) containerFactory
043: .getClass().newInstance();
044:
045: DxIterator it = iterator();
046: Object obj;
047: while ((obj = it.next()) != null) {
048: DxCollection newColl = (DxCollection) ((DxCollection) obj)
049: .clone();
050: answer.addForKey(newColl, it.key());
051: }
052: return answer;
053: } catch (Exception e) {
054: throw new RuntimeException(e.toString());
055: }
056: }
057:
058: public DxCollection valueClone() {
059: throw new RuntimeException(getClass().getName()
060: + ".valueClone() not implemented.");
061: }
062:
063: /**
064: * Compares two multimaps for equality.
065: */
066: public boolean equals(Object obj) {
067: if (obj instanceof DxMultiMap && obj != null) {
068: DxMultiMap rhs = (DxMultiMap) obj;
069:
070: if (this == obj) {
071: return true;
072: }
073:
074: //FIXME: alle keys muessen mit gleichen containern da sein
075: return false;
076: } else {
077: return false;
078: }
079: }
080:
081: public DxSet keySet() {
082: return map.keySet();
083: }
084:
085: public DxSet elementSet() {
086: DxSet answer = new DxHashSet();
087: answer.add(this );
088: return answer;
089: }
090:
091: public synchronized boolean add(Object obj) {
092: return addForKey(obj, buildKey(obj));
093: }
094:
095: public Object buildKey(Object obj) {
096: return obj;
097: }
098:
099: public synchronized boolean addForKey(Object obj, Object key) {
100: try {
101: DxCollection coll = (DxCollection) map.elementForKey(key);
102: if (coll == null) {
103: coll = (DxCollection) containerFactory.getClass()
104: .newInstance();
105: map.addForKey(coll, key);
106: }
107: coll.add(obj);
108: multiItemCount++;
109: return true;
110: } catch (Exception e) {
111: throw new RuntimeException(e.toString());
112: }
113: }
114:
115: public DxCollection elementsForKey(Object key) {
116: return (DxCollection) map.elementForKey(key);
117: }
118:
119: public Object elementForKey(Object key) {
120: return map.elementForKey(key);
121: }
122:
123: public Object keyForElement(Object obj) {
124: DxIterator it = iterator();
125: while (it.next() != null) {
126: DxIterator iit = iterator();
127: while (iit.next() != null) {
128: if (obj.equals(iit.object())) {
129: return iit.key();
130: }
131: }
132: }
133: return null;
134: }
135:
136: public synchronized Object removeForKey(Object key) {
137: DxCollection answer = (DxCollection) map.removeForKey(key);
138: if (answer != null) {
139: multiItemCount--;
140: }
141: return answer;
142: }
143:
144: public boolean removeAllKeys(DxCollection coll) {
145: throw new RuntimeException(getClass().getName()
146: + ".removeAllKeys() not implemented.");
147: }
148:
149: public boolean containsKey(Object key) {
150: return map.containsKey(key);
151: }
152:
153: /** */
154: public DxIterator iterator() {
155: return new DxMultiIterator(this );
156: }
157:
158: /** */
159: public int count() {
160: return multiItemCount;
161: }
162:
163: /** */
164: public boolean isEmpty() {
165: return multiItemCount == 0;
166: }
167:
168: public synchronized void clear() {
169: map.clear();
170: multiItemCount = 0;
171: }
172:
173: public void writeExternal(ObjectOutput out) throws IOException {
174: out.writeInt(count());
175: DxIterator it = iterator();
176: Object obj;
177: while ((obj = it.next()) != null) {
178: out.writeObject(obj);
179: out.writeObject(it.key());
180: }
181: }
182:
183: public synchronized void readExternal(ObjectInput in)
184: throws IOException, ClassNotFoundException {
185: int count = in.readInt();
186: for (; count > 0; count--) {
187: addForKey(in.readObject(), in.readObject());
188: }
189: }
190:
191: }
|