1 11 package org.eclipse.ant.internal.ui.dtd.util; 12 13 import com.ibm.icu.text.MessageFormat; 14 import java.util.Comparator ; 15 import java.util.Iterator ; 16 17 20 public class SortedMap implements FactoryObject { 21 22 private SortedSet fSet; 23 private IMapHolder fHolder; 24 private SortedMap fNext; 25 26 public SortedMap(IMapHolder holder, Comparator comp) { 27 fHolder = holder; 28 fSet = new SortedSet(holder, comp); 29 } 30 31 public SortedMap(Comparator comp) { 32 fSet = new SortedSet(comp); 33 } 34 35 public SortedMap(IMapHolder holder) { 36 fHolder = holder; 37 fSet = new SortedSet(holder); 38 } 39 40 public SortedMap() { 41 fSet = new SortedSet(); 42 } 43 44 public void setMapHolder(IMapHolder holder) { 45 fHolder = holder; 46 fSet.setKeyHolder(holder); 47 } 48 49 public void setComparator(Comparator comp) { 50 fSet.setComparator(comp); 51 } 52 53 public boolean containsKey(Object key) { 54 return fSet.contains(key); 55 } 56 57 public boolean containsKeyIdentity(Object key) { 58 return fSet.containsIdentity(key); 59 } 60 61 public Object put(Object key, Object val) { 62 Object [] values = fHolder.getValues(); 63 int index = fSet.indexOf(key); 64 Object result = index >= 0 && values != null ? values[index] : null; 65 66 int i = fSet.internalAdd(key, false); 67 if (i >= 0) 68 internalPut(i, val); 69 return result; 70 } 71 72 protected void internalPut(int i, Object val) { 73 Object [] values = fHolder.getValues(); 74 if (values == null) { 75 values = new Object [1]; 76 values[0] = val; 77 return; 78 } 79 80 Object [] tmp = new Object [values.length+1]; 81 System.arraycopy(values,0,tmp,0,i); 82 tmp[i] = val; 83 System.arraycopy(values,i,tmp,i+1,values.length-i); 84 fHolder.setValues(tmp); 85 } 86 87 public int putAlways(Object key, Object val) { 88 int i = fSet.internalAdd(key, true); 89 internalPut(i, val); 90 return i; 91 } 92 93 public void append(Object key, Object val) { 94 Object [] values = fHolder.getValues(); 95 int len = values != null ? values.length : 0; 96 fSet.internalAdd(len, key); 97 internalPut(len, val); 98 } 99 100 public Object get(Object key) { 101 Object [] values = fHolder.getValues(); 102 if (values == null) 103 return null; 104 int i = fSet.indexOf(key); 105 if (i >= 0) 106 return values[i]; 107 return null; 108 } 109 110 public Object getIdentity(Object key) { 111 Object [] values = fHolder.getValues(); 112 if (values == null) 113 return null; 114 int i = fSet.indexOfIdentity(key); 115 if (i >= 0) 116 return values[i]; 117 return null; 118 } 119 120 public Object [] keys() { 121 return fSet.members(); 122 } 123 124 public Object [] values() { 125 Object [] values = fHolder.getValues(); 126 if (values == null) 127 return new Object [0]; 128 return values; 129 } 130 131 public Iterator keyIterator() { 132 return new ArrayIterator(); 133 } 134 135 public Iterator valueIterator() { 136 return new ArrayIterator(); 137 } 138 139 private class ArrayIterator implements Iterator { 140 private int fIndex; 141 142 public ArrayIterator() { 143 fIndex = -1; 144 } 145 148 public boolean hasNext() { 149 Object [] array = SortedMap.this.fHolder.getKeys(); 150 if (array == null) 151 return false; 152 return fIndex + 1 < array.length; 153 } 154 155 158 public Object next() { 159 Object [] array = SortedMap.this.fHolder.getKeys(); 160 if (array == null) 161 throw new IllegalStateException (AntDTDUtilMessages.SortedMap_next___called_for_empty_array_1); 162 return array[++fIndex]; 163 } 164 165 168 public void remove() { 169 SortedMap.this.remove(fIndex); 170 --fIndex; 171 } 172 173 } 174 175 public void remove(int i) { 176 Object [] values = fHolder.getValues(); 177 if (values == null) { 178 throw new IllegalArgumentException (MessageFormat.format(AntDTDUtilMessages.SortedMap_remove__0___in_empty_map_2, new String []{Integer.toString(i)})); 179 } 180 fSet.remove(i); 181 Object [] tmp = new Object [values.length-1]; 182 System.arraycopy(values,0,tmp,0,i); 183 System.arraycopy(values,i+1,tmp,i,values.length-i-1); 184 fHolder.setValues(tmp); 185 } 186 187 public Object remove(Object obj) { 188 Object [] values = fHolder.getValues(); 189 if (values == null) 190 return null; 191 int i = fSet.indexOf(obj); 192 if (i >= 0) { 193 Object tmp = values[i]; 194 fSet.remove(i); 195 remove(i); 196 return tmp; 197 } 198 return null; 199 } 200 public Object removeIdentity(Object obj) { 201 Object [] values = fHolder.getValues(); 202 if (values == null) 203 return null; 204 int i = fSet.indexOfIdentity(obj); 205 if (i >= 0) { 206 Object tmp = values[i]; 207 fSet.remove(i); 208 remove(i); 209 return tmp; 210 } 211 return null; 212 } 213 214 public int size() { 215 return fSet.size(); 216 } 217 218 public int keyIndex(Object key) { 219 return fSet.indexOf(key); 220 } 221 222 public void merge(SortedMap other) { 223 Object [] values = fHolder.getValues(); 224 Object [] keys = fHolder.getKeys(); 225 Object [] othervalues = other.fHolder.getValues(); 226 Object [] otherkeys = other.fHolder.getKeys(); 227 if (otherkeys == null) 228 return; 229 if (keys == null) { 230 fHolder.setKeys(otherkeys); 231 fHolder.setValues(othervalues); 232 return; 233 } 234 int ithis = 0, iother = 0, i = 0; 235 int mthis = keys.length, mother = otherkeys.length; 236 Object [] ktmp = new Object [mthis+mother]; 237 Object [] vtmp = new Object [mthis+mother]; 238 while (ithis < mthis && iother < mother) { 239 int comp = fSet.fComp.compare(keys[ithis], otherkeys[iother]); 240 if (comp <= 0) { 241 vtmp[i] = values[ithis]; 242 ktmp[i++] = keys[ithis++]; 243 } 244 else { 245 vtmp[i] = othervalues[iother]; 246 ktmp[i++] = otherkeys[iother++]; 247 } 248 } 249 while (ithis < mthis) { 250 vtmp[i] = values[ithis]; 251 ktmp[i++] = keys[ithis++]; 252 } 253 while (iother < mother) { 254 vtmp[i] = othervalues[iother]; 255 ktmp[i++] = otherkeys[iother++]; 256 } 257 fHolder.setKeys(ktmp); 258 fHolder.setValues(vtmp); 259 } 260 261 public FactoryObject next() { 262 return fNext; 263 } 264 265 public void next(FactoryObject next) { 266 fNext = (SortedMap) next; 267 } 268 } 269 | Popular Tags |