1 9 package org.ozoneDB.DxLib; 10 11 import java.io.*; 12 13 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 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 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 e) { 54 throw new RuntimeException ( e.toString() ); 55 } 56 } 57 58 59 public DxCollection valueClone() { 60 throw new RuntimeException ( getClass().getName() + ".valueClone() not implemented." ); 61 } 62 63 64 67 public boolean equals( Object obj ) { 68 if (obj instanceof DxMultiMap && obj != null) { 69 DxMultiMap rhs = (DxMultiMap)obj; 70 71 if (this == obj) { 72 return true; 73 } 74 75 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 obj ) { 96 return addForKey( obj, buildKey( obj ) ); 97 } 98 99 100 public Object buildKey( Object obj ) { 101 return obj; 102 } 103 104 105 public synchronized boolean addForKey( Object obj, Object 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 e) { 116 throw new RuntimeException ( e.toString() ); 117 } 118 } 119 120 121 public DxCollection elementsForKey( Object key ) { 122 return (DxCollection)map.elementForKey( key ); 123 } 124 125 126 public Object elementForKey( Object key ) { 127 return map.elementForKey( key ); 128 } 129 130 131 public Object keyForElement( Object 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 removeForKey( Object 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 ( getClass().getName() + ".removeAllKeys() not implemented." ); 156 } 157 158 159 public boolean containsKey( Object 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 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 { 200 int count = in.readInt(); 201 for (; count > 0; count--) { 202 addForKey( in.readObject(), in.readObject() ); 203 } 204 } 205 206 } 207 | Popular Tags |