blob: 5add8b4cfb2b6336abed828f2860e9e6f60679a8 [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
29/**
30 * Atomix distributed set.
31 */
32public class AtomixDistributedSet<E> implements AsyncDistributedSet<E> {
33 private final io.atomix.core.set.AsyncDistributedSet<E> atomixSet;
34 private final Map<SetEventListener<E>, io.atomix.core.collection.CollectionEventListener<E>> listenerMap =
35 Maps.newIdentityHashMap();
36
37 public AtomixDistributedSet(io.atomix.core.set.AsyncDistributedSet<E> atomixSet) {
38 this.atomixSet = atomixSet;
39 }
40
41 @Override
42 public String name() {
43 return atomixSet.name();
44 }
45
46 @Override
47 public CompletableFuture<Integer> size() {
48 return atomixSet.size();
49 }
50
51 @Override
52 public CompletableFuture<Boolean> add(E element) {
53 return atomixSet.add(element);
54 }
55
56 @Override
57 public CompletableFuture<Boolean> remove(E element) {
58 return atomixSet.remove(element);
59 }
60
61 @Override
62 public CompletableFuture<Boolean> isEmpty() {
63 return atomixSet.isEmpty();
64 }
65
66 @Override
67 public CompletableFuture<Void> clear() {
68 return atomixSet.clear();
69 }
70
71 @Override
72 public CompletableFuture<Boolean> contains(E element) {
73 return atomixSet.contains(element);
74 }
75
76 @Override
77 public CompletableFuture<Boolean> addAll(Collection<? extends E> c) {
78 return atomixSet.addAll(c);
79 }
80
81 @Override
82 public CompletableFuture<Boolean> containsAll(Collection<? extends E> c) {
83 return atomixSet.containsAll(c);
84 }
85
86 @Override
87 public CompletableFuture<Boolean> retainAll(Collection<? extends E> c) {
88 return atomixSet.retainAll(c);
89 }
90
91 @Override
92 public CompletableFuture<Boolean> removeAll(Collection<? extends E> c) {
93 return atomixSet.removeAll(c);
94 }
95
96 @Override
97 public CompletableFuture<? extends Set<E>> getAsImmutableSet() {
98 return CompletableFuture.completedFuture(atomixSet.stream().collect(Collectors.toSet()));
99 }
100
101 @Override
102 public synchronized CompletableFuture<Void> addListener(SetEventListener<E> listener) {
103 io.atomix.core.collection.CollectionEventListener<E> atomixListener = event ->
104 listener.event(new SetEvent<E>(
105 name(),
106 SetEvent.Type.valueOf(event.type().name()),
107 event.element()));
108 listenerMap.put(listener, atomixListener);
109 return atomixSet.addListener(atomixListener);
110 }
111
112 @Override
113 public CompletableFuture<Void> removeListener(SetEventListener<E> listener) {
114 io.atomix.core.collection.CollectionEventListener<E> atomixListener = listenerMap.remove(listener);
115 if (atomixListener != null) {
116 return atomixSet.removeListener(atomixListener);
117 }
118 return CompletableFuture.completedFuture(null);
119 }
120}