blob: c451da9e8da0b0bb0d6d519a99c39c4d7ce10ede [file] [log] [blame]
pierfa48c6e2019-10-11 18:19:59 +02001/*
2 * Copyright 2019-present Open Networking Foundation
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 */
16package org.onosproject.store.primitives.impl;
17
18import com.google.common.collect.HashMultimap;
19import com.google.common.collect.Multimap;
20import org.onosproject.store.service.AsyncConsistentMultimapAdapter;
21import org.onosproject.store.service.Versioned;
22
23import java.util.Collection;
24import java.util.Map;
25import java.util.concurrent.CompletableFuture;
26
27public class AsyncConsistentMultimapMock<K, V> extends AsyncConsistentMultimapAdapter<K, V> {
28 private final Multimap<K, V> baseMap = HashMultimap.create();
29 private static final int DEFAULT_CREATION_TIME = 0;
30 private static final int DEFAULT_VERSION = 0;
31
32 AsyncConsistentMultimapMock() {
33 }
34
35 Versioned<Collection<? extends V>> makeVersioned(Collection<? extends V> v) {
36 return new Versioned<>(v, DEFAULT_VERSION, DEFAULT_CREATION_TIME);
37 }
38
39 @Override
40 public CompletableFuture<Integer> size() {
41 return CompletableFuture.completedFuture(baseMap.size());
42 }
43
44 @Override
45 public CompletableFuture<Boolean> isEmpty() {
46 return CompletableFuture.completedFuture(baseMap.isEmpty());
47 }
48
49 @Override
50 public CompletableFuture<Boolean> putAll(Map<K, Collection<? extends V>> mapping) {
51 CompletableFuture<Boolean> result = CompletableFuture.completedFuture(false);
52 for (Map.Entry<K, Collection<? extends V>> entry : mapping.entrySet()) {
53 if (baseMap.putAll(entry.getKey(), entry.getValue())) {
54 result = CompletableFuture.completedFuture(true);
55 }
56 }
57 return result;
58 }
59
60 @Override
61 public CompletableFuture<Versioned<Collection<? extends V>>> get(K key) {
62 return CompletableFuture.completedFuture(makeVersioned(baseMap.get(key)));
63 }
64
65 @Override
66 public CompletableFuture<Boolean> removeAll(Map<K, Collection<? extends V>> mapping) {
67 CompletableFuture<Boolean> result = CompletableFuture.completedFuture(false);
68 for (Map.Entry<K, Collection<? extends V>> entry : mapping.entrySet()) {
69 for (V value : entry.getValue()) {
70 if (baseMap.remove(entry.getKey(), value)) {
71 result = CompletableFuture.completedFuture(true);
72 }
73 }
74 }
75 return result;
76 }
77}
78
79