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