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