1 // ========================================================================
2 // Copyright (c) 2009-2009 Mort Bay Consulting Pty. Ltd.
3 // ------------------------------------------------------------------------
4 // All rights reserved. This program and the accompanying materials
5 // are made available under the terms of the Eclipse Public License v1.0
6 // and Apache License v2.0 which accompanies this distribution.
7 // The Eclipse Public License is available at
8 // http://www.eclipse.org/legal/epl-v10.html
9 // The Apache License v2.0 is available at
10 // http://www.opensource.org/licenses/apache2.0.php
11 // You may elect to redistribute this code under either of these licenses.
12 // ========================================================================
13
14 package org.eclipse.jetty.util.statistic;
15
16 import java.util.concurrent.atomic.AtomicLong;
17
18
19 /* ------------------------------------------------------------ */
20 /** Statistics on a counter value.
21 * <p>
22 * Keep total, current and maximum values of a counter that
23 * can be incremented and decremented. The total refers only
24 * to increments.
25 *
26 */
27 public class CounterStatistic
28 {
29 protected final AtomicLong _max = new AtomicLong();
30 protected final AtomicLong _curr = new AtomicLong();
31 protected final AtomicLong _total = new AtomicLong();
32
33 /* ------------------------------------------------------------ */
34 public void reset()
35 {
36 reset(0);
37 }
38
39 /* ------------------------------------------------------------ */
40 public void reset(final long value)
41 {
42 _max.set(value);
43 _curr.set(value);
44 _total.set(0); // total always set to 0 to properly calculate cumulative total
45 }
46
47 /* ------------------------------------------------------------ */
48 /**
49 * @param delta the amount to add to the count
50 */
51 public void add(final long delta)
52 {
53 long value=_curr.addAndGet(delta);
54 if (delta > 0)
55 _total.addAndGet(delta);
56 long oldValue = _max.get();
57 while (value > oldValue)
58 {
59 if (_max.compareAndSet(oldValue, value))
60 break;
61 oldValue = _max.get();
62 }
63 }
64
65 /* ------------------------------------------------------------ */
66 /**
67 * @param delta the amount to subtract the count by.
68 */
69 public void subtract(final long delta)
70 {
71 add(-delta);
72 }
73
74 /* ------------------------------------------------------------ */
75 /**
76 */
77 public void increment()
78 {
79 add(1);
80 }
81
82 /* ------------------------------------------------------------ */
83 /**
84 */
85 public void decrement()
86 {
87 add(-1);
88 }
89
90 /* ------------------------------------------------------------ */
91 /**
92 * @return max value
93 */
94 public long getMax()
95 {
96 return _max.get();
97 }
98
99 /* ------------------------------------------------------------ */
100 /**
101 * @return current value
102 */
103 public long getCurrent()
104 {
105 return _curr.get();
106 }
107
108 /* ------------------------------------------------------------ */
109 /**
110 * @return total value
111 */
112 public long getTotal()
113 {
114 return _total.get();
115 }
116
117 /* ------------------------------------------------------------ */
118 protected void upxdateMax(long value)
119 {
120 }
121 }