2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2021 Nordix Foundation.
4 * ================================================================================
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.clamp.controlloop.runtime.supervision;
23 import java.time.Instant;
24 import java.util.HashMap;
25 import java.util.HashSet;
31 public class HandleCounter<K> {
34 private long maxWaitMs;
38 private int maxRetryCount;
40 private Map<K, Integer> mapCounter = new HashMap<>();
41 private Set<K> mapFault = new HashSet<>();
42 private Map<K, Long> mapTimer = new HashMap<>();
44 public long getDuration(K id) {
45 mapTimer.putIfAbsent(id, getEpochMilli());
46 return getEpochMilli() - mapTimer.get(id);
50 * Reset timer and clear counter and fault by id.
54 public void clear(K id) {
56 mapCounter.put(id, 0);
57 mapTimer.put(id, getEpochMilli());
61 * Remove counter, timer and fault by id.
65 public void remove(K id) {
67 mapCounter.remove(id);
71 public void setFault(K id) {
72 mapCounter.put(id, 0);
77 * Increment RetryCount by id e return true if minor or equal of maxRetryCount.
79 * @param id the identifier
80 * @return false if count is major of maxRetryCount
82 public boolean count(K id) {
83 int counter = mapCounter.getOrDefault(id, 0) + 1;
84 if (counter <= maxRetryCount) {
85 mapCounter.put(id, counter);
91 public boolean isFault(K id) {
92 return mapFault.contains(id);
95 public int getCounter(K id) {
96 return mapCounter.getOrDefault(id, 0);
99 protected long getEpochMilli() {
100 return Instant.now().toEpochMilli();
103 public Set<K> keySet() {
104 return mapCounter.keySet();