blob: 5483c749b3a1ac7110d5937642843ed1e1a9ac71 [file] [log] [blame]
Jordan Halterman00e92da2018-05-22 23:05:52 -07001/*
2 * Copyright 2018-present Open Networking Foundation
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 */
Thomas Vachuskab6d31672018-07-27 17:03:46 -070016package org.onosproject.store.atomix.primitives.impl;
Jordan Halterman00e92da2018-05-22 23:05:52 -070017
18import java.util.Collection;
19import java.util.Map;
20import java.util.Set;
21import java.util.concurrent.CompletableFuture;
22import java.util.stream.Collectors;
23
24import com.google.common.collect.Maps;
25import org.onosproject.store.service.AsyncDistributedSet;
26import org.onosproject.store.service.SetEvent;
27import org.onosproject.store.service.SetEventListener;
28
Jordan Halterman6cf60c32018-08-15 01:22:51 -070029import static org.onosproject.store.atomix.primitives.impl.AtomixFutures.adaptFuture;
30
Jordan Halterman00e92da2018-05-22 23:05:52 -070031/**
32 * Atomix distributed set.
33 */
34public class AtomixDistributedSet<E> implements AsyncDistributedSet<E> {
35 private final io.atomix.core.set.AsyncDistributedSet<E> atomixSet;
36 private final Map<SetEventListener<E>, io.atomix.core.collection.CollectionEventListener<E>> listenerMap =
37 Maps.newIdentityHashMap();
38
39 public AtomixDistributedSet(io.atomix.core.set.AsyncDistributedSet<E> atomixSet) {
40 this.atomixSet = atomixSet;
41 }
42
43 @Override
44 public String name() {
45 return atomixSet.name();
46 }
47
48 @Override
49 public CompletableFuture<Integer> size() {
Jordan Halterman6cf60c32018-08-15 01:22:51 -070050 return adaptFuture(atomixSet.size());
Jordan Halterman00e92da2018-05-22 23:05:52 -070051 }
52
53 @Override
54 public CompletableFuture<Boolean> add(E element) {
Jordan Halterman6cf60c32018-08-15 01:22:51 -070055 return adaptFuture(atomixSet.add(element));
Jordan Halterman00e92da2018-05-22 23:05:52 -070056 }
57
58 @Override
59 public CompletableFuture<Boolean> remove(E element) {
Jordan Halterman6cf60c32018-08-15 01:22:51 -070060 return adaptFuture(atomixSet.remove(element));
Jordan Halterman00e92da2018-05-22 23:05:52 -070061 }
62
63 @Override
64 public CompletableFuture<Boolean> isEmpty() {
Jordan Halterman6cf60c32018-08-15 01:22:51 -070065 return adaptFuture(atomixSet.isEmpty());
Jordan Halterman00e92da2018-05-22 23:05:52 -070066 }
67
68 @Override
69 public CompletableFuture<Void> clear() {
Jordan Halterman6cf60c32018-08-15 01:22:51 -070070 return adaptFuture(atomixSet.clear());
Jordan Halterman00e92da2018-05-22 23:05:52 -070071 }
72
73 @Override
74 public CompletableFuture<Boolean> contains(E element) {
Jordan Halterman6cf60c32018-08-15 01:22:51 -070075 return adaptFuture(atomixSet.contains(element));
Jordan Halterman00e92da2018-05-22 23:05:52 -070076 }
77
78 @Override
79 public CompletableFuture<Boolean> addAll(Collection<? extends E> c) {
Jordan Halterman6cf60c32018-08-15 01:22:51 -070080 return adaptFuture(atomixSet.addAll(c));
Jordan Halterman00e92da2018-05-22 23:05:52 -070081 }
82
83 @Override
84 public CompletableFuture<Boolean> containsAll(Collection<? extends E> c) {
Jordan Halterman6cf60c32018-08-15 01:22:51 -070085 return adaptFuture(atomixSet.containsAll(c));
Jordan Halterman00e92da2018-05-22 23:05:52 -070086 }
87
88 @Override
89 public CompletableFuture<Boolean> retainAll(Collection<? extends E> c) {
Jordan Halterman6cf60c32018-08-15 01:22:51 -070090 return adaptFuture(atomixSet.retainAll(c));
Jordan Halterman00e92da2018-05-22 23:05:52 -070091 }
92
93 @Override
94 public CompletableFuture<Boolean> removeAll(Collection<? extends E> c) {
Jordan Halterman6cf60c32018-08-15 01:22:51 -070095 return adaptFuture(atomixSet.removeAll(c));
Jordan Halterman00e92da2018-05-22 23:05:52 -070096 }
97
98 @Override
99 public CompletableFuture<? extends Set<E>> getAsImmutableSet() {
100 return CompletableFuture.completedFuture(atomixSet.stream().collect(Collectors.toSet()));
101 }
102
103 @Override
104 public synchronized CompletableFuture<Void> addListener(SetEventListener<E> listener) {
105 io.atomix.core.collection.CollectionEventListener<E> atomixListener = event ->
106 listener.event(new SetEvent<E>(
107 name(),
108 SetEvent.Type.valueOf(event.type().name()),
109 event.element()));
110 listenerMap.put(listener, atomixListener);
Jordan Halterman6cf60c32018-08-15 01:22:51 -0700111 return adaptFuture(atomixSet.addListener(atomixListener));
Jordan Halterman00e92da2018-05-22 23:05:52 -0700112 }
113
114 @Override
115 public CompletableFuture<Void> removeListener(SetEventListener<E> listener) {
116 io.atomix.core.collection.CollectionEventListener<E> atomixListener = listenerMap.remove(listener);
117 if (atomixListener != null) {
Jordan Halterman6cf60c32018-08-15 01:22:51 -0700118 return adaptFuture(atomixSet.removeListener(atomixListener));
Jordan Halterman00e92da2018-05-22 23:05:52 -0700119 }
120 return CompletableFuture.completedFuture(null);
121 }
122}