01: /*
02: * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
03: */
04: package com.tc.config.schema.dynamic;
05:
06: import com.tc.util.Assert;
07:
08: /**
09: * An {@link IntConfigItem} that returns the sum of one or more other {@link IntConfigItem}s.
10: */
11: public class SummingIntConfigItem implements IntConfigItem {
12:
13: private final IntConfigItem[] children;
14:
15: public SummingIntConfigItem(IntConfigItem[] children) {
16: Assert.assertNoNullElements(children);
17: this .children = children;
18: }
19:
20: public int getInt() {
21: long out = 0;
22: for (int i = 0; i < children.length; ++i) {
23: out += children[i].getInt();
24: }
25:
26: // Watch for overflow.
27: if (out > Integer.MAX_VALUE)
28: return Integer.MAX_VALUE;
29: else
30: return (int) out;
31: }
32:
33: public Object getObject() {
34: return new Integer(getInt());
35: }
36:
37: public void addListener(ConfigItemListener changeListener) {
38: for (int i = 0; i < this .children.length; ++i)
39: children[i].addListener(changeListener);
40: }
41:
42: public void removeListener(ConfigItemListener changeListener) {
43: for (int i = 0; i < this.children.length; ++i)
44: children[i].removeListener(changeListener);
45: }
46:
47: }
|