Jordi Ortiz | 6d17a49 | 2017-02-14 18:15:34 +0100 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2017-present Open Networking Foundation |
Jordi Ortiz | 6d17a49 | 2017-02-14 18:15:34 +0100 | [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 | import com.google.common.collect.HashMultimap; |
| 19 | import com.google.common.collect.Multiset; |
| 20 | |
| 21 | import java.util.Collection; |
Jordan Halterman | 21ef9e4 | 2018-05-21 22:11:07 -0700 | [diff] [blame] | 22 | import java.util.Iterator; |
Jordi Ortiz | 6d17a49 | 2017-02-14 18:15:34 +0100 | [diff] [blame] | 23 | import java.util.Map; |
| 24 | import java.util.Set; |
Jonathan Hart | 46bf89b | 2017-02-27 15:56:42 -0800 | [diff] [blame] | 25 | import java.util.concurrent.Executor; |
Jordi Ortiz | 6d17a49 | 2017-02-14 18:15:34 +0100 | [diff] [blame] | 26 | import java.util.concurrent.atomic.AtomicLong; |
pier | fa48c6e | 2019-10-11 18:19:59 +0200 | [diff] [blame^] | 27 | import java.util.stream.Collectors; |
Jordi Ortiz | 6d17a49 | 2017-02-14 18:15:34 +0100 | [diff] [blame] | 28 | |
| 29 | /** |
| 30 | * Implementation to test ConsistentMultimap. Very limited. |
| 31 | * No listener notification. Some methods (still not required) not implemented. |
| 32 | * @param <K> the key type |
| 33 | * @param <V> the value type |
| 34 | */ |
| 35 | public class TestConsistentMultimap<K, V> implements ConsistentMultimap<K, V> { |
| 36 | |
| 37 | private String name; |
| 38 | private HashMultimap<K, Versioned<V>> innermap; |
| 39 | private AtomicLong counter = new AtomicLong(); |
| 40 | |
| 41 | public TestConsistentMultimap() { |
| 42 | this.innermap = HashMultimap.create(); |
| 43 | } |
| 44 | |
| 45 | private Versioned<V> version(V v) { |
| 46 | return new Versioned<>(v, counter.incrementAndGet(), System.currentTimeMillis()); |
| 47 | } |
| 48 | |
| 49 | @Override |
| 50 | public int size() { |
| 51 | return innermap.size(); |
| 52 | } |
| 53 | |
| 54 | @Override |
| 55 | public boolean isEmpty() { |
| 56 | return innermap.isEmpty(); |
| 57 | } |
| 58 | |
| 59 | @Override |
| 60 | public boolean containsKey(K key) { |
| 61 | return innermap.containsKey(key); |
| 62 | } |
| 63 | |
| 64 | @Override |
| 65 | public boolean containsValue(V value) { |
| 66 | return innermap.containsValue(value); |
| 67 | } |
| 68 | |
| 69 | @Override |
| 70 | public boolean containsEntry(K key, V value) { |
| 71 | return innermap.containsEntry(key, value); |
| 72 | } |
| 73 | |
| 74 | @Override |
| 75 | public boolean put(K key, V value) { |
| 76 | return innermap.put(key, version(value)); |
| 77 | } |
| 78 | |
| 79 | @Override |
Jordan Halterman | 99c654d | 2018-06-04 14:53:06 -0700 | [diff] [blame] | 80 | public Versioned<Collection<? extends V>> putAndGet(K key, V value) { |
| 81 | innermap.put(key, version(value)); |
| 82 | return (Versioned<Collection<? extends V>>) innermap.get(key); |
| 83 | } |
| 84 | |
| 85 | @Override |
Jordi Ortiz | 6d17a49 | 2017-02-14 18:15:34 +0100 | [diff] [blame] | 86 | public boolean remove(K key, V value) { |
| 87 | return innermap.remove(key, value); |
| 88 | } |
| 89 | |
| 90 | @Override |
Jordan Halterman | 99c654d | 2018-06-04 14:53:06 -0700 | [diff] [blame] | 91 | public Versioned<Collection<? extends V>> removeAndGet(K key, V value) { |
| 92 | innermap.remove(key, value); |
| 93 | return (Versioned<Collection<? extends V>>) innermap.get(key); |
| 94 | } |
| 95 | |
| 96 | @Override |
Jordi Ortiz | 6d17a49 | 2017-02-14 18:15:34 +0100 | [diff] [blame] | 97 | public boolean removeAll(K key, Collection<? extends V> values) { |
| 98 | return false; |
| 99 | } |
| 100 | |
| 101 | @Override |
| 102 | public Versioned<Collection<? extends V>> removeAll(K key) { |
| 103 | return null; |
| 104 | } |
| 105 | |
| 106 | @Override |
pier | fa48c6e | 2019-10-11 18:19:59 +0200 | [diff] [blame^] | 107 | public boolean removeAll(Map<K, Collection<? extends V>> mapping) { |
| 108 | // Semantic is that any change in the map should return true |
| 109 | boolean result = false; |
| 110 | for (Map.Entry<K, Collection<? extends V>> entry : mapping.entrySet()) { |
| 111 | Collection<? extends Versioned<V>> versionedValues = entry.getValue().stream() |
| 112 | .map(this::version) |
| 113 | .collect(Collectors.toList()); |
| 114 | for (Versioned<V> value : versionedValues) { |
| 115 | if (innermap.remove(entry.getKey(), value)) { |
| 116 | result = true; |
| 117 | } |
| 118 | } |
| 119 | } |
| 120 | return result; |
| 121 | } |
| 122 | |
| 123 | @Override |
Jordi Ortiz | 6d17a49 | 2017-02-14 18:15:34 +0100 | [diff] [blame] | 124 | public boolean putAll(K key, Collection<? extends V> values) { |
| 125 | return false; |
| 126 | } |
| 127 | |
| 128 | @Override |
pier | fa48c6e | 2019-10-11 18:19:59 +0200 | [diff] [blame^] | 129 | public boolean putAll(Map<K, Collection<? extends V>> mapping) { |
| 130 | // Semantic is that any change in the map should return true |
| 131 | boolean result = false; |
| 132 | for (Map.Entry<K, Collection<? extends V>> entry : mapping.entrySet()) { |
| 133 | Collection<? extends Versioned<V>> versionedValues = entry.getValue().stream() |
| 134 | .map(this::version).collect(Collectors.toList()); |
| 135 | if (innermap.putAll(entry.getKey(), versionedValues)) { |
| 136 | result = true; |
| 137 | } |
| 138 | } |
| 139 | return result; |
| 140 | } |
| 141 | |
| 142 | @Override |
Jordi Ortiz | 6d17a49 | 2017-02-14 18:15:34 +0100 | [diff] [blame] | 143 | public Versioned<Collection<? extends V>> replaceValues(K key, Collection<V> values) { |
| 144 | return null; |
| 145 | } |
| 146 | |
| 147 | @Override |
| 148 | public void clear() { |
| 149 | innermap.clear(); |
| 150 | } |
| 151 | |
| 152 | @Override |
| 153 | public Versioned<Collection<? extends V>> get(K key) { |
| 154 | return (Versioned<Collection<? extends V>>) innermap.get(key); |
| 155 | } |
| 156 | |
| 157 | @Override |
| 158 | public Set<K> keySet() { |
| 159 | return innermap.keySet(); |
| 160 | } |
| 161 | |
| 162 | @Override |
| 163 | public Multiset<K> keys() { |
| 164 | return innermap.keys(); |
| 165 | } |
| 166 | |
| 167 | @Override |
| 168 | public Multiset<V> values() { |
| 169 | return null; |
| 170 | } |
| 171 | |
| 172 | @Override |
| 173 | public Collection<Map.Entry<K, V>> entries() { |
| 174 | return null; |
| 175 | } |
| 176 | |
| 177 | @Override |
Jordan Halterman | 21ef9e4 | 2018-05-21 22:11:07 -0700 | [diff] [blame] | 178 | public Iterator<Map.Entry<K, V>> iterator() { |
| 179 | return null; |
| 180 | } |
| 181 | |
| 182 | @Override |
Jordi Ortiz | 6d17a49 | 2017-02-14 18:15:34 +0100 | [diff] [blame] | 183 | public Map<K, Collection<V>> asMap() { |
| 184 | return null; |
| 185 | } |
| 186 | |
| 187 | @Override |
Jonathan Hart | 46bf89b | 2017-02-27 15:56:42 -0800 | [diff] [blame] | 188 | public void addListener(MultimapEventListener<K, V> listener, Executor executor) { |
| 189 | } |
| 190 | |
| 191 | @Override |
| 192 | public void removeListener(MultimapEventListener<K, V> listener) { |
| 193 | } |
| 194 | |
| 195 | @Override |
Jordi Ortiz | 6d17a49 | 2017-02-14 18:15:34 +0100 | [diff] [blame] | 196 | public String name() { |
| 197 | return this.name; |
| 198 | } |
| 199 | |
| 200 | @Override |
| 201 | public Type primitiveType() { |
| 202 | return null; |
| 203 | } |
| 204 | |
| 205 | public static TestConsistentMultimap.Builder builder() { |
| 206 | return new TestConsistentMultimap.Builder(); |
| 207 | } |
| 208 | |
| 209 | public static class Builder<K, V> extends ConsistentMultimapBuilder<K, V> { |
| 210 | |
| 211 | @Override |
| 212 | public AsyncConsistentMultimap<K, V> buildMultimap() { |
| 213 | return null; |
| 214 | } |
| 215 | |
| 216 | @Override |
| 217 | public ConsistentMultimap<K, V> build() { |
| 218 | return new TestConsistentMultimap<K, V>(); |
| 219 | } |
| 220 | } |
| 221 | |
| 222 | } |