KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mule > util > counters > impl > CounterFactoryImpl


1 /*
2  * $Id: CounterFactoryImpl.java 3798 2006-11-04 04:07:14Z aperepel $
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 MuleSource MPL
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 JavaDoc;
17 import java.util.HashMap JavaDoc;
18 import java.util.Iterator JavaDoc;
19 import java.util.Map JavaDoc;
20
21 /**
22  * @author <a HREF="mailto:gnt@codehaus.org">Guillaume Nodet</a>
23  * @version $Revision: 3798 $
24  */

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