Pier Luigi | f094c61 | 2017-10-14 12:15:02 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2017-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 | |
| 17 | package org.onosproject.store.service; |
| 18 | |
| 19 | import com.google.common.util.concurrent.AtomicLongMap; |
| 20 | |
| 21 | /** |
| 22 | * Test implementation of the atomic counter map. |
| 23 | */ |
| 24 | public final class TestAtomicCounterMap<K> extends AtomicCounterMapAdapter<K> { |
| 25 | |
| 26 | // Map name |
| 27 | private final String atomicCounterMapName; |
| 28 | // Atomic long map from guava |
| 29 | private AtomicLongMap<K> map; |
| 30 | |
| 31 | private TestAtomicCounterMap(String name) { |
| 32 | // Init name, map using create |
| 33 | atomicCounterMapName = name; |
| 34 | map = AtomicLongMap.create(); |
| 35 | } |
| 36 | |
| 37 | @Override |
| 38 | public long incrementAndGet(K key) { |
| 39 | // Forward directly to the guava map |
| 40 | return map.incrementAndGet(key); |
| 41 | } |
| 42 | |
| 43 | @Override |
| 44 | public long decrementAndGet(K key) { |
| 45 | // Forward directly to the guava map |
| 46 | return map.decrementAndGet(key); |
| 47 | } |
| 48 | |
| 49 | @Override |
| 50 | public long getAndIncrement(K key) { |
| 51 | // Forward directly to the guava map |
| 52 | return map.getAndIncrement(key); |
| 53 | } |
| 54 | |
| 55 | @Override |
| 56 | public long getAndDecrement(K key) { |
| 57 | // Forward directly to the guava map |
| 58 | return map.getAndDecrement(key); |
| 59 | } |
| 60 | |
| 61 | @Override |
| 62 | public long addAndGet(K key, long delta) { |
| 63 | // Forward directly to the guava map |
| 64 | return map.addAndGet(key, delta); |
| 65 | } |
| 66 | |
| 67 | @Override |
| 68 | public long getAndAdd(K key, long delta) { |
| 69 | // Forward directly to the guava map |
| 70 | return map.getAndAdd(key, delta); |
| 71 | } |
| 72 | |
| 73 | @Override |
| 74 | public long get(K key) { |
| 75 | // Forward directly to the guava map |
| 76 | return map.get(key); |
| 77 | } |
| 78 | |
| 79 | @Override |
| 80 | public long put(K key, long newValue) { |
| 81 | // Forward directly to the guava map |
| 82 | return map.put(key, newValue); |
| 83 | } |
| 84 | |
| 85 | @Override |
| 86 | // Coarse implementation, should we take the lock ? |
| 87 | public long putIfAbsent(K key, long newValue) { |
| 88 | // If it does not exist |
| 89 | if (!map.containsKey(key)) { |
| 90 | // Just do a put |
| 91 | return map.put(key, newValue); |
| 92 | } else { |
| 93 | // Let's return the existing value |
| 94 | return map.get(key); |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | @Override |
| 99 | // Coarse implementation, should we take the lock ? |
| 100 | public boolean replace(K key, long expectedOldValue, long newValue) { |
| 101 | // If the value exists and it the expected one |
| 102 | if (map.containsKey(key) && map.get(key) == expectedOldValue) { |
| 103 | // Let's put the value |
| 104 | map.put(key, newValue); |
| 105 | // Done, return true |
| 106 | return true; |
| 107 | |
| 108 | } else if (!map.containsKey(key) && expectedOldValue == 0) { |
| 109 | // If the value does not exist, and old value is 0 |
| 110 | map.put(key, newValue); |
| 111 | // Done, return true |
| 112 | return true; |
| 113 | } else { |
| 114 | // replace is not possible, just return false |
| 115 | return false; |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | @Override |
| 120 | public long remove(K key) { |
| 121 | // Forward directly to the guava map |
| 122 | return map.remove(key); |
| 123 | } |
| 124 | |
| 125 | @Override |
| 126 | // Coarse implementation, should we take the lock ? |
| 127 | public boolean remove(K key, long value) { |
| 128 | // If the value exists and it is equal to value |
| 129 | if (map.containsKey(key) && map.get(key) == value) { |
| 130 | // Let's remove the value |
| 131 | map.remove(key); |
| 132 | // Done, return true |
| 133 | return true; |
| 134 | } else { |
| 135 | // remove is not possible, just return false |
| 136 | return false; |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | @Override |
| 141 | public int size() { |
| 142 | return map.size(); |
| 143 | } |
| 144 | |
| 145 | @Override |
| 146 | public boolean isEmpty() { |
| 147 | return map.isEmpty(); |
| 148 | } |
| 149 | |
| 150 | @Override |
| 151 | public void clear() { |
| 152 | map.clear(); |
| 153 | } |
| 154 | |
| 155 | @Override |
| 156 | public String name() { |
| 157 | return atomicCounterMapName; |
| 158 | } |
| 159 | |
| 160 | public static Builder builder() { |
| 161 | return new Builder(); |
| 162 | } |
| 163 | |
| 164 | public static class Builder<K> extends AtomicCounterMapBuilder<K> { |
| 165 | |
| 166 | @Override |
| 167 | public AtomicCounterMap<K> build() { |
| 168 | return new TestAtomicCounterMap<>(name()); |
| 169 | } |
| 170 | |
| 171 | @Override |
| 172 | public AsyncAtomicCounterMap<K> buildAsyncMap() { |
| 173 | return null; |
| 174 | } |
| 175 | } |
| 176 | |
| 177 | } |