KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ozoneDB > DxLib > DxMultiMap


1 // You can redistribute this software and/or modify it under the terms of
2
// the Ozone Library License version 1 published by ozone-db.org.
3
//
4
// The original code and portions created by SMB are
5
// Copyright (C) 1997-@year@ by SMB GmbH. All rights reserved.
6
//
7
// $Id: DxMultiMap.java,v 1.8 2000/10/28 16:55:14 daniela Exp $
8

9 package org.ozoneDB.DxLib;
10
11 import java.io.*;
12
13 /**
14  *
15  *
16  * @author <a HREF="http://www.softwarebuero.de/">SMB</a>
17  * @version $Revision: 1.8 $Date: 2000/10/28 16:55:14 $
18  */

19 public class DxMultiMap extends DxAbstractCollection implements DxMap, Externalizable {
20     
21     final static long serialVersionUID = 1L;
22     
23     protected transient DxMap map;
24     protected transient DxCollection containerFactory;
25     protected transient int multiItemCount = 0;
26     
27     
28     public DxMultiMap() {
29         map = new DxHashMap();
30         containerFactory = new DxArrayBag( 8 );
31     }
32     
33     
34     public DxMultiMap( DxMap _map, DxCollection _containerFactory ) {
35         map = _map;
36         containerFactory = _containerFactory;
37     }
38     
39     
40     public Object JavaDoc clone() {
41         try {
42             DxMultiMap answer = (DxMultiMap)getClass().newInstance();
43             answer.map = (DxMap)map.getClass().newInstance();
44             answer.containerFactory = (DxCollection)containerFactory.getClass().newInstance();
45             
46             DxIterator it = iterator();
47             Object JavaDoc obj;
48             while ((obj = it.next()) != null) {
49                 DxCollection newColl = (DxCollection)((DxCollection)obj).clone();
50                 answer.addForKey( newColl, it.key() );
51             }
52             return answer;
53         } catch (Exception JavaDoc e) {
54             throw new RuntimeException JavaDoc( e.toString() );
55         }
56     }
57     
58     
59     public DxCollection valueClone() {
60         throw new RuntimeException JavaDoc( getClass().getName() + ".valueClone() not implemented." );
61     }
62     
63     
64     /**
65      * Compares two multimaps for equality.
66      */

67     public boolean equals( Object JavaDoc obj ) {
68         if (obj instanceof DxMultiMap && obj != null) {
69             DxMultiMap rhs = (DxMultiMap)obj;
70             
71             if (this == obj) {
72                 return true;
73             }
74             
75             //FIXME: alle keys muessen mit gleichen containern da sein
76
return false;
77         } else {
78             return false;
79         }
80     }
81     
82     
83     public DxSet keySet() {
84         return map.keySet();
85     }
86     
87     
88     public DxSet elementSet() {
89         DxSet answer = new DxHashSet();
90         answer.add( this );
91         return answer;
92     }
93     
94     
95     public synchronized boolean add( Object JavaDoc obj ) {
96         return addForKey( obj, buildKey( obj ) );
97     }
98     
99     
100     public Object JavaDoc buildKey( Object JavaDoc obj ) {
101         return obj;
102     }
103     
104     
105     public synchronized boolean addForKey( Object JavaDoc obj, Object JavaDoc key ) {
106         try {
107             DxCollection coll = (DxCollection)map.elementForKey( key );
108             if (coll == null) {
109                 coll = (DxCollection)containerFactory.getClass().newInstance();
110                 map.addForKey( coll, key );
111             }
112             coll.add( obj );
113             multiItemCount++;
114             return true;
115         } catch (Exception JavaDoc e) {
116             throw new RuntimeException JavaDoc( e.toString() );
117         }
118     }
119     
120     
121     public DxCollection elementsForKey( Object JavaDoc key ) {
122         return (DxCollection)map.elementForKey( key );
123     }
124     
125     
126     public Object JavaDoc elementForKey( Object JavaDoc key ) {
127         return map.elementForKey( key );
128     }
129     
130     
131     public Object JavaDoc keyForElement( Object JavaDoc obj ) {
132         DxIterator it = iterator();
133         while (it.next() != null) {
134             DxIterator iit = iterator();
135             while (iit.next() != null) {
136                 if (obj.equals( iit.object() )) {
137                     return iit.key();
138                 }
139             }
140         }
141         return null;
142     }
143     
144     
145     public synchronized Object JavaDoc removeForKey( Object JavaDoc key ) {
146         DxCollection answer = (DxCollection)map.removeForKey( key );
147         if (answer != null) {
148             multiItemCount--;
149         }
150         return answer;
151     }
152     
153     
154     public boolean removeAllKeys( DxCollection coll ) {
155         throw new RuntimeException JavaDoc( getClass().getName() + ".removeAllKeys() not implemented." );
156     }
157     
158     
159     public boolean containsKey( Object JavaDoc key ) {
160         return map.containsKey( key );
161     }
162     
163     
164     /** */
165     public DxIterator iterator() {
166         return new DxMultiIterator( this );
167     }
168     
169     
170     /** */
171     public int count() {
172         return multiItemCount;
173     }
174     
175     
176     /** */
177     public boolean isEmpty() {
178         return multiItemCount == 0;
179     }
180     
181     
182     public synchronized void clear() {
183         map.clear();
184         multiItemCount = 0;
185     }
186     
187     
188     public void writeExternal( ObjectOutput out ) throws IOException {
189         out.writeInt( count() );
190         DxIterator it = iterator();
191         Object JavaDoc obj;
192         while ((obj = it.next()) != null) {
193             out.writeObject( obj );
194             out.writeObject( it.key() );
195         }
196     }
197     
198     
199     public synchronized void readExternal( ObjectInput in ) throws IOException, ClassNotFoundException JavaDoc {
200         int count = in.readInt();
201         for (; count > 0; count--) {
202             addForKey( in.readObject(), in.readObject() );
203         }
204     }
205     
206 }
207
Popular Tags