KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * $Id: RatePerUnit.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.CounterFactory.Type;
14
15 import java.security.InvalidParameterException JavaDoc;
16 import java.util.Iterator JavaDoc;
17 import java.util.LinkedList JavaDoc;
18
19 /**
20  * @author <a HREF="mailto:gnt@codehaus.org">Guillaume Nodet</a>
21  * @version $Revision: 3798 $
22  */

23 public class RatePerUnit extends AggregateCounter
24 {
25
26     private static class Sample
27     {
28         public Sample(double value, long time)
29         {
30             this.value = value;
31             this.time = time;
32         }
33
34         private double value;
35         private long time;
36
37         /**
38          * @return the time of the sample
39          */

40         public long getTime()
41         {
42             return time;
43         }
44
45         /**
46          * @return the value of the sample
47          */

48         public double getValue()
49         {
50             return value;
51         }
52
53     }
54
55     private LinkedList JavaDoc samples;
56     private long unit;
57     private long length;
58     private long baseTime;
59
60     public RatePerUnit(String JavaDoc name, String JavaDoc p, Type type, AbstractCounter base)
61     {
62         super(name, type, base);
63         if (type == Type.RATE_PER_SECOND)
64         {
65             unit = 1000;
66         }
67         else if (type == Type.RATE_PER_MINUTE)
68         {
69             unit = 60 * 1000;
70         }
71         else if (type == Type.RATE_PER_HOUR)
72         {
73             unit = 60 * 60 * 1000;
74         }
75         else
76         {
77             throw new InvalidParameterException JavaDoc();
78         }
79         try
80         {
81             length = Long.parseLong(p);
82         }
83         catch (Exception JavaDoc e)
84         {
85             length = 0;
86         }
87         if (length <= 0)
88         {
89             length = 128;
90         }
91         samples = new LinkedList JavaDoc();
92         this.baseTime = System.currentTimeMillis();
93     }
94
95     public double nextValue()
96     {
97         if (samples.isEmpty())
98         {
99             return 0.0;
100         }
101         else
102         {
103             double total = 0.0;
104             long current = getTime();
105             Iterator JavaDoc it = samples.iterator();
106             Sample sample = null;
107             while (it.hasNext())
108             {
109                 sample = (Sample)it.next();
110                 if (current - sample.time > length)
111                 {
112                     break;
113                 }
114                 total += sample.value;
115             }
116             return total / (1 + current - (sample != null ? sample.time : 0));
117         }
118     }
119
120     public void doCompute()
121     {
122         Sample l = samples.isEmpty() ? null : (Sample)samples.getFirst();
123         long t = getTime();
124         if (l == null || t > l.time)
125         {
126             Sample s = new Sample(getBase().nextValue(), t);
127             samples.addFirst(s);
128         }
129         else
130         {
131             l.value += getBase().nextValue();
132         }
133         while (samples.size() > length)
134         {
135             samples.removeLast();
136         }
137     }
138
139     protected long getTime()
140     {
141         return (System.currentTimeMillis() - baseTime) / unit;
142     }
143
144 }
145
Popular Tags