View Javadoc

1   /*
2    * $Id: AbstractCounter.java 19191 2010-08-25 21:05:23Z tcarlson $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
5    *
6    * The software in this package is published under the terms of the CPAL v1.0
7    * license, a copy of which has been included with this distribution in the
8    * LICENSE.txt file.
9    */
10  
11  package org.mule.util.counters.impl;
12  
13  import org.mule.util.counters.Counter;
14  import org.mule.util.counters.CounterFactory.Type;
15  
16  import java.util.ArrayList;
17  import java.util.Collections;
18  import java.util.Iterator;
19  import java.util.List;
20  
21  public abstract class AbstractCounter implements Counter
22  {
23  
24      private final Type type;
25      private final String name;
26      private final List aggregates;
27  
28      public AbstractCounter(String name, Type type)
29      {
30          super();
31          this.name = name;
32          this.type = type;
33          this.aggregates = Collections.synchronizedList(new ArrayList());
34      }
35  
36      public Type getType()
37      {
38          return this.type;
39      }
40  
41      public String getName()
42      {
43          return this.name;
44      }
45  
46      public abstract double increment();
47  
48      public abstract double incrementBy(double value);
49  
50      public abstract double decrement();
51  
52      public abstract void setRawValue(double value);
53  
54      public abstract double nextValue();
55  
56      protected void addAggregate(AggregateCounter counter)
57      {
58          this.aggregates.add(counter);
59      }
60  
61      protected void propagate()
62      {
63          Iterator it = this.aggregates.iterator();
64          while (it.hasNext())
65          {
66              AggregateCounter agg = (AggregateCounter) it.next();
67              agg.compute();
68          }
69      }
70  
71  }