1 //
2 // ========================================================================
3 // Copyright (c) 1995-2016 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 _total.set(0);
44 _max.set(0);
45 long current=_curr.get();
46 _total.addAndGet(current);
47 Atomics.updateMax(_max,current);
48 }
49
50 /* ------------------------------------------------------------ */
51 public void reset(final long value)
52 {
53 _total.set(0);
54 _max.set(0);
55 _curr.set(value);
56 if (value>0)
57 {
58 _total.addAndGet(value);
59 Atomics.updateMax(_max,value);
60 }
61 }
62
63 /* ------------------------------------------------------------ */
64 /**
65 * @param delta the amount to add to the count
66 * @return the new value
67 */
68 public long add(final long delta)
69 {
70 long value=_curr.addAndGet(delta);
71 if (delta > 0)
72 {
73 _total.addAndGet(delta);
74 Atomics.updateMax(_max,value);
75 }
76 return value;
77 }
78
79 /* ------------------------------------------------------------ */
80 /**
81 * increment the value by one
82 * @return the new value, post increment
83 */
84 public long increment()
85 {
86 return add(1);
87 }
88
89 /* ------------------------------------------------------------ */
90 /**
91 * decrement by 1
92 * @return the new value, post-decrement
93 */
94 public long decrement()
95 {
96 return add(-1);
97 }
98
99 /* ------------------------------------------------------------ */
100 /**
101 * @return max value
102 */
103 public long getMax()
104 {
105 return _max.get();
106 }
107
108 /* ------------------------------------------------------------ */
109 /**
110 * @return current value
111 */
112 public long getCurrent()
113 {
114 return _curr.get();
115 }
116
117 /* ------------------------------------------------------------ */
118 /**
119 * @return total value
120 */
121 public long getTotal()
122 {
123 return _total.get();
124 }
125
126 /* ------------------------------------------------------------ */
127 @Override
128 public String toString()
129 {
130 return String.format("%s@%x{c=%d,m=%d,t=%d}",this.getClass().getSimpleName(),hashCode(),_curr.get(),_max.get(),_total.get());
131 }
132 }