KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > hivemind > management > CalculatorImpl


1 // Copyright 2005 The Apache Software Foundation
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
6
//
7
// http://www.apache.org/licenses/LICENSE-2.0
8
//
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14

15 package org.apache.hivemind.management;
16
17 import java.util.Random JavaDoc;
18
19
20 /**
21  * @author Huegen
22  * @since 1.1
23  */

24 public class CalculatorImpl implements Calculator
25 {
26     private int _sum = 0;
27     private Random JavaDoc random = new Random JavaDoc(System.currentTimeMillis());
28
29     public int getSum()
30     {
31         return _sum;
32     }
33
34     public void add(int value)
35     {
36         sleepRandom();
37         _sum += value;
38     }
39
40     public void subtract(int value)
41     {
42         sleepRandom();
43         _sum -= value;
44     }
45
46     public void multiply(int value)
47     {
48         sleepRandom();
49         _sum *= value;
50     }
51
52     public void clear()
53     {
54         sleepRandom();
55         _sum = 0;
56     }
57
58     /**
59      * Sleep a little bit to give PerformanceMonitor some data
60      * to work with
61      */

62     public void sleepRandom()
63     {
64         try
65         {
66             Thread.sleep(random.nextInt(100));
67         }
68         catch (InterruptedException JavaDoc e)
69         {
70         }
71     }
72 }
73
Popular Tags