blob: 011aa082a304463ef7a7f079221a10d314394948 [file] [log] [blame]
Avantika-Huawei9e848e82016-09-01 12:12:42 +05301/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2016-present Open Networking Foundation
Avantika-Huawei9e848e82016-09-01 12:12:42 +05303 *
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.pcelabelstore.util;
18
19import com.google.common.collect.ImmutableSet;
20import com.google.common.collect.Sets;
21import org.onosproject.store.primitives.DefaultDistributedSet;
22import org.onosproject.store.service.AsyncDistributedSet;
23import org.onosproject.store.service.DistributedSet;
24import org.onosproject.store.service.DistributedSetBuilder;
25import org.onosproject.store.service.SetEvent;
26import org.onosproject.store.service.SetEventListener;
27
28import java.util.Collection;
29import java.util.HashSet;
30import java.util.LinkedList;
31import java.util.List;
32import java.util.Set;
33import java.util.concurrent.CompletableFuture;
34
35/**
36 * Test implementation of the distributed set.
37 */
38public final class TestDistributedSet<E> extends DistributedSetAdapter<E> {
39 private final List<SetEventListener<E>> listeners;
40 private final Set<E> set;
41 private final String setName;
42
43 /**
44 * Public constructor.
45 *
46 * @param setName name to be assigned to this set
47 */
48 public TestDistributedSet(String setName) {
49 set = new HashSet<>();
50 listeners = new LinkedList<>();
51 this.setName = setName;
52 }
53
54 /**
55 * Notify all listeners of a set event.
56 *
57 * @param event the SetEvent
58 */
59 private void notifyListeners(SetEvent<E> event) {
60 listeners.forEach(
61 listener -> listener.event(event)
62 );
63 }
64
65 @Override
66 public CompletableFuture<Void> addListener(SetEventListener<E> listener) {
67 listeners.add(listener);
68 return CompletableFuture.completedFuture(null);
69 }
70
71 @Override
72 public CompletableFuture<Void> removeListener(SetEventListener<E> listener) {
73 listeners.remove(listener);
74 return CompletableFuture.completedFuture(null);
75 }
76
77 @Override
78 public CompletableFuture<Boolean> add(E element) {
79 SetEvent<E> event =
80 new SetEvent<>(setName, SetEvent.Type.ADD, element);
81 notifyListeners(event);
82 return CompletableFuture.completedFuture(set.add(element));
83 }
84
85 @Override
86 public CompletableFuture<Boolean> remove(E element) {
87 SetEvent<E> event =
88 new SetEvent<>(setName, SetEvent.Type.REMOVE, element);
89 notifyListeners(event);
90 return CompletableFuture.completedFuture(set.remove(element));
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) {
116 c.forEach(this::add);
117 return CompletableFuture.completedFuture(true);
118 }
119
120 @Override
121 public CompletableFuture<Boolean> containsAll(Collection<? extends E> c) {
122 return CompletableFuture.completedFuture(set.containsAll(c));
123 }
124
125 @Override
126 public CompletableFuture<Boolean> retainAll(Collection<? extends E> c) {
127 Set notInSet2;
128 notInSet2 = Sets.difference(set, (Set<?>) c);
129 return removeAll(ImmutableSet.copyOf(notInSet2));
130 }
131
132 @Override
133 public CompletableFuture<Boolean> removeAll(Collection<? extends E> c) {
134 c.forEach(this::remove);
135 return CompletableFuture.completedFuture(true);
136 }
137
138 @Override
139 public CompletableFuture<? extends Set<E>> getAsImmutableSet() {
140 return CompletableFuture.completedFuture(ImmutableSet.copyOf(set));
141 }
142
143 @Override
144 public String name() {
145 return this.setName;
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}