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