01: /*
02: * $Id: Operator.java 8077 2007-08-27 20:15:25Z aperepel $
03: * --------------------------------------------------------------------------------------
04: * Copyright (c) MuleSource, Inc. All rights reserved. http://www.mulesource.com
05: *
06: * The software in this package is published under the terms of the CPAL v1.0
07: * license, a copy of which has been included with this distribution in the
08: * LICENSE.txt file.
09: */
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: public class Operator extends AggregateCounter {
17:
18: private final Counter base2;
19: private double val1;
20: private double val2;
21:
22: public Operator(String name, AbstractCounter base,
23: AbstractCounter base2, Type type) {
24: super (name, type, base);
25: this .base2 = base2;
26: base2.addAggregate(this );
27: }
28:
29: public double nextValue() {
30: Type type = this .getType();
31:
32: if (type == Type.PLUS) {
33: return val1 + val2;
34: } else if (type == Type.MINUS) {
35: return val1 - val2;
36: } else if (type == Type.MULTIPLY) {
37: return val1 * val2;
38: } else if (type == Type.DIVIDE) {
39: return val2 == 0.0 ? (val1 >= 0 ? Double.POSITIVE_INFINITY
40: : Double.NEGATIVE_INFINITY) : (val1 / val2);
41: } else {
42: throw new IllegalStateException();
43: }
44: }
45:
46: public void doCompute() {
47: this.val1 = this.getBase().nextValue();
48: this.val2 = base2.nextValue();
49: }
50:
51: }
|