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