]> WPIA git - gigi.git/blob - lib/jetty/org/eclipse/jetty/util/statistic/CounterStatistic.java
Merge "Update notes about password security"
[gigi.git] / lib / jetty / org / eclipse / jetty / util / statistic / CounterStatistic.java
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         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      */
58     public long add(final long delta)
59     {
60         long value=_curr.addAndGet(delta);
61         if (delta > 0)
62         {
63             _total.addAndGet(delta);
64             Atomics.updateMax(_max,value);
65         }
66         return value;
67     }
68
69     /* ------------------------------------------------------------ */
70     /**
71      */
72     public long increment()
73     {
74         return add(1);
75     }
76
77     /* ------------------------------------------------------------ */
78     /**
79      */
80     public long decrement()
81     {
82         return add(-1);
83     }
84
85     /* ------------------------------------------------------------ */
86     /**
87      * @return max value
88      */
89     public long getMax()
90     {
91         return _max.get();
92     }
93
94     /* ------------------------------------------------------------ */
95     /**
96      * @return current value
97      */
98     public long getCurrent()
99     {
100         return _curr.get();
101     }
102
103     /* ------------------------------------------------------------ */
104     /**
105      * @return total value
106      */
107     public long getTotal()
108     {
109         return _total.get();
110     }
111
112     /* ------------------------------------------------------------ */
113     @Override
114     public String toString()
115     {
116         return String.format("%s@%x{c=%d,m=%d,t=%d}",this.getClass().getSimpleName(),hashCode(),_curr.get(),_max.get(),_total.get());
117     }
118 }