1   package example;
2   
3   /**
4    * Simple example class to show how to use ComTest.
5    * Counter is a class that counts sum, min and max
6    * of the items added.  Only maxN item is taken acount.
7    *  
8    * @author vesal
9    *
10   * @example
11   * <pre name="testJAVA">
12   * private Counter gCnt;
13   * 
14   * /.** *./
15   * \@Before public void doInit() {
16   *   gCnt = new Counter(3);
17   * } 
18   * 
19   * /.** *./
20   * \@After public void doAfter() {
21   *   System.out.println("After: " + gCnt.getCount());
22   * }
23   * </pre>
24   */
25  public class Counter {
26    private int count = 0;
27    private int max = 0;
28    private int min = 0;
29    private int sum = 0;
30    private final int maxN;
31    
32    /**
33     * Init counter with max N items
34     * @param maxN
35     */
36    public Counter(int maxN) {
37        this.maxN = maxN;
38    }
39    
40    /**
41     * Adds i to current counter if not allready too many added.
42     * @param i value to add
43     * @example
44     * <pre name="testCounterAdd">
45     * #THROWS IndexOutOfBoundsException,Exception
46     * // Test by ordinary sentences 
47     * // Counter cnt = new Counter(3);
48     * Counter cnt = gCnt;
49     *              cnt.getCount() === 0;  cnt.getSum() === 0; 
50     * cnt.add(1);  cnt.getCount() === 1;  cnt.getSum() === 1; 
51     * cnt.add(2);  cnt.getCount() === 2;  cnt.getSum() === 3; 
52     * </pre>
53     * 
54     * @example
55     * <pre name="testCounterAddTable">
56     * // test by table 
57     * Counter cnt = new Counter(3); 
58     * cnt.add($add);  cnt.getCount() === $count;  cnt.getSum() === $sum;
59     * cnt.getMax() === $max; cnt.getMin() === $min;
60     * 
61     *  ----------------------------------------------
62     *     $add   |   $count   | $sum   | $max | $min
63     *  ----------------------------------------------
64     *     ---    |     0      |  0     |   0  |   0  // after creation
65     *      1     |     1      |  1     |   1  |   1
66     *      2     |     2      |  1+2   |   2  |   1
67     *      3     |     3      |  1+2+3 |   3  |   1
68     *      4     |     3      |  6     |   3  |   1  // MaxN exceeded
69     *  ==============================================
70     *      5     |     1      |  5     |   5  |   5
71     *      2     |     2      |  5+2   |   5  |   2
72     *      3     |     3      |  5+2+3 |   5  |   2
73     *  ==============================================
74     *     -1     |     1      |  -1    |  -1  |  -1
75     *      2     |     2      | -1+2   |   2  |  -1
76     *      9     |     3      | -1+2+9 |   9  |  -1
77     *       
78     * </pre>
79     */ 
80    public void add(int i) {
81       if ( count >= maxN ) return;
82       sum += i;
83       count++;
84       if ( count == 1 ) min = max = i; 
85       if ( max < i ) max = i;
86       if ( min > i ) min = i;
87    }
88    
89    /** @return count of items */
90    public int getCount()  { return count; }
91    
92    /** @return max item       */
93    public int getMax()    { return max;   }
94    
95    /** @return min item       */
96    public int getMin()    { return min;   }
97    
98    /** @return Sum of items   */
99    public int getSum()    { return sum;   }
100  
101   /** @return Avg of items    */
102   public double getAvg() { if ( getCount() > 0 ) return getSum()/getCount(); else return 0; }  
103   
104   
105 }
106