blob: aa1349c5b6219f975efa78ad9e0cb7ddfb09b0c0 [file] [log] [blame]
Madan Jampania090a112016-01-18 16:38:17 -08001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2016-present Open Networking Laboratory
Madan Jampania090a112016-01-18 16:38:17 -08003 *
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 */
16package org.onosproject.store.service;
17
18import java.util.Collection;
19import java.util.Set;
20import java.util.concurrent.CompletableFuture;
21
Madan Jampani538be742016-02-10 14:55:38 -080022import org.onosproject.store.primitives.DefaultDistributedSet;
23
Madan Jampania090a112016-01-18 16:38:17 -080024/**
25 * A distributed collection designed for holding unique elements.
26 * <p>
27 * All methods of {@code AsyncDistributedSet} immediately return a {@link CompletableFuture future}.
28 * The returned future will be {@link CompletableFuture#complete completed} when the operation
29 * completes.
30 *
31 * @param <E> set entry type
32 */
33public interface AsyncDistributedSet<E> extends DistributedPrimitive {
34
35 @Override
Madan Jampani39fff102016-02-14 13:17:28 -080036 default DistributedPrimitive.Type primitiveType() {
Madan Jampania090a112016-01-18 16:38:17 -080037 return DistributedPrimitive.Type.SET;
38 }
39
40 /**
41 * Registers the specified listener to be notified whenever
42 * the set is updated.
43 *
44 * @param listener listener to notify about set update events
Jian Lidfba7392016-01-22 16:46:58 -080045 * @return CompletableFuture that is completed when the operation completes
Madan Jampania090a112016-01-18 16:38:17 -080046 */
47 CompletableFuture<Void> addListener(SetEventListener<E> listener);
48
49 /**
50 * Unregisters the specified listener.
51 *
52 * @param listener listener to unregister.
53 * @return CompletableFuture that is completed when the operation completes
54 */
55 CompletableFuture<Void> removeListener(SetEventListener<E> listener);
56
57 /**
58 * Adds the specified element to this set if it is not already present (optional operation).
59 * @param element element to add
60 * @return {@code true} if this set did not already contain the specified element.
61 */
62 CompletableFuture<Boolean> add(E element);
63
64 /**
65 * Removes the specified element to this set if it is present (optional operation).
66 * @param element element to remove
67 * @return {@code true} if this set contained the specified element
68 */
69 CompletableFuture<Boolean> remove(E element);
70
71 /**
72 * Returns the number of elements in the set.
73 * @return size of the set
74 */
75 CompletableFuture<Integer> size();
76
77 /**
78 * Returns if the set is empty.
79 * @return {@code true} if this set is empty
80 */
81 CompletableFuture<Boolean> isEmpty();
82
83 /**
84 * Removes all elements from the set.
Jian Lidfba7392016-01-22 16:46:58 -080085 * @return CompletableFuture that is completed when the operation completes
Madan Jampania090a112016-01-18 16:38:17 -080086 */
87 CompletableFuture<Void> clear();
88
89 /**
90 * Returns if this set contains the specified element.
91 * @param element element to check
92 * @return {@code true} if this set contains the specified element
93 */
94 CompletableFuture<Boolean> contains(E element);
95
96 /**
97 * Adds all of the elements in the specified collection to this set if they're not
98 * already present (optional operation).
99 * @param c collection containing elements to be added to this set
100 * @return {@code true} if this set contains all elements in the collection
101 */
102 CompletableFuture<Boolean> addAll(Collection<? extends E> c);
103
104 /**
105 * Returns if this set contains all the elements in specified collection.
106 * @param c collection
107 * @return {@code true} if this set contains all elements in the collection
108 */
109 CompletableFuture<Boolean> containsAll(Collection<? extends E> c);
110
111 /**
112 * Retains only the elements in this set that are contained in the specified collection (optional operation).
113 * @param c collection containing elements to be retained in this set
114 * @return {@code true} if this set changed as a result of the call
115 */
116 CompletableFuture<Boolean> retainAll(Collection<? extends E> c);
117
118 /**
119 * Removes from this set all of its elements that are contained in the specified collection (optional operation).
120 * If the specified collection is also a set, this operation effectively modifies this set so that its
121 * value is the asymmetric set difference of the two sets.
122 * @param c collection containing elements to be removed from this set
123 * @return {@code true} if this set changed as a result of the call
124 */
125 CompletableFuture<Boolean> removeAll(Collection<? extends E> c);
126
Madan Jampani538be742016-02-10 14:55:38 -0800127
128 /**
129 * Returns a new {@link DistributedSet} that is backed by this instance.
130 *
131 * @return new {@code DistributedSet} instance
132 */
133 default DistributedSet<E> asDistributedSet() {
Jordan Halterman6440b092017-05-24 17:48:08 -0700134 return asDistributedSet(DistributedPrimitive.DEFAULT_OPERATION_TIMEOUT_MILLIS);
Madan Jampani538be742016-02-10 14:55:38 -0800135 }
136
137 /**
138 * Returns a new {@link DistributedSet} that is backed by this instance.
139 *
140 * @param timeoutMillis timeout duration for the returned DistributedSet operations
141 * @return new {@code DistributedSet} instance
142 */
143 default DistributedSet<E> asDistributedSet(long timeoutMillis) {
144 return new DefaultDistributedSet<>(this, timeoutMillis);
145 }
146
Madan Jampania090a112016-01-18 16:38:17 -0800147 /**
148 * Returns the entries as a immutable set. The returned set is a snapshot and will not reflect new changes made to
149 * this AsyncDistributedSet
150 * @return immutable set copy
151 */
152 CompletableFuture<? extends Set<E>> getAsImmutableSet();
153}