View Javadoc

1   /*
2    * $Id: CounterFactory.java 7976 2007-08-21 14:26:13Z dirk.olmes $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSource, Inc.  All rights reserved.  http://www.mulesource.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;
12  
13  import org.mule.util.counters.impl.CounterFactoryImpl;
14  
15  import java.util.Iterator;
16  
17  /**
18   * This class is the Counter's factory. It is the main entry point for operations on
19   * counters. The user can:
20   * <ul>
21   * <li>retrieve a counter by its name</li>
22   * <li>create a counter</li>
23   * <li>retrieve a list of public counters</li>
24   * </ul>
25   */
26  public final class CounterFactory
27  {
28  
29      /** Do not instanciate. */
30      private CounterFactory ()
31      {
32          // no-op
33      }
34  
35      /**
36       * Enum class for all different types of counters. The type of a counter is
37       * defined on creation and can be retrieved on each counter.
38       */
39      public static final class Type
40      {
41  
42          /** A basic counter representing a double value */
43          public static final Type NUMBER = new Type("Number");
44          /** Counter representing the sum of two counters */
45          public static final Type SUM = new Type("Sum");
46          /** Counter representing the minimum value of a counter */
47          public static final Type MIN = new Type("Min");
48          /** Counter representing the maximum value of a counter */
49          public static final Type MAX = new Type("Max");
50          /** Counter representing the average value of a counter */
51          public static final Type AVERAGE = new Type("Average");
52          /** Counter representing the time average value of a counter */
53          public static final Type TIME_AVERAGE = new Type("TimeAverage");
54          /** Counter representing the variation of a counter */
55          public static final Type DELTA = new Type("Delta");
56          /** Counter representing the instant rate of a counter */
57          public static final Type INSTANT_RATE = new Type("InstantRate");
58          /** Counter representing rate per second of a counter */
59          public static final Type RATE_PER_SECOND = new Type("RatePerSecond");
60          /** Counter representing rate per minute of a counter */
61          public static final Type RATE_PER_MINUTE = new Type("RatePerMinute");
62          /** Counter representing rate per hour of a counter */
63          public static final Type RATE_PER_HOUR = new Type("RatePerHour");
64          /** Counter represening the sum of two other counters */
65          public static final Type PLUS = new Type("Plus");
66          /** Counter represening the difference of two other counters */
67          public static final Type MINUS = new Type("Minus");
68          /** Counter represening the multiplication of two other counters */
69          public static final Type MULTIPLY = new Type("Multiply");
70          /** Counter represening the division of two other counters */
71          public static final Type DIVIDE = new Type("Divide");
72  
73          /** The string representation of this counter type */
74          private String name;
75  
76          /**
77           * Constructor of the type
78           * 
79           * @param name the name of the counter type
80           */
81          protected Type(String name)
82          {
83              this.name = name;
84          }
85  
86          public String getName()
87          {
88              return this.name;
89          }
90      }
91  
92      /**
93       * Search the defined counters for a counter of the given name.
94       * 
95       * @param name the name of the counter to retrieve
96       * @return the counter
97       */
98      public static Counter getCounter(String name)
99      {
100         return CounterFactoryImpl.getCounter(name);
101     }
102 
103     /**
104      * Create a new public counter of the given type.
105      * 
106      * @param name the name of the counter to create
107      * @param type the type of the counter
108      * @return the newly created counter
109      */
110     public static Counter createCounter(String name, Type type)
111     {
112         return createCounter(name, null, null, type, true);
113     }
114 
115     /**
116      * Create a new counter of the given type and visibility.
117      * 
118      * @param name the name of the counter to create
119      * @param type the type of the counter
120      * @param visible boolean specifying if the counter is public or not
121      * @return the newly created counter
122      */
123     public static Counter createCounter(String name, Type type, boolean visible)
124     {
125         return createCounter(name, null, null, type, visible);
126     }
127 
128     /**
129      * Create a new public aggregate counter of the given type.
130      * 
131      * @param name the name of the counter to create
132      * @param base the name of the counter to use for computation
133      * @param type the type of the counter
134      * @return the newly created counter
135      */
136     public static Counter createCounter(String name, String base, Type type)
137     {
138         return createCounter(name, base, null, type, true);
139     }
140 
141     /**
142      * Create a new aggregate counter of the given type and visibility.
143      * 
144      * @param name the name of the counter to create
145      * @param base the name of the counter to use for computation
146      * @param type the type of the counter
147      * @param visible boolean specifying if the counter is public or not
148      * @return the newly created counter
149      */
150     public static Counter createCounter(String name, String base, Type type, boolean visible)
151     {
152         return createCounter(name, base, null, type, visible);
153     }
154 
155     /**
156      * Create a new public aggregate counter of the given type.
157      * 
158      * @param name the name of the counter to create
159      * @param first the name of the first counter to use for computation
160      * @param second the name of the first counter to use for computation
161      * @param type the type of the counter
162      * @return the newly created counter
163      */
164     public static Counter createCounter(String name, String first, String second, Type type)
165     {
166         return createCounter(name, first, second, type, true);
167     }
168 
169     /**
170      * Create a new aggregate counter of the given type and visibility.
171      * 
172      * @param name the name of the counter to create
173      * @param first the name of the first counter to use for computation
174      * @param second the name of the first counter to use for computation
175      * @param type the type of the counter
176      * @param visible boolean specifying if the counter is public or not
177      * @return the newly created counter
178      */
179     public static Counter createCounter(String name, String first, String second, Type type, boolean visible)
180     {
181         return CounterFactoryImpl.createCounter(name, first, second, type, visible);
182     }
183 
184     /**
185      * Retrieve an iterator giving the list of public defined counters.
186      * 
187      * @return an iterator to walk throught the list of public defined counters
188      */
189     public static Iterator getCounters()
190     {
191         return CounterFactoryImpl.getCounters();
192     }
193 
194 }