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