KickJava   Java API By Example, From Geeks To Geeks.

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


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
8 /**
9  * 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) return Integer.MAX_VALUE;
28     else return (int) out;
29   }
30
31   public Object JavaDoc getObject() {
32     return new Integer JavaDoc(getInt());
33   }
34
35   public void addListener(ConfigItemListener changeListener) {
36     for (int i = 0; i < this.children.length; ++i)
37       children[i].addListener(changeListener);
38   }
39
40   public void removeListener(ConfigItemListener changeListener) {
41     for (int i = 0; i < this.children.length; ++i)
42       children[i].removeListener(changeListener);
43   }
44
45 }
46
Popular Tags