blob: 8b9a88bed48f2221067b6945f581f2373a4c4752 [file] [log] [blame]
Brian Stanke81fe2382016-03-03 15:54:32 -05001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2016-present Open Networking Laboratory
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;
24import java.util.HashSet;
25import java.util.LinkedList;
26import java.util.List;
27import java.util.Set;
28import java.util.concurrent.CompletableFuture;
29
30/**
31 * Test implementation of the distributed set.
32 */
33public 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
144 public Type primitiveType() {
145 return super.primitiveType();
146 }
147
148 @Override
149 public DistributedSet<E> asDistributedSet() {
150 return new DefaultDistributedSet<>(this, 0);
151 }
152
153 @Override
154 public DistributedSet<E> asDistributedSet(long timeoutMillis) {
155 return new DefaultDistributedSet<>(this, timeoutMillis);
156 }
157
158 /**
159 * Returns a new Builder instance.
160 *
161 * @return Builder
162 **/
163 public static Builder builder() {
164 return new Builder();
165 }
166
167 /**
168 * Builder constructor that instantiates a TestDistributedSet.
169 *
170 * @param <E>
171 */
172 public static class Builder<E> extends DistributedSetBuilder<E> {
173 @Override
174 public AsyncDistributedSet<E> build() {
175 return new TestDistributedSet(name());
176 }
177 }
178}