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 | 5e88435 | 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; |
| 27 | |
| 28 | /** |
| 29 | * Implementation to test ConsistentMultimap. Very limited. |
| 30 | * No listener notification. Some methods (still not required) not implemented. |
| 31 | * @param <K> the key type |
| 32 | * @param <V> the value type |
| 33 | */ |
| 34 | public class TestConsistentMultimap<K, V> implements ConsistentMultimap<K, V> { |
| 35 | |
| 36 | private String name; |
| 37 | private HashMultimap<K, Versioned<V>> innermap; |
| 38 | private AtomicLong counter = new AtomicLong(); |
| 39 | |
| 40 | public TestConsistentMultimap() { |
| 41 | this.innermap = HashMultimap.create(); |
| 42 | } |
| 43 | |
| 44 | private Versioned<V> version(V v) { |
| 45 | return new Versioned<>(v, counter.incrementAndGet(), System.currentTimeMillis()); |
| 46 | } |
| 47 | |
| 48 | @Override |
| 49 | public int size() { |
| 50 | return innermap.size(); |
| 51 | } |
| 52 | |
| 53 | @Override |
| 54 | public boolean isEmpty() { |
| 55 | return innermap.isEmpty(); |
| 56 | } |
| 57 | |
| 58 | @Override |
| 59 | public boolean containsKey(K key) { |
| 60 | return innermap.containsKey(key); |
| 61 | } |
| 62 | |
| 63 | @Override |
| 64 | public boolean containsValue(V value) { |
| 65 | return innermap.containsValue(value); |
| 66 | } |
| 67 | |
| 68 | @Override |
| 69 | public boolean containsEntry(K key, V value) { |
| 70 | return innermap.containsEntry(key, value); |
| 71 | } |
| 72 | |
| 73 | @Override |
| 74 | public boolean put(K key, V value) { |
| 75 | return innermap.put(key, version(value)); |
| 76 | } |
| 77 | |
| 78 | @Override |
Jordan Halterman | 8c57a09 | 2018-06-04 14:53:06 -0700 | [diff] [blame] | 79 | public Versioned<Collection<? extends V>> putAndGet(K key, V value) { |
| 80 | innermap.put(key, version(value)); |
| 81 | return (Versioned<Collection<? extends V>>) innermap.get(key); |
| 82 | } |
| 83 | |
| 84 | @Override |
Jordi Ortiz | 6d17a49 | 2017-02-14 18:15:34 +0100 | [diff] [blame] | 85 | public boolean remove(K key, V value) { |
| 86 | return innermap.remove(key, value); |
| 87 | } |
| 88 | |
| 89 | @Override |
Jordan Halterman | 8c57a09 | 2018-06-04 14:53:06 -0700 | [diff] [blame] | 90 | public Versioned<Collection<? extends V>> removeAndGet(K key, V value) { |
| 91 | innermap.remove(key, value); |
| 92 | return (Versioned<Collection<? extends V>>) innermap.get(key); |
| 93 | } |
| 94 | |
| 95 | @Override |
Jordi Ortiz | 6d17a49 | 2017-02-14 18:15:34 +0100 | [diff] [blame] | 96 | public boolean removeAll(K key, Collection<? extends V> values) { |
| 97 | return false; |
| 98 | } |
| 99 | |
| 100 | @Override |
| 101 | public Versioned<Collection<? extends V>> removeAll(K key) { |
| 102 | return null; |
| 103 | } |
| 104 | |
| 105 | @Override |
| 106 | public boolean putAll(K key, Collection<? extends V> values) { |
| 107 | return false; |
| 108 | } |
| 109 | |
| 110 | @Override |
| 111 | public Versioned<Collection<? extends V>> replaceValues(K key, Collection<V> values) { |
| 112 | return null; |
| 113 | } |
| 114 | |
| 115 | @Override |
| 116 | public void clear() { |
| 117 | innermap.clear(); |
| 118 | } |
| 119 | |
| 120 | @Override |
| 121 | public Versioned<Collection<? extends V>> get(K key) { |
| 122 | return (Versioned<Collection<? extends V>>) innermap.get(key); |
| 123 | } |
| 124 | |
| 125 | @Override |
| 126 | public Set<K> keySet() { |
| 127 | return innermap.keySet(); |
| 128 | } |
| 129 | |
| 130 | @Override |
| 131 | public Multiset<K> keys() { |
| 132 | return innermap.keys(); |
| 133 | } |
| 134 | |
| 135 | @Override |
| 136 | public Multiset<V> values() { |
| 137 | return null; |
| 138 | } |
| 139 | |
| 140 | @Override |
| 141 | public Collection<Map.Entry<K, V>> entries() { |
| 142 | return null; |
| 143 | } |
| 144 | |
| 145 | @Override |
Jordan Halterman | 5e88435 | 2018-05-21 22:11:07 -0700 | [diff] [blame] | 146 | public Iterator<Map.Entry<K, V>> iterator() { |
| 147 | return null; |
| 148 | } |
| 149 | |
| 150 | @Override |
Jordi Ortiz | 6d17a49 | 2017-02-14 18:15:34 +0100 | [diff] [blame] | 151 | public Map<K, Collection<V>> asMap() { |
| 152 | return null; |
| 153 | } |
| 154 | |
| 155 | @Override |
Jonathan Hart | 46bf89b | 2017-02-27 15:56:42 -0800 | [diff] [blame] | 156 | public void addListener(MultimapEventListener<K, V> listener, Executor executor) { |
| 157 | } |
| 158 | |
| 159 | @Override |
| 160 | public void removeListener(MultimapEventListener<K, V> listener) { |
| 161 | } |
| 162 | |
| 163 | @Override |
Jordi Ortiz | 6d17a49 | 2017-02-14 18:15:34 +0100 | [diff] [blame] | 164 | public String name() { |
| 165 | return this.name; |
| 166 | } |
| 167 | |
| 168 | @Override |
| 169 | public Type primitiveType() { |
| 170 | return null; |
| 171 | } |
| 172 | |
| 173 | public static TestConsistentMultimap.Builder builder() { |
| 174 | return new TestConsistentMultimap.Builder(); |
| 175 | } |
| 176 | |
| 177 | public static class Builder<K, V> extends ConsistentMultimapBuilder<K, V> { |
| 178 | |
| 179 | @Override |
| 180 | public AsyncConsistentMultimap<K, V> buildMultimap() { |
| 181 | return null; |
| 182 | } |
| 183 | |
| 184 | @Override |
| 185 | public ConsistentMultimap<K, V> build() { |
| 186 | return new TestConsistentMultimap<K, V>(); |
| 187 | } |
| 188 | } |
| 189 | |
| 190 | } |