View Javadoc

1   //
2   //  ========================================================================
3   //  Copyright (c) 1995-2013 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 void add(final long delta)
59      {
60          long value=_curr.addAndGet(delta);
61          if (delta > 0)
62              _total.addAndGet(delta);
63          Atomics.updateMax(_max,value);
64      }
65  
66      /* ------------------------------------------------------------ */
67      /**
68       * @param delta the amount to subtract the count by.
69       */
70      public void subtract(final long delta)
71      {
72          add(-delta);
73      }
74  
75      /* ------------------------------------------------------------ */
76      /**
77       */
78      public void increment()
79      {
80          add(1);
81      }
82  
83      /* ------------------------------------------------------------ */
84      /**
85       */
86      public void decrement()
87      {
88          add(-1);
89      }
90  
91      /* ------------------------------------------------------------ */
92      /**
93       * @return max value
94       */
95      public long getMax()
96      {
97          return _max.get();
98      }
99  
100     /* ------------------------------------------------------------ */
101     /**
102      * @return current value
103      */
104     public long getCurrent()
105     {
106         return _curr.get();
107     }
108 
109     /* ------------------------------------------------------------ */
110     /**
111      * @return total value
112      */
113     public long getTotal()
114     {
115         return _total.get();
116     }
117 }