Update license header in appc lifecycle and metric
[appc.git] / appc-metric / appc-metric-bundle / src / main / java / org / onap / appc / metricservice / metric / impl / DefaultPrimitiveCounter.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP : APPC
4  * ================================================================================
5  * Copyright (C) 2017-2018 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  * ============LICENSE_END=========================================================
22  */
23
24 package org.onap.appc.metricservice.metric.impl;
25
26 import org.onap.appc.metricservice.metric.MetricType;
27 import org.onap.appc.metricservice.metric.PrimitiveCounter;
28
29 import java.text.SimpleDateFormat;
30 import java.util.Calendar;
31 import java.util.HashMap;
32 import java.util.concurrent.atomic.AtomicLong;
33
34
35 public class DefaultPrimitiveCounter implements PrimitiveCounter {
36     private String name;
37     private MetricType metricType;
38     private AtomicLong counter = new AtomicLong();
39
40     private final SimpleDateFormat dateTimeFormat = new SimpleDateFormat("YYYY-MM-dd:HH:mm:ss");
41     private String lastResetTime = dateTimeFormat.format(Calendar.getInstance().getTime());
42
43     public DefaultPrimitiveCounter(String name, MetricType metricType, long counter) {
44         this.name = name;
45         this.metricType = metricType;
46         this.counter.set(counter);
47     }
48
49     public DefaultPrimitiveCounter(String name, MetricType metricType) {
50         this.counter.set(0);
51         this.name = name;
52         this.metricType = metricType;
53     }
54
55     @Override
56     public void increment() {
57         increment(1);
58     }
59
60     @Override
61     public void increment(long value) {
62         this.counter.incrementAndGet();
63     }
64
65     @Override
66     public void decrement() {
67         decrement(1);
68     }
69
70     @Override
71     public void decrement(long value) {
72         this.counter.decrementAndGet();
73     }
74
75     @Override
76     public long value() {
77         return this.counter.get();
78     }
79
80     @Override
81     public String name() {
82         return this.name;
83     }
84
85     @Override
86     public void reset() {
87         this.counter.set(0);
88         Calendar cal = Calendar.getInstance();
89         lastResetTime = dateTimeFormat.format(cal.getTime());
90     }
91
92     @Override
93     public String toString() {
94         return "DefaultPrimitiveCounter{" + "name='" + name + '\'' + ", metricType=" + metricType + ", counter="
95                 + counter.get() + '}';
96     }
97
98     @Override
99     public MetricType type() {
100         return this.metricType;
101     }
102
103     @Override
104     public HashMap<String, String> getMetricsOutput() {
105         return new HashMap<>();
106     }
107
108     @Override
109     public String getLastModified() {
110         return lastResetTime;
111     }
112 }