1 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 25 public class CounterFactoryImpl 26 { 27 28 private static Map counters = new HashMap (); 29 private static ArrayList publicCounters = new ArrayList (); 30 31 public static Counter getCounter(String name) 32 { 33 return (Counter)counters.get(name); 34 } 35 36 public static Counter createCounter(String name, String first, String second, Type type, boolean visible) 37 { 38 Counter counter = getCounter(name); 39 if (counter != null) 40 { 41 throw new IllegalStateException (); 42 } 43 else 44 { 45 counter = internalCreateCounter(name, first, second, type, visible); 46 } 47 return counter; 48 } 49 50 public static Iterator getCounters() 51 { 52 return publicCounters.iterator(); 53 } 54 55 protected static AbstractCounter internalCreateCounter(String name, 56 String first, 57 String second, 58 Type type, 59 boolean visible) 60 { 61 AbstractCounter counter = null; 62 if (name == null) 63 { 64 throw new IllegalStateException (); 65 } 66 else if (first == null && second == null) 67 { 68 if (type == Type.NUMBER) 69 { 70 counter = new Number (name); 71 } 72 else 73 { 74 throw new IllegalStateException (); 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 (); 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 (); 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 (); 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 (); 139 } 140 counter = new Operator(name, b, b2, type); 141 } 142 else 143 { 144 throw new IllegalStateException (); 145 } 146 } 147 else 148 { 149 throw new IllegalStateException (); 150 } 151 counters.put(name, counter); 152 if (visible) 153 { 154 publicCounters.add(counter); 155 } 156 return counter; 157 } 158 } 159 | Popular Tags |