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 | |
public class Operator extends AggregateCounter |
13 | |
{ |
14 | |
|
15 | |
private final Counter base2; |
16 | |
private double val1; |
17 | |
private double val2; |
18 | |
|
19 | |
public Operator(String name, AbstractCounter base, AbstractCounter base2, Type type) |
20 | |
{ |
21 | 0 | super(name, type, base); |
22 | 0 | this.base2 = base2; |
23 | 0 | base2.addAggregate(this); |
24 | 0 | } |
25 | |
|
26 | |
public double nextValue() |
27 | |
{ |
28 | 0 | Type type = this.getType(); |
29 | |
|
30 | 0 | if (type == Type.PLUS) |
31 | |
{ |
32 | 0 | return val1 + val2; |
33 | |
} |
34 | 0 | else if (type == Type.MINUS) |
35 | |
{ |
36 | 0 | return val1 - val2; |
37 | |
} |
38 | 0 | else if (type == Type.MULTIPLY) |
39 | |
{ |
40 | 0 | return val1 * val2; |
41 | |
} |
42 | 0 | else if (type == Type.DIVIDE) |
43 | |
{ |
44 | 0 | return val2 == 0.0 |
45 | |
? (val1 >= 0 ? Double.POSITIVE_INFINITY : Double.NEGATIVE_INFINITY) |
46 | |
: (val1 / val2); |
47 | |
} |
48 | |
else |
49 | |
{ |
50 | 0 | throw new IllegalStateException(); |
51 | |
} |
52 | |
} |
53 | |
|
54 | |
public void doCompute() |
55 | |
{ |
56 | 0 | this.val1 = this.getBase().nextValue(); |
57 | 0 | this.val2 = base2.nextValue(); |
58 | 0 | } |
59 | |
|
60 | |
} |