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: DxAbstractMap.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 abstract class DxAbstractMap extends DxAbstractCollection
020: implements DxMap, Externalizable {
021:
022: final static long serialVersionUID = 1L;
023:
024: public Object clone(DxMap newMap) {
025: try {
026: DxIterator it = iterator();
027: Object obj;
028: while ((obj = it.next()) != null) {
029: newMap.addForKey(obj, it.key());
030: }
031: return newMap;
032: } catch (Exception e) {
033: throw new RuntimeException(e.toString());
034: }
035: }
036:
037: public DxCollection valueClone() {
038: throw new RuntimeException(getClass().getName()
039: + ".valueClone() not implemented.");
040: }
041:
042: /**
043: * Compares two maps for equality. Returns true if the keySets are
044: * equal.
045: */
046: public boolean equals(Object obj) {
047: if (obj != null && obj instanceof DxMap) {
048: DxMap rhs = (DxMap) obj;
049: if (this == obj) {
050: return true;
051: }
052: // FIXME: direct call to DxSet.equlas() is "ambigous"...
053: Object keySet = keySet();
054: return keySet.equals(rhs.keySet());
055: } else {
056: return false;
057: }
058: }
059:
060: public synchronized boolean add(Object obj) {
061: return addForKey(obj, buildKey(obj));
062: }
063:
064: public synchronized boolean addAll(DxCollection coll) {
065: if (coll instanceof DxMap) {
066: boolean answer = false;
067: DxIterator it = coll.iterator();
068: Object obj;
069: while ((obj = it.next()) != null) {
070: if (addForKey(obj, it.key())) {
071: answer = true;
072: }
073: }
074: return answer;
075: } else {
076: return super .addAll(coll);
077: }
078: }
079:
080: public synchronized boolean removeAllKeys(DxCollection coll) {
081: boolean answer = false;
082: if (!coll.isEmpty() && !isEmpty()) {
083: DxIterator it = coll.iterator();
084: Object key;
085: while ((key = it.next()) != null) {
086: if (removeForKey(key) != null) {
087: answer = true;
088: }
089: }
090: }
091: return answer;
092: }
093:
094: /**
095: * This method is not declared abstract because it does not need to
096: * be implemented in any case.
097: */
098: public Object buildKey(Object obj) {
099: return obj;
100: // throw new RuntimeException ("buildKey(): subclass responsibility");
101: }
102:
103: public boolean containsKey(Object key) {
104: return elementForKey(key) != null;
105: }
106:
107: public DxSet keySet() {
108: DxSet answer = new DxHashSet();
109: DxIterator it = iterator();
110: while (it.next() != null) {
111: answer.add(it.key());
112: }
113: return answer;
114: }
115:
116: public DxSet elementSet() {
117: DxSet answer = new DxHashSet();
118: answer.addAll(this );
119: return answer;
120: }
121:
122: public void writeExternal(ObjectOutput out) throws IOException {
123: // System.out.println (getClass().getName() + ".writeExternal()...");
124: out.writeInt(count());
125: DxIterator it = iterator();
126: Object obj;
127: while ((obj = it.next()) != null) {
128: out.writeObject(obj);
129: out.writeObject(it.key());
130: }
131: }
132:
133: public synchronized void readExternal(ObjectInput in)
134: throws IOException, ClassNotFoundException {
135: // System.out.println ("abstract.readExternal()...");
136: int count = in.readInt();
137: for (; count > 0; count--) {
138: addForKey(in.readObject(), in.readObject());
139: }
140: }
141:
142: }
|