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