1 9 package org.ozoneDB.DxLib; 10 11 import java.io.*; 12 13 19 public abstract class DxAbstractMap extends DxAbstractCollection implements DxMap, Externalizable { 20 21 final static long serialVersionUID = 1L; 22 23 24 public Object clone( DxMap newMap ) { 25 try { 26 DxIterator it = iterator(); 27 Object obj; 28 while ((obj = it.next()) != null) { 29 newMap.addForKey( obj, it.key() ); 30 } 31 return newMap; 32 } catch (Exception e) { 33 throw new RuntimeException ( e.toString() ); 34 } 35 } 36 37 38 public DxCollection valueClone() { 39 throw new RuntimeException ( getClass().getName() + ".valueClone() not implemented." ); 40 } 41 42 43 47 public boolean equals( Object obj ) { 48 if (obj != null && obj instanceof DxMap) { 49 DxMap rhs = (DxMap)obj; 50 if (this == obj) { 51 return true; 52 } 53 Object keySet = keySet(); 55 return keySet.equals( rhs.keySet() ); 56 } else { 57 return false; 58 } 59 } 60 61 62 public synchronized boolean add( Object 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 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 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 103 public Object buildKey( Object obj ) { 104 return obj; 105 } 107 108 109 public boolean containsKey( Object 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 out.writeInt( count() ); 134 DxIterator it = iterator(); 135 Object 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 { 144 int count = in.readInt(); 146 for (; count > 0; count--) { 147 addForKey( in.readObject(), in.readObject() ); 148 } 149 } 150 151 } 152 | Popular Tags |