2 * Copyright (c) 2014 Pantheon Technologies s.r.o. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
9 package org.opendaylight.openflowjava.statistics;
11 import java.util.concurrent.atomic.AtomicLong;
17 public class Counter {
19 private AtomicLong counterValue;
20 private AtomicLong counterLastReadValue;
26 counterValue = new AtomicLong(0L);
27 counterLastReadValue = new AtomicLong(0L);
31 * Increment current counter value
33 public void incrementCounter(){
34 counterValue.incrementAndGet();
38 * return the last read value of counter. This value can be set during the reading of current counter value,
39 * for detail see method getCounterValue(boolean modifyLastReadValue).
40 * @return the counterLastReadValue
42 public long getCounterLastReadValue() {
43 return counterLastReadValue.get();
47 * get current value of counter and rewrite CounterLastReadValue by current value
48 * @return the current value of counter
50 public long getCounterValue() {
51 return getCounterValue(true);
55 * get current counter value
56 * @param modifyLastReadValue
57 * true - CounterLastReadValue will be rewritten by current CounterValue
58 * false - no change CounterLastReadValue
59 * @return the current value of counter
61 public long getCounterValue(boolean modifyLastReadValue) {
62 if(modifyLastReadValue){
63 counterLastReadValue.set(counterValue.get());
65 return counterValue.get();
69 * set current counter value and CounterLastReadValue to 0 (zero)
73 counterLastReadValue.set(0l);
77 * @return last and current count for specified statistic
79 public String getStat() {
80 long cntPrevVal = getCounterLastReadValue();
81 long cntCurValue = getCounterValue();
82 return String.format("+%d | %d",cntCurValue-cntPrevVal,cntCurValue);