blob: f70d1863cd3a80f8288142a6cc9f89053cd0e32f [file] [log] [blame]
Jordi Ortiz6d17a492017-02-14 18:15:34 +01001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2017-present Open Networking Foundation
Jordi Ortiz6d17a492017-02-14 18:15:34 +01003 *
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 */
16package org.onosproject.store.service;
17
18import com.google.common.collect.HashMultimap;
19import com.google.common.collect.Multiset;
20
21import java.util.Collection;
Jordan Halterman5e884352018-05-21 22:11:07 -070022import java.util.Iterator;
Jordi Ortiz6d17a492017-02-14 18:15:34 +010023import java.util.Map;
24import java.util.Set;
Jonathan Hart46bf89b2017-02-27 15:56:42 -080025import java.util.concurrent.Executor;
Jordi Ortiz6d17a492017-02-14 18:15:34 +010026import java.util.concurrent.atomic.AtomicLong;
pier4fcb4b22019-10-11 18:19:59 +020027import java.util.stream.Collectors;
Jordi Ortiz6d17a492017-02-14 18:15:34 +010028
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 */
35public 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 Halterman8c57a092018-06-04 14:53:06 -070080 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 Ortiz6d17a492017-02-14 18:15:34 +010086 public boolean remove(K key, V value) {
87 return innermap.remove(key, value);
88 }
89
90 @Override
Jordan Halterman8c57a092018-06-04 14:53:06 -070091 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 Ortiz6d17a492017-02-14 18:15:34 +010097 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
pier4fcb4b22019-10-11 18:19:59 +0200107 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 Ortiz6d17a492017-02-14 18:15:34 +0100124 public boolean putAll(K key, Collection<? extends V> values) {
125 return false;
126 }
127
128 @Override
pier4fcb4b22019-10-11 18:19:59 +0200129 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 Ortiz6d17a492017-02-14 18:15:34 +0100143 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 Halterman5e884352018-05-21 22:11:07 -0700178 public Iterator<Map.Entry<K, V>> iterator() {
179 return null;
180 }
181
182 @Override
Jordi Ortiz6d17a492017-02-14 18:15:34 +0100183 public Map<K, Collection<V>> asMap() {
184 return null;
185 }
186
187 @Override
Jonathan Hart46bf89b2017-02-27 15:56:42 -0800188 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 Ortiz6d17a492017-02-14 18:15:34 +0100196 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}