Format ONAP-XACML and add JUnit
[policy/engine.git] / ONAP-XACML / src / main / java / org / onap / policy / xacml / util / MetricsUtil.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP-XACML
4  * ================================================================================
5  * Copyright (C) 2017-2019 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.xacml.util;
22
23 public class MetricsUtil {
24
25     private MetricsUtil() {
26         //
27         // private constructor to hide the implicit public one for utility class
28         //
29     }
30
31     public static class AvgLatency {
32         private long cumLatency = 0;
33         private long count = 0;
34
35         public void compute(long latency) {
36             cumLatency += latency;
37             count++;
38         }
39
40         public long avg() {
41             if (count == 0)
42                 return 0;
43
44             return cumLatency / count;
45         }
46
47         public void reset() {
48             cumLatency = 0;
49             count = 0;
50         }
51     }
52
53     public static class MinLatency {
54         private long min = Long.MAX_VALUE;
55
56         public synchronized void compute(long ts) {
57             if (ts < min)
58                 min = ts;
59         }
60
61         public long min() {
62             return min;
63         }
64
65         public void reset() {
66             min = Long.MAX_VALUE;
67         }
68     }
69
70     public static class MaxLatency {
71         private long max = Long.MIN_VALUE;
72
73         public synchronized void compute(long ts) {
74             if (ts > max)
75                 max = ts;
76         }
77
78         public long max() {
79             return max;
80         }
81
82         public void reset() {
83             max = Long.MIN_VALUE;
84         }
85     }
86
87 }