1 19 24 25 package org.netbeans.core.output2; 26 27 import java.lang.ref.WeakReference ; 28 import java.util.Arrays ; 29 30 37 class PairMap { 38 40 private String [] keys = new String [10]; 41 42 Object [] vals = new Object [10]; 43 int last = -1; 44 private boolean weak = false; 45 46 47 PairMap() { 48 } 49 50 public synchronized void clear() { 51 keys = new String [10]; 52 vals = new Object [10]; 53 last = -1; 54 } 55 56 public synchronized int size() { 57 if (weak) { 58 return prune(); 59 } else { 60 return last+1; 61 } 62 } 63 64 public synchronized boolean isEmpty() { 65 return size() == 0; 66 } 67 68 public synchronized void setWeak(boolean val) { 69 if (weak != val) { 70 weak = val; 71 for (int i=0; i <= last; i++) { 72 if (weak) { 73 vals[i] = new WeakReference (vals[i]); 74 } else { 75 vals[i] = ((WeakReference ) vals[i]).get(); 76 } 77 } 78 if (!weak) { 79 prune(); 80 } 81 } 82 } 83 84 public synchronized void add (String key, NbIO value) { 85 if (last == keys.length -1) { 86 growArrays(); 87 } 88 last++; 89 keys[last] = key; 90 vals[last] = value; 91 } 92 93 public synchronized NbIO get (String key, boolean mustBeClosed) { 94 if (last < 0) { 95 return null; 96 } 97 boolean foundNull = false; 98 for (int i=last; i >= 0; i--) { 99 if (keys[i].equals(key)) { 100 NbIO io = getValue(i); 101 foundNull |= io == null; 102 if (io != null && (!mustBeClosed || (mustBeClosed && io.isStreamClosed()))) { 103 return io; 104 } 105 } 106 } 107 if (foundNull) { 108 prune(); 109 } 110 return null; 111 } 112 113 public boolean containsValue(NbIO io) { 114 if (last < 0) { 115 return false; 116 } 117 for (int i=last; i >= 0; i--) { 118 if (getValue(i) == io) { 119 return true; 120 } 121 } 122 return false; 123 } 124 125 public synchronized NbIO get (String key) { 126 return get(key, false); 127 } 128 129 public synchronized String remove (NbIO io) { 130 int idx = indexOfVal (io); 131 if (idx == -1) { 132 return null; 133 } 134 String result = keys[idx]; 135 removeIndex(idx); 136 return result; 137 } 138 139 public synchronized NbIO remove (String key) { 140 int idx = indexOfKey(key); 141 if (idx == -1) { 142 return null; 143 } 144 NbIO result = getValue(idx); 145 removeIndex(idx); 146 return result; 147 } 148 149 private NbIO getValue(int idx) { 150 if (idx > last) { 151 throw new ArrayIndexOutOfBoundsException ("Tried to fetch item " + idx + " but map only contains " + (last + 1) + " elements"); } 154 NbIO result; 155 Object o = vals[idx]; 156 if (weak) { 157 result = (NbIO) ((WeakReference ) vals[idx]).get(); 158 if (result == null && !pruning) { 159 removeIndex(idx); 160 } 161 162 } else { 163 result = (NbIO) o; 164 } 165 return result; 166 } 167 168 public void setValue (int idx, NbIO value) { 169 if (weak) { 170 vals[idx] = new WeakReference (value); 171 } else { 172 vals[idx] = value; 173 } 174 } 175 176 private void removeIndex (int idx) { 177 if (idx < 0 || idx > last) { 178 throw new ArrayIndexOutOfBoundsException ("Trying to remove " + "element " + idx + " but map only contains " + (last+1) + " elements"); } 182 if (idx == last) { 183 keys[idx] = null; 184 vals[idx] = null; 185 } else { 186 keys[idx] = keys[last]; 187 vals[idx] = vals[last]; 188 vals[last] = null; 189 keys[last] = null; 190 } 191 last--; 192 } 193 194 private int indexOfKey (String key) { 195 for (int i=last; i >= 0; i--) { 196 if (keys[i].equals(key)) { 197 return i; 198 } 199 } 200 return -1; 201 } 202 203 private int indexOfVal (NbIO val) { 204 for (int i=last; i >= 0; i--) { 205 if (vals[i] == val) { 206 return i; 207 } 208 } 209 return -1; 210 } 211 212 private void growArrays() { 213 String [] newKeys = new String [keys.length * 2]; 214 NbIO[] newVals = new NbIO[vals.length * 2]; 215 System.arraycopy (keys, 0, newKeys, 0, last+1); 216 System.arraycopy (vals, 0, newVals, 0, last+1); 217 keys = newKeys; 218 vals = newVals; 219 } 220 221 private boolean pruning = false; 222 private int prune() { 223 pruning = true; 224 int oldSize = last + 1; 225 int result = oldSize; 226 int[] removes = new int[oldSize]; 227 Arrays.fill (removes, -1); 228 for (int i=last; i >= 0; i--) { 229 if (getValue(i) == null) { 230 removes[i] = i; 231 result--; 232 } 233 } 234 235 if (result != oldSize) { 236 for (int i=removes.length-1; i >= 0; i--) { 237 if (removes[i] != -1) { 238 removeIndex(removes[i]); 239 } 240 } 241 } 242 pruning = false; 243 return result; 244 } 245 246 } 247 | Popular Tags |