Brian Stanke | 81fe238 | 2016-03-03 15:54:32 -0500 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Foundation |
Brian Stanke | 81fe238 | 2016-03-03 15:54:32 -0500 | [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 | |
| 17 | package org.onosproject.store.service; |
| 18 | |
| 19 | import com.google.common.collect.ImmutableSet; |
| 20 | import com.google.common.collect.Sets; |
| 21 | import org.onosproject.store.primitives.DefaultDistributedSet; |
| 22 | |
| 23 | import java.util.Collection; |
Brian Stanke | 81fe238 | 2016-03-03 15:54:32 -0500 | [diff] [blame] | 24 | import java.util.LinkedList; |
| 25 | import java.util.List; |
| 26 | import java.util.Set; |
| 27 | import java.util.concurrent.CompletableFuture; |
| 28 | |
| 29 | /** |
| 30 | * Test implementation of the distributed set. |
| 31 | */ |
| 32 | public final class TestDistributedSet<E> extends DistributedSetAdapter<E> { |
| 33 | private final List<SetEventListener<E>> listeners; |
| 34 | private final Set<E> set; |
| 35 | private final String setName; |
| 36 | |
| 37 | /** |
| 38 | * Public constructor. |
| 39 | * |
| 40 | * @param setName name to be assigned to this set |
| 41 | */ |
| 42 | public TestDistributedSet(String setName) { |
Ray Milkey | 0537005 | 2017-11-15 17:00:41 -0800 | [diff] [blame] | 43 | set = Sets.newConcurrentHashSet(); |
Brian Stanke | 81fe238 | 2016-03-03 15:54:32 -0500 | [diff] [blame] | 44 | listeners = new LinkedList<>(); |
| 45 | this.setName = setName; |
| 46 | } |
| 47 | |
| 48 | /** |
| 49 | * Notify all listeners of a set event. |
| 50 | * |
| 51 | * @param event the SetEvent |
| 52 | */ |
| 53 | private void notifyListeners(SetEvent<E> event) { |
| 54 | listeners.forEach( |
| 55 | listener -> listener.event(event) |
| 56 | ); |
| 57 | } |
| 58 | |
| 59 | @Override |
| 60 | public CompletableFuture<Void> addListener(SetEventListener<E> listener) { |
| 61 | listeners.add(listener); |
| 62 | return CompletableFuture.completedFuture(null); |
| 63 | } |
| 64 | |
| 65 | @Override |
| 66 | public CompletableFuture<Void> removeListener(SetEventListener<E> listener) { |
| 67 | listeners.remove(listener); |
| 68 | return CompletableFuture.completedFuture(null); |
| 69 | } |
| 70 | |
| 71 | @Override |
| 72 | public CompletableFuture<Boolean> add(E element) { |
Yuta HIGUCHI | 86ac40b | 2017-11-16 12:25:39 -0800 | [diff] [blame] | 73 | boolean updated = set.add(element); |
| 74 | if (updated) { |
| 75 | SetEvent<E> event = |
| 76 | new SetEvent<>(setName, SetEvent.Type.ADD, element); |
| 77 | notifyListeners(event); |
| 78 | } |
| 79 | return CompletableFuture.completedFuture(updated); |
Brian Stanke | 81fe238 | 2016-03-03 15:54:32 -0500 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | @Override |
| 83 | public CompletableFuture<Boolean> remove(E element) { |
Yuta HIGUCHI | 86ac40b | 2017-11-16 12:25:39 -0800 | [diff] [blame] | 84 | boolean updated = set.remove(element); |
| 85 | if (updated) { |
| 86 | SetEvent<E> event = |
| 87 | new SetEvent<>(setName, SetEvent.Type.REMOVE, element); |
| 88 | notifyListeners(event); |
| 89 | } |
| 90 | return CompletableFuture.completedFuture(updated); |
Brian Stanke | 81fe238 | 2016-03-03 15:54:32 -0500 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | @Override |
| 94 | public CompletableFuture<Integer> size() { |
| 95 | return CompletableFuture.completedFuture(set.size()); |
| 96 | } |
| 97 | |
| 98 | @Override |
| 99 | public CompletableFuture<Boolean> isEmpty() { |
| 100 | return CompletableFuture.completedFuture(set.isEmpty()); |
| 101 | } |
| 102 | |
| 103 | @Override |
| 104 | public CompletableFuture<Void> clear() { |
| 105 | removeAll(ImmutableSet.copyOf(set)); |
| 106 | return CompletableFuture.completedFuture(null); |
| 107 | } |
| 108 | |
| 109 | @Override |
| 110 | public CompletableFuture<Boolean> contains(E element) { |
| 111 | return CompletableFuture.completedFuture(set.contains(element)); |
| 112 | } |
| 113 | |
| 114 | @Override |
| 115 | public CompletableFuture<Boolean> addAll(Collection<? extends E> c) { |
Yuta HIGUCHI | 86ac40b | 2017-11-16 12:25:39 -0800 | [diff] [blame] | 116 | return c.stream() |
| 117 | .map(this::add) |
| 118 | .reduce(CompletableFuture.completedFuture(false), |
| 119 | (l, r) -> l.thenCombine(r, Boolean::logicalOr)); |
Brian Stanke | 81fe238 | 2016-03-03 15:54:32 -0500 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | @Override |
| 123 | public CompletableFuture<Boolean> containsAll(Collection<? extends E> c) { |
| 124 | return CompletableFuture.completedFuture(set.containsAll(c)); |
| 125 | } |
| 126 | |
| 127 | @Override |
| 128 | public CompletableFuture<Boolean> retainAll(Collection<? extends E> c) { |
Yuta HIGUCHI | 86ac40b | 2017-11-16 12:25:39 -0800 | [diff] [blame] | 129 | Set<? extends E> s = (c instanceof Set) ? |
| 130 | (Set<? extends E>) c : |
| 131 | ImmutableSet.copyOf(c); |
| 132 | Set<E> notInSet2 = Sets.difference(set, s); |
Brian Stanke | 81fe238 | 2016-03-03 15:54:32 -0500 | [diff] [blame] | 133 | return removeAll(ImmutableSet.copyOf(notInSet2)); |
| 134 | } |
| 135 | |
| 136 | @Override |
| 137 | public CompletableFuture<Boolean> removeAll(Collection<? extends E> c) { |
Yuta HIGUCHI | 86ac40b | 2017-11-16 12:25:39 -0800 | [diff] [blame] | 138 | return c.stream() |
| 139 | .map(this::remove) |
| 140 | .reduce(CompletableFuture.completedFuture(false), |
| 141 | (l, r) -> l.thenCombine(r, Boolean::logicalOr)); |
Brian Stanke | 81fe238 | 2016-03-03 15:54:32 -0500 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | @Override |
| 145 | public CompletableFuture<? extends Set<E>> getAsImmutableSet() { |
| 146 | return CompletableFuture.completedFuture(ImmutableSet.copyOf(set)); |
| 147 | } |
| 148 | |
| 149 | @Override |
| 150 | public String name() { |
| 151 | return this.setName; |
| 152 | } |
| 153 | |
| 154 | @Override |
Brian Stanke | 81fe238 | 2016-03-03 15:54:32 -0500 | [diff] [blame] | 155 | public DistributedSet<E> asDistributedSet() { |
| 156 | return new DefaultDistributedSet<>(this, 0); |
| 157 | } |
| 158 | |
| 159 | @Override |
| 160 | public DistributedSet<E> asDistributedSet(long timeoutMillis) { |
| 161 | return new DefaultDistributedSet<>(this, timeoutMillis); |
| 162 | } |
| 163 | |
| 164 | /** |
| 165 | * Returns a new Builder instance. |
| 166 | * |
| 167 | * @return Builder |
| 168 | **/ |
Yuta HIGUCHI | 86ac40b | 2017-11-16 12:25:39 -0800 | [diff] [blame] | 169 | public static <E> Builder<E> builder() { |
| 170 | return new Builder<>(); |
Brian Stanke | 81fe238 | 2016-03-03 15:54:32 -0500 | [diff] [blame] | 171 | } |
| 172 | |
| 173 | /** |
| 174 | * Builder constructor that instantiates a TestDistributedSet. |
| 175 | * |
| 176 | * @param <E> |
| 177 | */ |
| 178 | public static class Builder<E> extends DistributedSetBuilder<E> { |
| 179 | @Override |
| 180 | public AsyncDistributedSet<E> build() { |
Yuta HIGUCHI | 86ac40b | 2017-11-16 12:25:39 -0800 | [diff] [blame] | 181 | return new TestDistributedSet<>(name()); |
Brian Stanke | 81fe238 | 2016-03-03 15:54:32 -0500 | [diff] [blame] | 182 | } |
| 183 | } |
| 184 | } |