Jordan Halterman | 4922a06 | 2017-07-31 15:55:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2017-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 | */ |
| 16 | package org.onosproject.store.primitives.impl; |
| 17 | |
| 18 | import java.util.Collection; |
| 19 | import java.util.Map; |
| 20 | import java.util.Objects; |
| 21 | import java.util.Set; |
| 22 | import java.util.concurrent.CompletableFuture; |
| 23 | import java.util.stream.Collectors; |
| 24 | |
| 25 | import org.onosproject.store.service.AsyncConsistentMap; |
| 26 | import org.onosproject.store.service.Versioned; |
| 27 | |
| 28 | /** |
| 29 | * {@link org.onosproject.store.service.AsyncConsistentMap} that doesn't allow null values. |
| 30 | */ |
| 31 | public class NotNullAsyncConsistentMap<K, V> extends DelegatingAsyncConsistentMap<K, V> { |
| 32 | private final AsyncConsistentMap<K, V> delegateMap; |
| 33 | |
| 34 | public NotNullAsyncConsistentMap(AsyncConsistentMap<K, V> delegateMap) { |
| 35 | super(delegateMap); |
| 36 | this.delegateMap = delegateMap; |
| 37 | } |
| 38 | |
| 39 | @Override |
| 40 | public CompletableFuture<Boolean> containsValue(V value) { |
| 41 | if (value == null) { |
| 42 | return CompletableFuture.completedFuture(false); |
| 43 | } |
| 44 | return super.containsValue(value); |
| 45 | } |
| 46 | |
| 47 | @Override |
| 48 | public CompletableFuture<Versioned<V>> get(K key) { |
| 49 | return super.get(key).thenApply(v -> v != null && v.value() == null ? null : v); |
| 50 | } |
| 51 | |
| 52 | @Override |
| 53 | public CompletableFuture<Versioned<V>> getOrDefault(K key, V defaultValue) { |
| 54 | return super.getOrDefault(key, defaultValue).thenApply(v -> v != null && v.value() == null ? null : v); |
| 55 | } |
| 56 | |
| 57 | @Override |
| 58 | public CompletableFuture<Versioned<V>> put(K key, V value) { |
| 59 | if (value == null) { |
| 60 | return super.remove(key); |
| 61 | } |
| 62 | return super.put(key, value); |
| 63 | } |
| 64 | |
| 65 | @Override |
| 66 | public CompletableFuture<Versioned<V>> putAndGet(K key, V value) { |
| 67 | if (value == null) { |
| 68 | return super.remove(key).thenApply(v -> null); |
| 69 | } |
| 70 | return super.putAndGet(key, value); |
| 71 | } |
| 72 | |
| 73 | @Override |
| 74 | public CompletableFuture<Collection<Versioned<V>>> values() { |
| 75 | return super.values().thenApply(value -> value.stream() |
| 76 | .filter(v -> v.value() != null) |
| 77 | .collect(Collectors.toList())); |
| 78 | } |
| 79 | |
| 80 | @Override |
| 81 | public CompletableFuture<Set<Map.Entry<K, Versioned<V>>>> entrySet() { |
| 82 | return super.entrySet().thenApply(entries -> entries.stream() |
| 83 | .filter(e -> e.getValue().value() != null) |
| 84 | .collect(Collectors.toSet())); |
| 85 | } |
| 86 | |
| 87 | @Override |
| 88 | public CompletableFuture<Versioned<V>> putIfAbsent(K key, V value) { |
| 89 | if (value == null) { |
| 90 | return super.remove(key); |
| 91 | } |
| 92 | return super.putIfAbsent(key, value); |
| 93 | } |
| 94 | |
| 95 | @Override |
| 96 | public CompletableFuture<Boolean> remove(K key, V value) { |
| 97 | if (value == null) { |
| 98 | return CompletableFuture.completedFuture(false); |
| 99 | } |
| 100 | return super.remove(key, value); |
| 101 | } |
| 102 | |
| 103 | @Override |
| 104 | public CompletableFuture<Boolean> remove(K key, long version) { |
| 105 | return super.remove(key, version); |
| 106 | } |
| 107 | |
| 108 | @Override |
| 109 | public CompletableFuture<Versioned<V>> replace(K key, V value) { |
| 110 | if (value == null) { |
| 111 | return super.remove(key); |
| 112 | } |
| 113 | return super.replace(key, value); |
| 114 | } |
| 115 | |
| 116 | @Override |
| 117 | public CompletableFuture<Boolean> replace(K key, V oldValue, V newValue) { |
| 118 | if (oldValue == null) { |
| 119 | return super.putIfAbsent(key, newValue).thenApply(Objects::isNull); |
| 120 | } else if (newValue == null) { |
| 121 | return super.remove(key, oldValue); |
| 122 | } |
| 123 | return super.replace(key, oldValue, newValue); |
| 124 | } |
| 125 | |
| 126 | @Override |
| 127 | public CompletableFuture<Boolean> replace(K key, long oldVersion, V newValue) { |
| 128 | return super.replace(key, oldVersion, newValue); |
| 129 | } |
| 130 | } |