ce5a39629e85aec9d3314c0f311546e381171f69
[appc.git] / appc-metric / appc-metric-bundle / src / main / java / org / openecomp / appc / metricservice / metric / impl / DefaultPrimitiveCounter.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP : APPC
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Copyright (C) 2017 Amdocs
8  * =============================================================================
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  * 
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  * 
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  * 
21  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22  * ============LICENSE_END=========================================================
23  */
24
25 package org.openecomp.appc.metricservice.metric.impl;
26
27 import org.openecomp.appc.metricservice.metric.MetricType;
28 import org.openecomp.appc.metricservice.metric.PrimitiveCounter;
29
30 import java.text.SimpleDateFormat;
31 import java.util.Calendar;
32 import java.util.HashMap;
33 import java.util.concurrent.atomic.AtomicLong;
34
35
36 public class DefaultPrimitiveCounter implements PrimitiveCounter {
37     private String name;
38     private MetricType metricType;
39     private AtomicLong counter = new AtomicLong();
40
41     private final SimpleDateFormat dateTimeFormat = new SimpleDateFormat("YYYY-MM-dd:HH:mm:ss");
42     private String lastResetTime = dateTimeFormat.format(Calendar.getInstance().getTime());
43
44     public DefaultPrimitiveCounter(String name, MetricType metricType, long counter) {
45         this.name = name;
46         this.metricType = metricType;
47         this.counter.set(counter);
48     }
49
50     public DefaultPrimitiveCounter(String name, MetricType metricType) {
51         this.counter.set(0);
52         this.name = name;
53         this.metricType = metricType;
54     }
55
56     @Override
57     public void increment() {
58         increment(1);
59     }
60
61     @Override
62     public void increment(long value) {
63         this.counter.incrementAndGet();
64     }
65
66     @Override
67     public void decrement() {
68         decrement(1);
69     }
70
71     @Override
72     public void decrement(long value) {
73         this.counter.decrementAndGet();
74     }
75
76     @Override
77     public long value() {
78         return this.counter.get();
79     }
80
81     @Override
82     public String name() {
83         return this.name;
84     }
85
86     @Override
87     public void reset() {
88         this.counter.set(0);
89         Calendar cal = Calendar.getInstance();
90         lastResetTime = dateTimeFormat.format(cal.getTime());
91     }
92
93     @Override
94     public String toString() {
95         return "DefaultPrimitiveCounter{" + "name='" + name + '\'' + ", metricType=" + metricType + ", counter="
96                 + counter.get() + '}';
97     }
98
99     @Override
100     public MetricType type() {
101         return this.metricType;
102     }
103
104     @Override
105     public HashMap<String, String> getMetricsOutput() {
106         return new HashMap<>();
107     }
108
109     @Override
110     public String getLastModified() {
111         return lastResetTime;
112     }
113 }