View Javadoc

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