1 4 package com.tc.config.schema.dynamic; 5 6 import com.tc.util.Assert; 7 import com.tc.util.stringification.OurStringBuilder; 8 9 12 public abstract class DerivedConfigItem implements ConfigItem, ConfigItemListener { 13 14 private final ConfigItem[] derivedFrom; 15 private final CompoundConfigItemListener listener; 16 17 private Object currentValue; 18 19 public DerivedConfigItem(ConfigItem[] derivedFrom) { 20 Assert.assertNoNullElements(derivedFrom); 21 this.derivedFrom = derivedFrom; 22 23 for (int i = 0; i < derivedFrom.length; ++i) 24 derivedFrom[i].addListener(this); 25 26 this.listener = new CompoundConfigItemListener(); 27 this.currentValue = createValueFrom(this.derivedFrom); 28 } 29 30 public Object getObject() { 31 return currentValue; 32 } 33 34 protected abstract Object createValueFrom(ConfigItem[] fromWhich); 35 36 public void addListener(ConfigItemListener changeListener) { 37 this.listener.addListener(changeListener); 38 } 39 40 public void removeListener(ConfigItemListener changeListener) { 41 this.listener.removeListener(changeListener); 42 } 43 44 public void valueChanged(Object oldValue, Object newValue) { 45 Object ourOldValue = currentValue; 46 this.currentValue = createValueFrom(this.derivedFrom); 47 48 if (((ourOldValue == null) != (this.currentValue == null)) 49 || ((ourOldValue != null) && (!ourOldValue.equals(this.currentValue)))) { 50 this.listener.valueChanged(ourOldValue, this.currentValue); 51 } 52 } 53 54 public String toString() { 55 return new OurStringBuilder(this, OurStringBuilder.COMPACT_STYLE).append("derived from", this.derivedFrom) 56 .toString(); 57 } 58 59 } 60 | Popular Tags |