blob: f9a08f87a66e032232f07f5a27c7dad46e9f00ba [file] [log] [blame]
Ray Milkey2eb91672018-04-24 10:07:52 -07001/*
2 * Copyright 2018-present Open Networking Foundation
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package org.onosproject.store.primitives;
18
19import org.onosproject.store.service.AsyncAtomicCounterMap;
20
21import java.util.HashMap;
22import java.util.Map;
23import java.util.concurrent.CompletableFuture;
24
25public class AsyncAtomicCounterMapAdapter<K> implements AsyncAtomicCounterMap<K> {
26 private Map<K, Long> map = new HashMap<>();
27
28 @Override
29 public CompletableFuture<Long> incrementAndGet(K key) {
30 Long value = map.getOrDefault(key, 0L) + 1;
31 map.put(key, value);
32 return CompletableFuture.completedFuture(value);
33
34 }
35
36 @Override
37 public CompletableFuture<Long> decrementAndGet(K key) {
38 Long value = map.getOrDefault(key, 0L) - 1;
39 map.put(key, value);
40 return CompletableFuture.completedFuture(value);
41
42 }
43
44 @Override
45 public CompletableFuture<Long> getAndIncrement(K key) {
46 Long value = map.getOrDefault(key, 0L);
47 map.put(key, value + 1);
48 return CompletableFuture.completedFuture(value);
49
50 }
51
52 @Override
53 public CompletableFuture<Long> getAndDecrement(K key) {
54 Long value = map.getOrDefault(key, 0L);
55 map.put(key, value - 1);
56 return CompletableFuture.completedFuture(value);
57 }
58
59 @Override
60 public CompletableFuture<Long> addAndGet(K key, long delta) {
61 Long value = map.getOrDefault(key, 0L) + delta;
62 map.put(key, value);
63 return CompletableFuture.completedFuture(value);
64 }
65
66 @Override
67 public CompletableFuture<Long> getAndAdd(K key, long delta) {
68 Long value = map.getOrDefault(key, 0L);
69 map.put(key, value + delta);
70 return CompletableFuture.completedFuture(value);
71 }
72
73 @Override
74 public CompletableFuture<Long> get(K key) {
75 Long value = map.getOrDefault(key, 0L);
76 return CompletableFuture.completedFuture(value);
77 }
78
79 @Override
80 public CompletableFuture<Long> put(K key, long newValue) {
81 Long value = map.getOrDefault(key, 0L);
82 map.put(key, newValue);
83 return CompletableFuture.completedFuture(value);
84 }
85
86 @Override
87 public CompletableFuture<Long> putIfAbsent(K key, long newValue) {
88 Long value = map.putIfAbsent(key, newValue);
89 if (value == null) {
90 value = 0L;
91 }
92 return CompletableFuture.completedFuture(value);
93 }
94
95 @Override
96 public CompletableFuture<Boolean> replace(K key, long expectedOldValue, long newValue) {
97 boolean value = map.replace(key, expectedOldValue, newValue);
98 return CompletableFuture.completedFuture(value);
99 }
100
101 @Override
102 public CompletableFuture<Long> remove(K key) {
103 Long value = map.remove(key);
104 return CompletableFuture.completedFuture(value);
105 }
106
107 @Override
108 public CompletableFuture<Boolean> remove(K key, long value) {
109 boolean result = map.remove(key, value);
110 return CompletableFuture.completedFuture(result);
111 }
112
113 @Override
114 public CompletableFuture<Integer> size() {
115 int value = map.size();
116 return CompletableFuture.completedFuture(value);
117 }
118
119 @Override
120 public CompletableFuture<Boolean> isEmpty() {
121 boolean value = map.isEmpty();
122 return CompletableFuture.completedFuture(value);
123 }
124
125
126
127 @Override
128 public CompletableFuture<Void> clear() {
129 map.clear();
130 return CompletableFuture.completedFuture(null);
131
132 }
133
134 @Override
135 public String name() {
136 return null;
137 }
138}