Madan Jampani | a090a11 | 2016-01-18 16:38:17 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Laboratory |
Madan Jampani | a090a11 | 2016-01-18 16:38:17 -0800 | [diff] [blame] | 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 | */ |
| 16 | package org.onosproject.store.service; |
| 17 | |
| 18 | import java.util.Collection; |
| 19 | import java.util.Set; |
| 20 | import java.util.concurrent.CompletableFuture; |
| 21 | |
Madan Jampani | 538be74 | 2016-02-10 14:55:38 -0800 | [diff] [blame] | 22 | import org.onosproject.store.primitives.DefaultDistributedSet; |
| 23 | |
Madan Jampani | a090a11 | 2016-01-18 16:38:17 -0800 | [diff] [blame] | 24 | /** |
| 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 | */ |
| 33 | public interface AsyncDistributedSet<E> extends DistributedPrimitive { |
| 34 | |
| 35 | @Override |
Madan Jampani | 39fff10 | 2016-02-14 13:17:28 -0800 | [diff] [blame] | 36 | default DistributedPrimitive.Type primitiveType() { |
Madan Jampani | a090a11 | 2016-01-18 16:38:17 -0800 | [diff] [blame] | 37 | 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 Li | dfba739 | 2016-01-22 16:46:58 -0800 | [diff] [blame] | 45 | * @return CompletableFuture that is completed when the operation completes |
Madan Jampani | a090a11 | 2016-01-18 16:38:17 -0800 | [diff] [blame] | 46 | */ |
| 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 Li | dfba739 | 2016-01-22 16:46:58 -0800 | [diff] [blame] | 85 | * @return CompletableFuture that is completed when the operation completes |
Madan Jampani | a090a11 | 2016-01-18 16:38:17 -0800 | [diff] [blame] | 86 | */ |
| 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 Jampani | 538be74 | 2016-02-10 14:55:38 -0800 | [diff] [blame] | 127 | |
| 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() { |
| 134 | return asDistributedSet(DistributedPrimitive.DEFAULT_OPERTATION_TIMEOUT_MILLIS); |
| 135 | } |
| 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 Jampani | a090a11 | 2016-01-18 16:38:17 -0800 | [diff] [blame] | 147 | /** |
| 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 | } |