Mass replace CRLF->LF
[openflowjava.git] / openflow-protocol-impl / src / main / java / org / opendaylight / openflowjava / statistics / Counter.java
1 /*
2  * Copyright (c) 2014 Pantheon Technologies s.r.o. and others. All rights reserved.
3  *
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
7  */
8
9 package org.opendaylight.openflowjava.statistics;
10
11 import java.util.concurrent.atomic.AtomicLong;
12 /**
13  * Counts statistics
14  * 
15  * @author madamjak
16  */
17 public class Counter {
18
19     private AtomicLong counterValue;
20     private AtomicLong counterLastReadValue;
21
22     /**
23      * Default constructor
24      */
25     public Counter() {
26         counterValue = new AtomicLong(0L);
27         counterLastReadValue = new AtomicLong(0L);
28     }
29
30     /**
31      * Increment current counter value
32      */
33     public void incrementCounter(){
34         counterValue.incrementAndGet();
35     }
36
37     /**
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
41      */
42     public long getCounterLastReadValue() {
43         return counterLastReadValue.get();
44     }
45
46     /**
47      * get current value of counter and rewrite CounterLastReadValue by current value
48      * @return  the current value of counter
49      */
50     public long getCounterValue() {
51         return getCounterValue(true);
52     }
53
54     /**
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
60      */
61     public long getCounterValue(boolean modifyLastReadValue) {
62         if(modifyLastReadValue){
63             counterLastReadValue.set(counterValue.get());
64         }
65         return counterValue.get();
66     }
67
68     /**
69      * set current counter value and CounterLastReadValue to 0 (zero)
70      */
71     public void reset(){
72         counterValue.set(0l);
73         counterLastReadValue.set(0l);
74     }
75
76     /**
77      * @return last and current count for specified statistic
78      */
79     public String getStat() {
80         long cntPrevVal = getCounterLastReadValue();
81         long cntCurValue = getCounterValue();
82         return String.format("+%d | %d",cntCurValue-cntPrevVal,cntCurValue);
83     }
84 }