Jihwan Kim | 1f53627 | 2015-12-18 03:10:09 +0900 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Foundation |
Jihwan Kim | 1f53627 | 2015-12-18 03:10:09 +0900 | [diff] [blame] | 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 | package org.onosproject.store.service; |
| 17 | |
| 18 | /** |
| 19 | * Distributed version of com.google.common.util.concurrent.AtomicLongMap. |
| 20 | */ |
Jihwan Kim | cf8f6b2 | 2016-11-12 13:07:04 +0900 | [diff] [blame] | 21 | public interface AtomicCounterMap<K> extends DistributedPrimitive { |
| 22 | |
| 23 | @Override |
| 24 | default DistributedPrimitive.Type primitiveType() { |
| 25 | return DistributedPrimitive.Type.COUNTER_MAP; |
| 26 | } |
Jihwan Kim | 1f53627 | 2015-12-18 03:10:09 +0900 | [diff] [blame] | 27 | |
| 28 | /** |
| 29 | * Increments by one the value currently associated with key, and returns the new value. |
| 30 | * |
| 31 | * @param key key with which the specified value is to be associated |
Ray Milkey | d7909ca | 2016-03-04 08:46:13 -0800 | [diff] [blame] | 32 | * @return incremented value |
Jihwan Kim | 1f53627 | 2015-12-18 03:10:09 +0900 | [diff] [blame] | 33 | */ |
| 34 | long incrementAndGet(K key); |
| 35 | |
| 36 | /** |
| 37 | * Decrements by one the value currently associated with key, and returns the new value. |
| 38 | * |
| 39 | * @param key key with which the specified value is to be associated |
| 40 | * @return updated value |
| 41 | */ |
| 42 | long decrementAndGet(K key); |
| 43 | |
| 44 | /** |
| 45 | * Increments by one the value currently associated with key, and returns the old value. |
| 46 | * |
| 47 | * @param key key with which the specified value is to be associated |
| 48 | * @return previous value |
| 49 | */ |
| 50 | long getAndIncrement(K key); |
| 51 | |
| 52 | /** |
| 53 | * Decrements by one the value currently associated with key, and returns the old value. |
| 54 | * |
| 55 | * @param key key with which the specified value is to be associated |
| 56 | * @return previous value |
| 57 | */ |
| 58 | long getAndDecrement(K key); |
| 59 | |
| 60 | /** |
| 61 | * Adds delta to the value currently associated with key, and returns the new value. |
| 62 | * |
| 63 | * @param key key with which the specified value is to be associated |
| 64 | * @param delta the value to add |
| 65 | * @return updated value |
| 66 | */ |
| 67 | long addAndGet(K key, long delta); |
| 68 | |
| 69 | /** |
| 70 | * Adds delta to the value currently associated with key, and returns the old value. |
| 71 | * |
| 72 | * @param key key with which the specified value is to be associated |
| 73 | * @param delta the value to add |
| 74 | * @return previous value |
| 75 | */ |
| 76 | long getAndAdd(K key, long delta); |
| 77 | |
| 78 | /** |
| 79 | * Returns the value associated with key, or zero if there is no value associated with key. |
| 80 | * |
| 81 | * @param key key with which the specified value is to be associated |
| 82 | * @return current value |
| 83 | */ |
| 84 | long get(K key); |
| 85 | |
| 86 | /** |
| 87 | * Associates ewValue with key in this map, and returns the value previously |
| 88 | * associated with key, or zero if there was no such value. |
| 89 | * |
| 90 | * @param key key with which the specified value is to be associated |
| 91 | * @param newValue the value to put |
| 92 | * @return previous value or zero |
| 93 | */ |
| 94 | long put(K key, long newValue); |
| 95 | |
Jihwan Kim | 1f53627 | 2015-12-18 03:10:09 +0900 | [diff] [blame] | 96 | /** |
| 97 | * If key is not already associated with a value or if key is associated with |
| 98 | * zero, associate it with newValue. Returns the previous value associated with |
| 99 | * key, or zero if there was no mapping for key. |
| 100 | * |
| 101 | * @param key key with which the specified value is to be associated |
| 102 | * @param newValue the value to put |
| 103 | * @return previous value or zero |
| 104 | */ |
| 105 | long putIfAbsent(K key, long newValue); |
| 106 | |
| 107 | /** |
| 108 | * If (key, expectedOldValue) is currently in the map, this method replaces |
| 109 | * expectedOldValue with newValue and returns true; otherwise, this method return false. |
| 110 | * |
| 111 | * If expectedOldValue is zero, this method will succeed if (key, zero) |
| 112 | * is currently in the map, or if key is not in the map at all. |
| 113 | * |
| 114 | * @param key key with which the specified value is to be associated |
| 115 | * @param expectedOldValue the expected value |
| 116 | * @param newValue the value to replace |
| 117 | * @return true if the value was replaced, false otherwise |
| 118 | */ |
| 119 | boolean replace(K key, long expectedOldValue, long newValue); |
| 120 | |
| 121 | /** |
| 122 | * Removes and returns the value associated with key. If key is not |
| 123 | * in the map, this method has no effect and returns zero. |
| 124 | * |
| 125 | * @param key key with which the specified value is to be associated |
| 126 | * @return the previous value associated with the specified key or null |
| 127 | */ |
| 128 | long remove(K key); |
| 129 | |
| 130 | /** |
| 131 | * If (key, value) is currently in the map, this method removes it and returns |
| 132 | * true; otherwise, this method returns false. |
| 133 | * |
| 134 | * @param key key with which the specified value is to be associated |
| 135 | * @param value the value to remove |
| 136 | * @return true if the value was removed, false otherwise |
| 137 | */ |
| 138 | boolean remove(K key, long value); |
Jordan Halterman | c955df7 | 2017-02-04 20:43:28 -0800 | [diff] [blame] | 139 | |
| 140 | /** |
| 141 | * Returns the number of entries in the map. |
| 142 | * |
| 143 | * @return the number of entries in the map, including {@code 0} values |
| 144 | */ |
| 145 | int size(); |
| 146 | |
| 147 | /** |
| 148 | * If the map is empty, returns true, otherwise false. |
| 149 | * |
| 150 | * @return true if the map is empty. |
| 151 | */ |
| 152 | boolean isEmpty(); |
| 153 | |
| 154 | /** |
| 155 | * Clears all entries from the map. |
| 156 | */ |
| 157 | void clear(); |
Jihwan Kim | 1f53627 | 2015-12-18 03:10:09 +0900 | [diff] [blame] | 158 | } |