1 29 30 package nextapp.echo2.app; 31 32 import java.io.Serializable ; 33 import java.util.Iterator ; 34 import java.util.SortedMap ; 35 import java.util.TreeMap ; 36 37 46 public class MutableStyle 47 implements Style { 48 49 private static final int GROW_RATE = 5 * 2; 51 private static final Object [] EMPTY = new Object [0]; 52 53 57 private class PropertyNameIterator 58 implements Iterator { 59 60 private int index = 0; 61 62 65 public boolean hasNext() { 66 return index < length; 67 } 68 69 72 public Object next() { 73 Object value = data[index]; 74 index += 2; 75 return value; 76 } 77 78 81 public void remove() { 82 throw new UnsupportedOperationException (); 83 } 84 } 85 86 89 public class IndexedPropertyValue 90 implements Serializable { 91 92 private SortedMap indicesToValues; 93 94 100 public Object getValue(int index) { 101 if (indicesToValues == null) { 102 return null; 103 } else { 104 return indicesToValues.get(new Integer (index)); 105 } 106 } 107 108 114 public Iterator getIndices() { 115 return indicesToValues.keySet().iterator(); 116 } 117 118 124 public boolean hasValue(int index) { 125 return indicesToValues != null && indicesToValues.containsKey(new Integer (index)); 126 } 127 128 133 private void removeValue(int index) { 134 if (indicesToValues != null) { 135 indicesToValues.remove(new Integer (index)); 136 if (indicesToValues.size() == 0) { 137 indicesToValues = null; 138 } 139 } 140 } 141 142 148 private void setValue(int index, Object value) { 149 if (indicesToValues == null) { 150 indicesToValues = new TreeMap (); 151 } 152 indicesToValues.put(new Integer (index), value); 153 } 154 } 155 156 private Object [] data = EMPTY; 157 int length = 0; 159 162 public MutableStyle() { 163 super(); 164 } 165 166 171 public void addStyleContent(Style style) { 172 Iterator nameIt = style.getPropertyNames(); 173 while (nameIt.hasNext()) { 174 String name = (String ) nameIt.next(); 175 Object value = style.getProperty(name); 176 if (value instanceof IndexedPropertyValue) { 177 IndexedPropertyValue indexedPropertyValue = (IndexedPropertyValue) value; 178 Iterator indexIt = indexedPropertyValue.getIndices(); 179 while (indexIt.hasNext()) { 180 int index = ((Integer ) indexIt.next()).intValue(); 181 setIndexedProperty(name, index, indexedPropertyValue.getValue(index)); 182 } 183 } else { 184 setProperty(name, value); 185 } 186 } 187 } 188 189 192 public Object getIndexedProperty(String propertyName, int propertyIndex) { 193 Object value = retrieveProperty(propertyName); 194 if (!(value instanceof IndexedPropertyValue)) { 195 return null; 196 } 197 return ((IndexedPropertyValue) value).getValue(propertyIndex); 198 } 199 200 203 public Object getProperty(String propertyName) { 204 return retrieveProperty(propertyName); 205 } 206 207 210 public Iterator getPropertyIndices(String propertyName) { 211 Object value = getProperty(propertyName); 212 if (!(value instanceof IndexedPropertyValue)) { 213 return null; 214 } 215 return ((IndexedPropertyValue) value).getIndices(); 216 } 217 218 221 public Iterator getPropertyNames() { 222 return new PropertyNameIterator(); 223 } 224 225 228 public boolean isIndexedPropertySet(String propertyName, int index) { 229 Object value = retrieveProperty(propertyName); 230 if (!(value instanceof IndexedPropertyValue)) { 231 return false; 232 } 233 return ((IndexedPropertyValue) value).hasValue(index); 234 } 235 236 239 public boolean isPropertySet(String propertyName) { 240 int propertyNameHashCode = propertyName.hashCode(); 241 for (int i = 0; i < length; i += 2) { 242 if (propertyNameHashCode == data[i].hashCode() && propertyName.equals(data[i])) { 243 return true; 244 } 245 } 246 return false; 247 } 248 249 255 public void removeIndexedProperty(String propertyName, int propertyIndex) { 256 Object value = retrieveProperty(propertyName); 257 if (!(value instanceof IndexedPropertyValue)) { 258 return; 259 } 260 ((IndexedPropertyValue) value).removeValue(propertyIndex); 261 } 262 263 268 public void removeProperty(String propertyName) { 269 int propertyNameHashCode = propertyName.hashCode(); 270 for (int i = 0; i < length; i += 2) { 271 if (propertyNameHashCode == data[i].hashCode() && propertyName.equals(data[i])) { 272 data[i] = data[length - 2]; 273 data[i + 1] = data[length - 1]; 274 data[length - 2] = null; 275 data[length - 1] = null; 276 length -= 2; 277 break; 278 } 279 } 280 281 if (length == 0) { 282 data = EMPTY; 283 } 284 } 285 286 292 private Object retrieveProperty(String propertyName) { 293 int propertyNameHashCode = propertyName.hashCode(); 294 for (int i = 0; i < length; i += 2) { 295 if (propertyNameHashCode == data[i].hashCode() && propertyName.equals(data[i])) { 296 return data[i + 1]; 297 } 298 } 299 return null; 300 } 301 302 309 public void setIndexedProperty(String propertyName, int propertyIndex, Object propertyValue) { 310 Object value = retrieveProperty(propertyName); 311 if (!(value instanceof IndexedPropertyValue)) { 312 value = new IndexedPropertyValue(); 313 setProperty(propertyName, value); 314 } 315 ((IndexedPropertyValue) value).setValue(propertyIndex, propertyValue); 316 } 317 318 326 public void setProperty(String propertyName, Object propertyValue) { 327 if (propertyValue == null) { 328 removeProperty(propertyName); 329 return; 330 } 331 332 if (data == EMPTY) { 333 data = new Object [GROW_RATE]; 334 } 335 336 int propertyNameHashCode = propertyName.hashCode(); 337 for (int i = 0; i < data.length; i += 2) { 338 if (data[i] == null) { 339 data[i] = propertyName; 342 data[i + 1] = propertyValue; 343 length += 2; 344 return; 345 } 346 if (propertyNameHashCode == data[i].hashCode() && propertyName.equals(data[i])) { 347 data[i + 1] = propertyValue; 349 return; 350 } 351 } 352 353 Object [] newData = new Object [data.length + GROW_RATE]; 355 System.arraycopy(data, 0, newData, 0, data.length); 356 357 newData[data.length] = propertyName; 358 newData[data.length + 1] = propertyValue; 359 length += 2; 360 data = newData; 361 } 362 363 368 public int size() { 369 return length / 2; 370 } 371 372 377 public String toString() { 378 StringBuffer out = new StringBuffer ("MutableStyle {"); 379 for (int i = 0; i < length; i += 2) { 380 out.append(data[i]); 381 out.append("="); 382 out.append(data[i + 1]); 383 if (i < length - 2) { 384 out.append(", "); 385 } 386 } 387 out.append("}"); 388 return out.toString(); 389 } 390 } 391 | Popular Tags |