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