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; |
| 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 |
| 79 | public boolean remove(K key, V value) { |
| 80 | return innermap.remove(key, value); |
| 81 | } |
| 82 | |
| 83 | @Override |
| 84 | public boolean removeAll(K key, Collection<? extends V> values) { |
| 85 | return false; |
| 86 | } |
| 87 | |
| 88 | @Override |
| 89 | public Versioned<Collection<? extends V>> removeAll(K key) { |
| 90 | return null; |
| 91 | } |
| 92 | |
| 93 | @Override |
| 94 | public boolean putAll(K key, Collection<? extends V> values) { |
| 95 | return false; |
| 96 | } |
| 97 | |
| 98 | @Override |
| 99 | public Versioned<Collection<? extends V>> replaceValues(K key, Collection<V> values) { |
| 100 | return null; |
| 101 | } |
| 102 | |
| 103 | @Override |
| 104 | public void clear() { |
| 105 | innermap.clear(); |
| 106 | } |
| 107 | |
| 108 | @Override |
| 109 | public Versioned<Collection<? extends V>> get(K key) { |
| 110 | return (Versioned<Collection<? extends V>>) innermap.get(key); |
| 111 | } |
| 112 | |
| 113 | @Override |
| 114 | public Set<K> keySet() { |
| 115 | return innermap.keySet(); |
| 116 | } |
| 117 | |
| 118 | @Override |
| 119 | public Multiset<K> keys() { |
| 120 | return innermap.keys(); |
| 121 | } |
| 122 | |
| 123 | @Override |
| 124 | public Multiset<V> values() { |
| 125 | return null; |
| 126 | } |
| 127 | |
| 128 | @Override |
| 129 | public Collection<Map.Entry<K, V>> entries() { |
| 130 | return null; |
| 131 | } |
| 132 | |
| 133 | @Override |
Jordan Halterman | 21ef9e4 | 2018-05-21 22:11:07 -0700 | [diff] [blame] | 134 | public Iterator<Map.Entry<K, V>> iterator() { |
| 135 | return null; |
| 136 | } |
| 137 | |
| 138 | @Override |
Jordi Ortiz | 6d17a49 | 2017-02-14 18:15:34 +0100 | [diff] [blame] | 139 | public Map<K, Collection<V>> asMap() { |
| 140 | return null; |
| 141 | } |
| 142 | |
| 143 | @Override |
Jonathan Hart | 46bf89b | 2017-02-27 15:56:42 -0800 | [diff] [blame] | 144 | public void addListener(MultimapEventListener<K, V> listener, Executor executor) { |
| 145 | } |
| 146 | |
| 147 | @Override |
| 148 | public void removeListener(MultimapEventListener<K, V> listener) { |
| 149 | } |
| 150 | |
| 151 | @Override |
Jordi Ortiz | 6d17a49 | 2017-02-14 18:15:34 +0100 | [diff] [blame] | 152 | public String name() { |
| 153 | return this.name; |
| 154 | } |
| 155 | |
| 156 | @Override |
| 157 | public Type primitiveType() { |
| 158 | return null; |
| 159 | } |
| 160 | |
| 161 | public static TestConsistentMultimap.Builder builder() { |
| 162 | return new TestConsistentMultimap.Builder(); |
| 163 | } |
| 164 | |
| 165 | public static class Builder<K, V> extends ConsistentMultimapBuilder<K, V> { |
| 166 | |
| 167 | @Override |
| 168 | public AsyncConsistentMultimap<K, V> buildMultimap() { |
| 169 | return null; |
| 170 | } |
| 171 | |
| 172 | @Override |
| 173 | public ConsistentMultimap<K, V> build() { |
| 174 | return new TestConsistentMultimap<K, V>(); |
| 175 | } |
| 176 | } |
| 177 | |
| 178 | } |