KickJava   Java API By Example, From Geeks To Geeks.

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


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: DxAbstractMap.java,v 1.9 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.9 $Date: 2000/10/28 16:55:14 $
18  */

19 public abstract class DxAbstractMap extends DxAbstractCollection implements DxMap, Externalizable {
20     
21     final static long serialVersionUID = 1L;
22     
23     
24     public Object JavaDoc clone( DxMap newMap ) {
25         try {
26             DxIterator it = iterator();
27             Object JavaDoc obj;
28             while ((obj = it.next()) != null) {
29                 newMap.addForKey( obj, it.key() );
30             }
31             return newMap;
32         } catch (Exception JavaDoc e) {
33             throw new RuntimeException JavaDoc( e.toString() );
34         }
35     }
36     
37     
38     public DxCollection valueClone() {
39         throw new RuntimeException JavaDoc( getClass().getName() + ".valueClone() not implemented." );
40     }
41     
42     
43     /**
44      * Compares two maps for equality. Returns true if the keySets are
45      * equal.
46      */

47     public boolean equals( Object JavaDoc obj ) {
48         if (obj != null && obj instanceof DxMap) {
49             DxMap rhs = (DxMap)obj;
50             if (this == obj) {
51                 return true;
52             }
53             // FIXME: direct call to DxSet.equlas() is "ambigous"...
54
Object JavaDoc keySet = keySet();
55             return keySet.equals( rhs.keySet() );
56         } else {
57             return false;
58         }
59     }
60     
61     
62     public synchronized boolean add( Object JavaDoc obj ) {
63         return addForKey( obj, buildKey( obj ) );
64     }
65     
66     
67     public synchronized boolean addAll( DxCollection coll ) {
68         if (coll instanceof DxMap) {
69             boolean answer = false;
70             DxIterator it = coll.iterator();
71             Object JavaDoc obj;
72             while ((obj = it.next()) != null) {
73                 if (addForKey( obj, it.key() )) {
74                     answer = true;
75                 }
76             }
77             return answer;
78         } else {
79             return super.addAll( coll );
80         }
81     }
82     
83     
84     public synchronized boolean removeAllKeys( DxCollection coll ) {
85         boolean answer = false;
86         if (!coll.isEmpty() && !isEmpty()) {
87             DxIterator it = coll.iterator();
88             Object JavaDoc key;
89             while ((key = it.next()) != null) {
90                 if (removeForKey( key ) != null) {
91                     answer = true;
92                 }
93             }
94         }
95         return answer;
96     }
97     
98     
99     /**
100      * This method is not declared abstract because it does not need to
101      * be implemented in any case.
102      */

103     public Object JavaDoc buildKey( Object JavaDoc obj ) {
104         return obj;
105     // throw new RuntimeException ("buildKey(): subclass responsibility");
106
}
107     
108     
109     public boolean containsKey( Object JavaDoc key ) {
110         return elementForKey( key ) != null;
111     }
112     
113     
114     public DxSet keySet() {
115         DxSet answer = new DxHashSet();
116         DxIterator it = iterator();
117         while (it.next() != null) {
118             answer.add( it.key() );
119         }
120         return answer;
121     }
122     
123     
124     public DxSet elementSet() {
125         DxSet answer = new DxHashSet();
126         answer.addAll( this );
127         return answer;
128     }
129     
130     
131     public void writeExternal( ObjectOutput out ) throws IOException {
132         // System.out.println (getClass().getName() + ".writeExternal()...");
133
out.writeInt( count() );
134         DxIterator it = iterator();
135         Object JavaDoc obj;
136         while ((obj = it.next()) != null) {
137             out.writeObject( obj );
138             out.writeObject( it.key() );
139         }
140     }
141     
142     
143     public synchronized void readExternal( ObjectInput in ) throws IOException, ClassNotFoundException JavaDoc {
144         // System.out.println ("abstract.readExternal()...");
145
int count = in.readInt();
146         for (; count > 0; count--) {
147             addForKey( in.readObject(), in.readObject() );
148         }
149     }
150     
151 }
152
Popular Tags