View Javadoc
1   /*
2    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
3    * The software in this package is published under the terms of the CPAL v1.0
4    * license, a copy of which has been included with this distribution in the
5    * LICENSE.txt file.
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          super(name, type, base);
22          this.base2 = base2;
23          base2.addAggregate(this);
24      }
25  
26      public double nextValue()
27      {
28          Type type = this.getType();
29  
30          if (type == Type.PLUS)
31          {
32              return val1 + val2;
33          }
34          else if (type == Type.MINUS)
35          {
36              return val1 - val2;
37          }
38          else if (type == Type.MULTIPLY)
39          {
40              return val1 * val2;
41          }
42          else if (type == Type.DIVIDE)
43          {
44              return val2 == 0.0
45                              ? (val1 >= 0 ? Double.POSITIVE_INFINITY : Double.NEGATIVE_INFINITY)
46                              : (val1 / val2);
47          }
48          else
49          {
50              throw new IllegalStateException();
51          }
52      }
53  
54      public void doCompute()
55      {
56          this.val1 = this.getBase().nextValue();
57          this.val2 = base2.nextValue();
58      }
59  
60  }