View Javadoc

1   /*
2    * $Id: CounterFactoryImpl.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.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.HashMap;
18  import java.util.Iterator;
19  import java.util.Map;
20  
21  public final class CounterFactoryImpl
22  {
23  
24      private static Map counters = new HashMap();
25      private static ArrayList publicCounters = new ArrayList();
26  
27      /** Do not instanciate. */
28      private CounterFactoryImpl ()
29      {
30          // no-op
31      }
32  
33      public static Counter getCounter(String name)
34      {
35          return (Counter) counters.get(name);
36      }
37  
38      public static Counter createCounter(String name, String first, String second, Type type, boolean visible)
39      {
40          Counter counter = getCounter(name);
41          if (counter != null)
42          {
43              throw new IllegalStateException();
44          }
45          else
46          {
47              counter = internalCreateCounter(name, first, second, type, visible);
48          }
49          return counter;
50      }
51  
52      public static Iterator getCounters()
53      {
54          return publicCounters.iterator();
55      }
56  
57      protected static AbstractCounter internalCreateCounter(String name,
58                                                             String first,
59                                                             String second,
60                                                             Type type,
61                                                             boolean visible)
62      {
63          AbstractCounter counter = null;
64          if (name == null)
65          {
66              throw new IllegalStateException();
67          }
68          else if (first == null && second == null)
69          {
70              if (type == Type.NUMBER)
71              {
72                  counter = new Number(name);
73              }
74              else
75              {
76                  throw new IllegalStateException();
77              }
78          }
79          else if (first != null && second == null)
80          {
81              AbstractCounter b = (AbstractCounter) getCounter(first);
82              if (b == null)
83              {
84                  throw new IllegalStateException();
85              }
86              if (type == Type.MIN)
87              {
88                  counter = new Min(name, b);
89              }
90              else if (type == Type.MAX)
91              {
92                  counter = new Max(name, b);
93              }
94              else if (type == Type.SUM)
95              {
96                  counter = new Sum(name, b);
97              }
98              else if (type == Type.AVERAGE)
99              {
100                 counter = new Average(name, b);
101             }
102             else if (type == Type.TIME_AVERAGE)
103             {
104                 counter = new TimeAverage(name, b);
105             }
106             else if (type == Type.DELTA)
107             {
108                 counter = new Delta(name, b);
109             }
110             else if (type == Type.INSTANT_RATE)
111             {
112                 counter = new InstantRate(name, b);
113             }
114             else if (type == Type.RATE_PER_SECOND || type == Type.RATE_PER_MINUTE
115                      || type == Type.RATE_PER_HOUR)
116             {
117                 counter = new RatePerUnit(name, null, type, b);
118             }
119             else
120             {
121                 throw new IllegalStateException();
122             }
123         }
124         else if (first != null && second != null)
125         {
126             AbstractCounter b = (AbstractCounter) getCounter(first);
127             if (b == null)
128             {
129                 throw new IllegalStateException();
130             }
131             if (type == Type.RATE_PER_SECOND || type == Type.RATE_PER_MINUTE || type == Type.RATE_PER_HOUR)
132             {
133                 counter = new RatePerUnit(name, second, type, b);
134             }
135             else if (type == Type.PLUS || type == Type.MINUS || type == Type.MULTIPLY || type == Type.DIVIDE)
136             {
137                 AbstractCounter b2 = (AbstractCounter) getCounter(second);
138                 if (b2 == null)
139                 {
140                     throw new IllegalStateException();
141                 }
142                 counter = new Operator(name, b, b2, type);
143             }
144             else
145             {
146                 throw new IllegalStateException();
147             }
148         }
149         else
150         {
151             throw new IllegalStateException();
152         }
153         counters.put(name, counter);
154         if (visible)
155         {
156             publicCounters.add(counter);
157         }
158         return counter;
159     }
160 }