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.
9 // The Eclipse Public License is available at
10 // http://www.eclipse.org/legal/epl-v10.html
12 // The Apache License v2.0 is available at
13 // http://www.opensource.org/licenses/apache2.0.php
15 // You may elect to redistribute this code under either of these licenses.
16 // ========================================================================
19 package org.eclipse.jetty.util.statistic;
21 import java.util.concurrent.atomic.AtomicLong;
23 import org.eclipse.jetty.util.Atomics;
26 /* ------------------------------------------------------------ */
27 /** Statistics on a counter value.
29 * Keep total, current and maximum values of a counter that
30 * can be incremented and decremented. The total refers only
34 public class CounterStatistic
36 protected final AtomicLong _max = new AtomicLong();
37 protected final AtomicLong _curr = new AtomicLong();
38 protected final AtomicLong _total = new AtomicLong();
40 /* ------------------------------------------------------------ */
46 /* ------------------------------------------------------------ */
47 public void reset(final long value)
51 _total.set(0); // total always set to 0 to properly calculate cumulative total
54 /* ------------------------------------------------------------ */
56 * @param delta the amount to add to the count
58 public long add(final long delta)
60 long value=_curr.addAndGet(delta);
63 _total.addAndGet(delta);
64 Atomics.updateMax(_max,value);
69 /* ------------------------------------------------------------ */
72 public long increment()
77 /* ------------------------------------------------------------ */
80 public long decrement()
85 /* ------------------------------------------------------------ */
94 /* ------------------------------------------------------------ */
96 * @return current value
98 public long getCurrent()
103 /* ------------------------------------------------------------ */
105 * @return total value
107 public long getTotal()
112 /* ------------------------------------------------------------ */
114 public String toString()
116 return String.format("%s@%x{c=%d,m=%d,t=%d}",this.getClass().getSimpleName(),hashCode(),_curr.get(),_max.get(),_total.get());