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 import java.util.concurrent.atomic.LongAccumulator;
23 import java.util.concurrent.atomic.LongAdder;
24
25 /* ------------------------------------------------------------ */
26 /** Statistics on a counter value.
27 * <p>
28 * Keep total, current and maximum values of a counter that
29 * can be incremented and decremented. The total refers only
30 * to increments.
31 *
32 */
33 public class CounterStatistic
34 {
35 protected final LongAccumulator _max = new LongAccumulator(Math::max,0L);
36 protected final AtomicLong _current = new AtomicLong();
37 protected final LongAdder _total = new LongAdder();
38
39 /* ------------------------------------------------------------ */
40 public void reset()
41 {
42 _total.reset();
43 _max.reset();
44 long current=_current.get();
45 _total.add(current);
46 _max.accumulate(current);
47 }
48
49 /* ------------------------------------------------------------ */
50 public void reset(final long value)
51 {
52 _current.set(value);
53 _total.reset();
54 _max.reset();
55 if (value>0)
56 {
57 _total.add(value);
58 _max.accumulate(value);
59 }
60 }
61
62 /* ------------------------------------------------------------ */
63 /**
64 * @param delta the amount to add to the count
65 * @return the new value
66 */
67 public long add(final long delta)
68 {
69 long value=_current.addAndGet(delta);
70 if (delta > 0)
71 {
72 _total.add(delta);
73 _max.accumulate(value);
74 }
75 return value;
76 }
77
78 /* ------------------------------------------------------------ */
79 /**
80 * increment the value by one
81 * @return the new value, post increment
82 */
83 public long increment()
84 {
85 long value=_current.incrementAndGet();
86 _total.increment();
87 _max.accumulate(value);
88 return value;
89 }
90
91 /* ------------------------------------------------------------ */
92 /**
93 * decrement by 1
94 * @return the new value, post-decrement
95 */
96 public long decrement()
97 {
98 return _current.decrementAndGet();
99 }
100
101 /* ------------------------------------------------------------ */
102 /**
103 * @return max value
104 */
105 public long getMax()
106 {
107 return _max.get();
108 }
109
110 /* ------------------------------------------------------------ */
111 /**
112 * @return current value
113 */
114 public long getCurrent()
115 {
116 return _current.get();
117 }
118
119 /* ------------------------------------------------------------ */
120 /**
121 * @return total value
122 */
123 public long getTotal()
124 {
125 return _total.sum();
126 }
127
128 /* ------------------------------------------------------------ */
129 @Override
130 public String toString()
131 {
132 return String.format("%s@%x{c=%d,m=%d,t=%d}",this.getClass().getSimpleName(),hashCode(),_current.get(),_max.get(),_total.sum());
133 }
134 }