1 //
2 // ========================================================================
3 // Copyright (c) 1995-2015 Mort Bay Consulting Pty. Ltd.
4 // ------------------------------------------------------------------------
5 // All rights reserved. This program and the accompanying materials
6 // are made available under the terms of the Eclipse Public License v1.0
7 // and Apache License v2.0 which accompanies this distribution.
8 //
9 // The Eclipse Public License is available at
10 // http://www.eclipse.org/legal/epl-v10.html
11 //
12 // The Apache License v2.0 is available at
13 // http://www.opensource.org/licenses/apache2.0.php
14 //
15 // You may elect to redistribute this code under either of these licenses.
16 // ========================================================================
17 //
18
19 package org.eclipse.jetty.util.statistic;
20
21 import java.util.concurrent.atomic.AtomicLong;
22
23 import org.eclipse.jetty.util.Atomics;
24
25
26 /* ------------------------------------------------------------ */
27 /** Statistics on a counter value.
28 * <p>
29 * Keep total, current and maximum values of a counter that
30 * can be incremented and decremented. The total refers only
31 * to increments.
32 *
33 */
34 public class CounterStatistic
35 {
36 protected final AtomicLong _max = new AtomicLong();
37 protected final AtomicLong _curr = new AtomicLong();
38 protected final AtomicLong _total = new AtomicLong();
39
40 /* ------------------------------------------------------------ */
41 public void reset()
42 {
43 reset(0);
44 }
45
46 /* ------------------------------------------------------------ */
47 public void reset(final long value)
48 {
49 _max.set(value);
50 _curr.set(value);
51 _total.set(0); // total always set to 0 to properly calculate cumulative total
52 }
53
54 /* ------------------------------------------------------------ */
55 /**
56 * @param delta the amount to add to the count
57 * @return the new value
58 */
59 public long add(final long delta)
60 {
61 long value=_curr.addAndGet(delta);
62 if (delta > 0)
63 {
64 _total.addAndGet(delta);
65 Atomics.updateMax(_max,value);
66 }
67 return value;
68 }
69
70 /* ------------------------------------------------------------ */
71 /**
72 * increment the value by one
73 * @return the new value, post increment
74 */
75 public long increment()
76 {
77 return add(1);
78 }
79
80 /* ------------------------------------------------------------ */
81 /**
82 * decrement by 1
83 * @return the new value, post-decrement
84 */
85 public long decrement()
86 {
87 return 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 @Override
119 public String toString()
120 {
121 return String.format("%s@%x{c=%d,m=%d,t=%d}",this.getClass().getSimpleName(),hashCode(),_curr.get(),_max.get(),_total.get());
122 }
123 }