KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tc > config > schema > dynamic > DerivedConfigItem


1 /*
2  * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
3  */

4 package com.tc.config.schema.dynamic;
5
6 import com.tc.util.Assert;
7 import com.tc.util.stringification.OurStringBuilder;
8
9 /**
10  * A {@link ConfigItem} whose value is derived from one or more other {@link ConfigItem}s.
11  */

12 public abstract class DerivedConfigItem implements ConfigItem, ConfigItemListener {
13
14   private final ConfigItem[] derivedFrom;
15   private final CompoundConfigItemListener listener;
16
17   private Object JavaDoc 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 JavaDoc getObject() {
31     return currentValue;
32   }
33
34   protected abstract Object JavaDoc 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 JavaDoc oldValue, Object JavaDoc newValue) {
45     Object JavaDoc 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 JavaDoc toString() {
55     return new OurStringBuilder(this, OurStringBuilder.COMPACT_STYLE).append("derived from", this.derivedFrom)
56         .toString();
57   }
58
59 }
60
Popular Tags