1 19 20 package org.netbeans.modules.java.bridge; 21 22 import java.beans.PropertyChangeEvent ; 23 import java.util.*; 24 25 30 public abstract class FlyweightIndexedProperty extends IndexedPropertyBase { 31 public FlyweightIndexedProperty(String propertyName) { 32 super(propertyName); 33 } 34 35 protected abstract Object [] getValue(ElementImpl beanImpl); 36 37 protected abstract Object [] createValue(int size); 38 39 42 public PropertyChangeEvent add(ElementImpl beanImpl, Object [] adding) { 43 Object [] oldVal = getValue(beanImpl); 44 Object [] newVal = createValue(oldVal.length + adding.length); 45 System.arraycopy(oldVal, 0, newVal, 0, oldVal.length); 46 System.arraycopy(adding, 0, newVal, oldVal.length, adding.length); 47 return createInsertionEvent(beanImpl.getElement(), oldVal, newVal.clone(), Arrays.asList(adding), 48 null); 49 } 50 51 53 public PropertyChangeEvent remove(ElementImpl beanImpl, Object [] toRemove) { 54 Object [] oldVal = getValue(beanImpl); 55 Object [] newVal = applyRemove(oldVal, toRemove); 56 return createRemovalEvent(beanImpl.getElement(), oldVal, newVal.clone(), Arrays.asList(toRemove), null); 57 } 58 59 protected boolean compareValuesForRemove(Object old, Object removed) { 60 return compareValues(old, removed); 61 } 62 63 66 private Object [] applyRemove(Object [] oldVals, Object [] remove) { 67 List result = new ArrayList(oldVals.length - remove.length); 68 for (int i = 0; i < oldVals.length; i++) { 69 boolean shouldRemove = false; 70 for (int j = 0; !shouldRemove && j < remove.length; j++) { 71 if (oldVals[i] == remove[j] || compareValuesForRemove(oldVals[i], remove[j])) { 72 shouldRemove = true; 73 } 74 } 75 if (!shouldRemove) 76 result.add(oldVals[i]); 77 } 78 return result.toArray(createValue(0)); 79 } 80 81 83 public PropertyChangeEvent set(ElementImpl beanImpl, Object [] newVal) { 84 Object [] oldVal = getValue(beanImpl); 85 return createChangeEvent(beanImpl.getElement(), oldVal, (Object [])newVal.clone()); 86 } 87 } 88 | Popular Tags |