1 11 package org.eclipse.ant.internal.ui.dtd.util; 12 13 import java.util.Comparator ; 14 15 22 public class SortedSet { 23 protected Comparator fComp; 24 protected IKeyHolder fKeyHolder; 25 protected SortedSet fNext; 26 27 public SortedSet(IKeyHolder holder, Comparator comp) { 28 fComp = comp; 29 fKeyHolder = holder; 30 } 31 36 public SortedSet(Comparator comp) { 37 fComp = comp; 38 } 39 43 public SortedSet(IKeyHolder holder) { 44 fKeyHolder = holder; 45 } 46 52 public SortedSet() { 53 } 54 public void setKeyHolder(IKeyHolder holder) { 55 fKeyHolder = holder; 56 } 57 public void setComparator(Comparator comp) { 58 fComp = comp; 59 } 60 66 public boolean add(Object obj) { 67 return internalAdd(obj, false) >= 0; 68 } 69 protected int internalAdd(Object obj, boolean always) { 70 Object [] array = fKeyHolder.getKeys(); 71 if (array == null) { 72 array = new Object [1]; 73 fKeyHolder.setKeys(array); 74 array[0] = obj; 75 return 0; 76 } 77 int i = 0; 78 int comp = -1; 79 80 for (; i < array.length; i++) { 81 if ((comp = fComp.compare(obj, array[i])) <= 0) { 82 break; 83 } 84 } 85 if (comp == 0 && !always) 86 return -1; 87 internalAdd(i, obj); 88 return i; 89 } 90 protected void internalAdd(int i, Object obj) { 91 Object [] array = fKeyHolder.getKeys(); 92 if (array == null) { 93 array = new Object [1]; 94 array[0] = obj; 95 fKeyHolder.setKeys(array); 96 } 97 else { 98 Object [] tmp = new Object [array.length+1]; 99 System.arraycopy(array,0,tmp,0,i); 100 tmp[i] = obj; 101 System.arraycopy(array,i,tmp,i+1,array.length-i); 102 fKeyHolder.setKeys(tmp); 103 } 104 } 105 110 public int addAlways(Object obj) { 111 return internalAdd(obj, true); 112 } 113 118 public void append(Object obj) { 119 Object [] array = fKeyHolder.getKeys(); 120 int len = array != null ? array.length : 0; 121 internalAdd(len, obj); 122 } 123 public boolean contains(Object obj) { 124 return indexOf(obj) >= 0; 125 } 126 public int indexOf(Object obj) { 127 Object [] array = fKeyHolder.getKeys(); 128 if (array == null) 129 return -1; 130 for (int i = 0; i < array.length; i++) { 131 int comp = fComp.compare(obj, array[i]); 132 if (comp == 0) 133 return i; 134 if (comp < 0) 135 return -1; 136 } 137 return -1; 138 } 139 public boolean containsIdentity(Object obj) { 140 return indexOf(obj) >= 0; 141 } 142 public int indexOfIdentity(Object obj) { 143 Object [] array = fKeyHolder.getKeys(); 144 if (array == null) 145 return -1; 146 for (int i = 0; i < array.length; i++) { 147 if (obj == array[i]) 148 return i; 149 } 150 return -1; 151 } 152 public boolean equals(Object o) { 153 if (this == o) 154 return true; 155 if (!(o instanceof SortedSet)) 156 return false; 157 SortedSet other = (SortedSet) o; 158 Object [] array = fKeyHolder.getKeys(); 159 Object [] otherarray = other.fKeyHolder.getKeys(); 160 if ((array == null) != (otherarray == null)) 161 return false; 162 if (array == null) 163 return true; 164 if (array.length != otherarray.length) 165 return false; 166 for (int i = 0; i < array.length; i++) { 167 if (array[i] != otherarray[i]) 168 return false; 169 } 170 return true; 171 } 172 public boolean equalsIdentify(Object o) { 173 if (this == o) 174 return true; 175 if (!(o instanceof SortedSet)) 176 return false; 177 SortedSet other = (SortedSet) o; 178 Object [] array = fKeyHolder.getKeys(); 179 Object [] otherarray = other.fKeyHolder.getKeys(); 180 if ((array == null) != (otherarray == null)) 181 return false; 182 if (array == null) 183 return true; 184 if (array.length != otherarray.length) 185 return false; 186 for (int i = 0; i < array.length; i++) { 187 if (fComp.compare(array[i], otherarray[i]) != 0) 188 return false; 189 } 190 return true; 191 } 192 public void merge(SortedSet other) { 193 Object [] array = fKeyHolder.getKeys(); 194 Object [] otherarray = other.fKeyHolder.getKeys(); 195 if (otherarray == null) 196 return; 197 if (array == null) { 198 array = otherarray; 199 return; 200 } 201 int ithis = 0, iother = 0, i = 0; 202 int mthis = array.length, mother = otherarray.length; 203 Object [] tmp = new Object [mthis+mother]; 204 while (ithis < mthis && iother < mother) { 205 int comp = fComp.compare(array[ithis], otherarray[iother]); 206 if (comp <= 0) { 207 tmp[i++] = array[ithis++]; 208 } 209 else { 210 tmp[i++] = otherarray[iother++]; 211 } 212 } 213 while (ithis < mthis) { 214 tmp[i++] = array[ithis++]; 215 } 216 while (iother < mother) { 217 tmp[i++] = otherarray[iother++]; 218 } 219 } 220 public Object [] members() { 221 Object [] array = fKeyHolder.getKeys(); 222 if (array == null) 223 return new Object [0]; 224 return array; 225 } 226 public int size() { 227 Object [] array = fKeyHolder.getKeys(); 228 return array == null ? 0 : array.length; 229 } 230 public void remove(int i) { 231 Object [] array = fKeyHolder.getKeys(); 232 Object [] tmp = new Object [array.length-1]; 233 System.arraycopy(array,0,tmp,0,i); 234 System.arraycopy(array,i+1,tmp,i,array.length-i-1); 235 fKeyHolder.setKeys(tmp); 236 } 237 public boolean remove(Object obj) { 238 int i = indexOf(obj); 239 if (i >= 0) { 240 remove(i); 241 return true; 242 } 243 return false; 244 } 245 public boolean removeIdentity(Object obj) { 246 int i = indexOfIdentity(obj); 247 if (i >= 0) { 248 remove(i); 249 return true; 250 } 251 return false; 252 } 253 public SortedSet getNextSet() { 254 return fNext; 255 } 256 public void setNextSet(SortedSet next) { 257 fNext = next; 258 } 259 260 } 261 | Popular Tags |