001: /*
002: * $Id: CounterFactoryImpl.java 8077 2007-08-27 20:15:25Z aperepel $
003: * --------------------------------------------------------------------------------------
004: * Copyright (c) MuleSource, Inc. All rights reserved. http://www.mulesource.com
005: *
006: * The software in this package is published under the terms of the CPAL v1.0
007: * license, a copy of which has been included with this distribution in the
008: * LICENSE.txt file.
009: */
010:
011: package org.mule.util.counters.impl;
012:
013: import org.mule.util.counters.Counter;
014: import org.mule.util.counters.CounterFactory.Type;
015:
016: import java.util.ArrayList;
017: import java.util.HashMap;
018: import java.util.Iterator;
019: import java.util.Map;
020:
021: public final class CounterFactoryImpl {
022:
023: private static Map counters = new HashMap();
024: private static ArrayList publicCounters = new ArrayList();
025:
026: /** Do not instanciate. */
027: private CounterFactoryImpl() {
028: // no-op
029: }
030:
031: public static Counter getCounter(String name) {
032: return (Counter) counters.get(name);
033: }
034:
035: public static Counter createCounter(String name, String first,
036: String second, Type type, boolean visible) {
037: Counter counter = getCounter(name);
038: if (counter != null) {
039: throw new IllegalStateException();
040: } else {
041: counter = internalCreateCounter(name, first, second, type,
042: visible);
043: }
044: return counter;
045: }
046:
047: public static Iterator getCounters() {
048: return publicCounters.iterator();
049: }
050:
051: protected static AbstractCounter internalCreateCounter(String name,
052: String first, String second, Type type, boolean visible) {
053: AbstractCounter counter = null;
054: if (name == null) {
055: throw new IllegalStateException();
056: } else if (first == null && second == null) {
057: if (type == Type.NUMBER) {
058: counter = new Number(name);
059: } else {
060: throw new IllegalStateException();
061: }
062: } else if (first != null && second == null) {
063: AbstractCounter b = (AbstractCounter) getCounter(first);
064: if (b == null) {
065: throw new IllegalStateException();
066: }
067: if (type == Type.MIN) {
068: counter = new Min(name, b);
069: } else if (type == Type.MAX) {
070: counter = new Max(name, b);
071: } else if (type == Type.SUM) {
072: counter = new Sum(name, b);
073: } else if (type == Type.AVERAGE) {
074: counter = new Average(name, b);
075: } else if (type == Type.TIME_AVERAGE) {
076: counter = new TimeAverage(name, b);
077: } else if (type == Type.DELTA) {
078: counter = new Delta(name, b);
079: } else if (type == Type.INSTANT_RATE) {
080: counter = new InstantRate(name, b);
081: } else if (type == Type.RATE_PER_SECOND
082: || type == Type.RATE_PER_MINUTE
083: || type == Type.RATE_PER_HOUR) {
084: counter = new RatePerUnit(name, null, type, b);
085: } else {
086: throw new IllegalStateException();
087: }
088: } else if (first != null && second != null) {
089: AbstractCounter b = (AbstractCounter) getCounter(first);
090: if (b == null) {
091: throw new IllegalStateException();
092: }
093: if (type == Type.RATE_PER_SECOND
094: || type == Type.RATE_PER_MINUTE
095: || type == Type.RATE_PER_HOUR) {
096: counter = new RatePerUnit(name, second, type, b);
097: } else if (type == Type.PLUS || type == Type.MINUS
098: || type == Type.MULTIPLY || type == Type.DIVIDE) {
099: AbstractCounter b2 = (AbstractCounter) getCounter(second);
100: if (b2 == null) {
101: throw new IllegalStateException();
102: }
103: counter = new Operator(name, b, b2, type);
104: } else {
105: throw new IllegalStateException();
106: }
107: } else {
108: throw new IllegalStateException();
109: }
110: counters.put(name, counter);
111: if (visible) {
112: publicCounters.add(counter);
113: }
114: return counter;
115: }
116: }
|