1 22 23 24 package com.mchange.v1.identicator; 25 26 import java.util.*; 27 import com.mchange.v1.util.*; 28 29 33 abstract class IdMap extends AbstractMap implements Map 34 { 35 Map inner; 36 Identicator id; 37 38 protected IdMap(Map inner, Identicator id) 39 { 40 this.inner = inner; 41 this.id = id; 42 } 43 44 public Object put(Object key, Object value) 45 { return inner.put( createIdKey( key ), value ); } 46 47 public boolean containsKey(Object key) 48 { return inner.containsKey( createIdKey( key ) ); } 49 50 public Object get(Object key) 51 { return inner.get( createIdKey( key ) ); } 52 53 public Object remove(Object key) 54 { return inner.remove( createIdKey( key ) ); } 55 56 protected Object removeIdHashKey( IdHashKey idhk ) 57 { return inner.remove( idhk ); } 58 59 public Set entrySet() 60 { return new UserEntrySet(); } 61 62 protected final Set internalEntrySet() 63 { return inner.entrySet(); } 64 65 protected abstract IdHashKey createIdKey(Object o); 66 67 protected final Entry createIdEntry(Object key, Object val) 68 { return new SimpleMapEntry( createIdKey( key ), val); } 69 70 protected final Entry createIdEntry(Entry entry) 71 { return createIdEntry( entry.getKey(), entry.getValue() ); } 72 73 private final class UserEntrySet extends AbstractSet 74 { 75 Set innerEntries = inner.entrySet(); 76 77 public Iterator iterator() 78 { 79 return new WrapperIterator(innerEntries.iterator(), true) 80 { 81 protected Object transformObject(Object o) 82 { return new UserEntry( (Entry) o ); } 83 }; 84 } 85 86 public int size() 87 { return innerEntries.size(); } 88 89 public boolean contains(Object o) 90 { 91 if (o instanceof Entry) 92 { 93 Entry entry = (Entry) o; 94 return innerEntries.contains( createIdEntry( entry ) ); 95 } 96 else 97 return false; 98 } 99 100 public boolean remove(Object o) 101 { 102 if (o instanceof Entry) 103 { 104 Entry entry = (Entry) o; 105 return innerEntries.remove( createIdEntry( entry ) ); 106 } 107 else 108 return false; 109 } 110 111 public void clear() 112 { inner.clear(); } 113 } 114 115 protected static class UserEntry extends AbstractMapEntry 116 { 117 private Entry innerEntry; 118 119 UserEntry(Entry innerEntry) 120 { this.innerEntry = innerEntry; } 121 122 public final Object getKey() 123 { return ((IdHashKey) innerEntry.getKey()).getKeyObj(); } 124 125 public final Object getValue() 126 { return innerEntry.getValue(); } 127 128 public final Object setValue(Object value) 129 { return innerEntry.setValue( value ); } 130 } 131 } 132 | Popular Tags |