KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mule > util > counters > impl > Operator


1 /*
2  * $Id: Operator.java 3798 2006-11-04 04:07:14Z aperepel $
3  * --------------------------------------------------------------------------------------
4  * Copyright (c) MuleSource, Inc. All rights reserved. http://www.mulesource.com
5  *
6  * The software in this package is published under the terms of the MuleSource MPL
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 /**
17  * @author <a HREF="mailto:gnt@codehaus.org">Guillaume Nodet</a>
18  * @version $Revision: 3798 $
19  */

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