| 1 | /* |
|---|
| 2 | * $Id$ |
|---|
| 3 | * |
|---|
| 4 | * Copyright 2006, The jCoderZ.org Project. All rights reserved. |
|---|
| 5 | * |
|---|
| 6 | * Redistribution and use in source and binary forms, with or without |
|---|
| 7 | * modification, are permitted provided that the following conditions are |
|---|
| 8 | * met: |
|---|
| 9 | * |
|---|
| 10 | * * Redistributions of source code must retain the above copyright |
|---|
| 11 | * notice, this list of conditions and the following disclaimer. |
|---|
| 12 | * * Redistributions in binary form must reproduce the above |
|---|
| 13 | * copyright notice, this list of conditions and the following |
|---|
| 14 | * disclaimer in the documentation and/or other materials |
|---|
| 15 | * provided with the distribution. |
|---|
| 16 | * * Neither the name of the jCoderZ.org Project nor the names of |
|---|
| 17 | * its contributors may be used to endorse or promote products |
|---|
| 18 | * derived from this software without specific prior written |
|---|
| 19 | * permission. |
|---|
| 20 | * |
|---|
| 21 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS "AS IS" AND |
|---|
| 22 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
|---|
| 23 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
|---|
| 24 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS |
|---|
| 25 | * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
|---|
| 26 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
|---|
| 27 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR |
|---|
| 28 | * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
|---|
| 29 | * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR |
|---|
| 30 | * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF |
|---|
| 31 | * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
|---|
| 32 | */ |
|---|
| 33 | package org.jcoderz.commons.statistics; |
|---|
| 34 | |
|---|
| 35 | /** |
|---|
| 36 | * This class implements a simple counter that can be incremented and |
|---|
| 37 | * decremented. |
|---|
| 38 | * The class is thread-safe. |
|---|
| 39 | * |
|---|
| 40 | * @author Albrecht Messner |
|---|
| 41 | */ |
|---|
| 42 | public class Counter |
|---|
| 43 | implements Resettable |
|---|
| 44 | { |
|---|
| 45 | private final String mName; |
|---|
| 46 | |
|---|
| 47 | /** Stores the counter's value. */ |
|---|
| 48 | private long mCounter = 0; |
|---|
| 49 | |
|---|
| 50 | /** |
|---|
| 51 | * Constructor. |
|---|
| 52 | * @param name the counter's name |
|---|
| 53 | */ |
|---|
| 54 | public Counter (String name) |
|---|
| 55 | { |
|---|
| 56 | mName = name; |
|---|
| 57 | } |
|---|
| 58 | /** |
|---|
| 59 | * Increment the counter by one. |
|---|
| 60 | * @return the incremented counter value |
|---|
| 61 | */ |
|---|
| 62 | public synchronized long increment () |
|---|
| 63 | { |
|---|
| 64 | mCounter++; |
|---|
| 65 | return mCounter; |
|---|
| 66 | } |
|---|
| 67 | |
|---|
| 68 | /** |
|---|
| 69 | * Decrement the counter by one. |
|---|
| 70 | * @return the decremented counter value |
|---|
| 71 | */ |
|---|
| 72 | public synchronized long decrement () |
|---|
| 73 | { |
|---|
| 74 | mCounter--; |
|---|
| 75 | return mCounter; |
|---|
| 76 | } |
|---|
| 77 | |
|---|
| 78 | /** |
|---|
| 79 | * Returns the current value of the counter. |
|---|
| 80 | * @return the current value of the counter. |
|---|
| 81 | */ |
|---|
| 82 | public synchronized long getCount () |
|---|
| 83 | { |
|---|
| 84 | return mCounter; |
|---|
| 85 | } |
|---|
| 86 | |
|---|
| 87 | /** {@inheritDoc} */ |
|---|
| 88 | public synchronized void reset () |
|---|
| 89 | { |
|---|
| 90 | mCounter = 0; |
|---|
| 91 | } |
|---|
| 92 | |
|---|
| 93 | /** |
|---|
| 94 | * @return Returns the name. |
|---|
| 95 | */ |
|---|
| 96 | public String getName () |
|---|
| 97 | { |
|---|
| 98 | return mName; |
|---|
| 99 | } |
|---|
| 100 | |
|---|
| 101 | /** {@inheritDoc} */ |
|---|
| 102 | public String toString () |
|---|
| 103 | { |
|---|
| 104 | return "[Counter: mName=" + getName() + ", mCounter=" + getCount() + "]"; |
|---|
| 105 | } |
|---|
| 106 | } |
|---|