blob: 49f28f2f7bf9e523c5d282f858d8d5bb14914420 [file] [log] [blame]
Brian Stanke81fe2382016-03-03 15:54:32 -05001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2016-present Open Networking Foundation
Brian Stanke81fe2382016-03-03 15:54:32 -05003 *
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
17package org.onosproject.store.service;
18
19import com.google.common.collect.ImmutableSet;
20import com.google.common.collect.Sets;
21import org.onosproject.store.primitives.DefaultDistributedSet;
22
23import java.util.Collection;
Brian Stanke81fe2382016-03-03 15:54:32 -050024import java.util.LinkedList;
25import java.util.List;
26import java.util.Set;
27import java.util.concurrent.CompletableFuture;
28
29/**
30 * Test implementation of the distributed set.
31 */
32public 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 Milkey05370052017-11-15 17:00:41 -080043 set = Sets.newConcurrentHashSet();
Brian Stanke81fe2382016-03-03 15:54:32 -050044 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 HIGUCHI86ac40b2017-11-16 12:25:39 -080073 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 Stanke81fe2382016-03-03 15:54:32 -050080 }
81
82 @Override
83 public CompletableFuture<Boolean> remove(E element) {
Yuta HIGUCHI86ac40b2017-11-16 12:25:39 -080084 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 Stanke81fe2382016-03-03 15:54:32 -050091 }
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 HIGUCHI86ac40b2017-11-16 12:25:39 -0800116 return c.stream()
117 .map(this::add)
118 .reduce(CompletableFuture.completedFuture(false),
119 (l, r) -> l.thenCombine(r, Boolean::logicalOr));
Brian Stanke81fe2382016-03-03 15:54:32 -0500120 }
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 HIGUCHI86ac40b2017-11-16 12:25:39 -0800129 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 Stanke81fe2382016-03-03 15:54:32 -0500133 return removeAll(ImmutableSet.copyOf(notInSet2));
134 }
135
136 @Override
137 public CompletableFuture<Boolean> removeAll(Collection<? extends E> c) {
Yuta HIGUCHI86ac40b2017-11-16 12:25:39 -0800138 return c.stream()
139 .map(this::remove)
140 .reduce(CompletableFuture.completedFuture(false),
141 (l, r) -> l.thenCombine(r, Boolean::logicalOr));
Brian Stanke81fe2382016-03-03 15:54:32 -0500142 }
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 Stanke81fe2382016-03-03 15:54:32 -0500155 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 HIGUCHI86ac40b2017-11-16 12:25:39 -0800169 public static <E> Builder<E> builder() {
170 return new Builder<>();
Brian Stanke81fe2382016-03-03 15:54:32 -0500171 }
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 HIGUCHI86ac40b2017-11-16 12:25:39 -0800181 return new TestDistributedSet<>(name());
Brian Stanke81fe2382016-03-03 15:54:32 -0500182 }
183 }
184}