00001 package ca.mcgill.sable.util;
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069 import ca.mcgill.sable.util.Map.Entry;
00070
00071 public class TypedSplayTreeMap extends SplayTreeMap
00072 {
00073 private Cast keyCast;
00074 private Cast valueCast;
00075 private Collection entries;
00076
00077 private class EntryCollection extends AbstractCollection
00078 {
00079 private Collection collection;
00080
00081 EntryCollection(Collection collection)
00082 {
00083 this.collection = collection;
00084 }
00085
00086 public int size()
00087 {
00088 return collection.size();
00089 }
00090
00091 public Iterator iterator()
00092 {
00093 return new EntryIterator(collection.iterator());
00094 }
00095 }
00096
00097 private class EntryIterator implements Iterator
00098 {
00099 private Iterator iterator;
00100
00101 EntryIterator(Iterator iterator)
00102 {
00103 this.iterator = iterator;
00104 }
00105
00106 public boolean hasNext()
00107 {
00108 return iterator.hasNext();
00109 }
00110
00111 public Object next()
00112 {
00113 return new TypedEntry((Entry) iterator.next());
00114 }
00115
00116 public void remove()
00117 {
00118 iterator.remove();
00119 }
00120 }
00121
00122 private class TypedEntry extends AbstractEntry
00123 {
00124 private Entry entry;
00125
00126 TypedEntry(Entry entry)
00127 {
00128 this.entry = entry;
00129 }
00130
00131 public Object getKey()
00132 {
00133 return entry.getKey();
00134 }
00135
00136 public Object getValue()
00137 {
00138 return entry.getValue();
00139 }
00140
00141 public Object setValue(Object value)
00142 {
00143 return entry.setValue(valueCast.cast(value));
00144 }
00145 }
00146 public TypedSplayTreeMap()
00147 {
00148 super();
00149
00150 keyCast = NoCast.instance;
00151 valueCast = NoCast.instance;
00152 }
00153 public TypedSplayTreeMap(Cast keyCast, Cast valueCast)
00154 {
00155 super();
00156
00157 this.keyCast = keyCast;
00158 this.valueCast = valueCast;
00159 }
00160 public TypedSplayTreeMap(Comparator comparator)
00161 {
00162 super(comparator);
00163
00164 keyCast = NoCast.instance;
00165 valueCast = NoCast.instance;
00166 }
00167 public TypedSplayTreeMap(Comparator comparator, Cast keyCast, Cast valueCast)
00168 {
00169 super(comparator);
00170
00171 this.keyCast = keyCast;
00172 this.valueCast = valueCast;
00173 }
00174 public TypedSplayTreeMap(Map map)
00175 {
00176 super(map);
00177
00178 keyCast = NoCast.instance;
00179 valueCast = NoCast.instance;
00180 }
00181 public TypedSplayTreeMap(Map map, Cast keyCast, Cast valueCast)
00182 {
00183 super(map);
00184
00185 this.keyCast = keyCast;
00186 this.valueCast = valueCast;
00187 }
00188 public TypedSplayTreeMap(Map map, Comparator comparator)
00189 {
00190 super(map, comparator);
00191
00192 keyCast = NoCast.instance;
00193 valueCast = NoCast.instance;
00194 }
00195 public TypedSplayTreeMap(Map map, Comparator comparator, Cast keyCast, Cast valueCast)
00196 {
00197 super(map, comparator);
00198
00199 this.keyCast = keyCast;
00200 this.valueCast = valueCast;
00201 }
00202 public Object clone()
00203 {
00204 return new TypedSplayTreeMap(this, getComparator(), keyCast, valueCast);
00205 }
00206 public Collection entries()
00207 {
00208 if(entries == null)
00209 {
00210 entries = new EntryCollection(super.entries());
00211 }
00212
00213 return entries;
00214 }
00215 public Cast getKeyCast()
00216 {
00217 return keyCast;
00218 }
00219 public Cast getValueCast()
00220 {
00221 return valueCast;
00222 }
00223 public Object put(Object key, Object value)
00224 {
00225 return super.put(keyCast.cast(key), valueCast.cast(value));
00226 }
00227 }