Madan Jampani | 08706ce | 2015-04-01 14:49:28 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 Open Networking Laboratory |
| 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.consistent.impl; |
| 17 | |
| 18 | import java.util.Collection; |
| 19 | import java.util.Iterator; |
| 20 | import java.util.Set; |
Madan Jampani | bff6d8f | 2015-03-31 16:53:47 -0700 | [diff] [blame] | 21 | |
Madan Jampani | 08706ce | 2015-04-01 14:49:28 -0700 | [diff] [blame] | 22 | import org.onosproject.store.service.ConsistentMap; |
| 23 | import org.onosproject.store.service.Serializer; |
| 24 | |
| 25 | import com.google.common.collect.Sets; |
| 26 | |
| 27 | /** |
| 28 | * Implementation of distributed set that is backed by a ConsistentMap. |
| 29 | |
| 30 | * @param <E> set element type |
| 31 | */ |
| 32 | public class DefaultDistributedSet<E> implements Set<E> { |
| 33 | |
| 34 | private final ConsistentMap<E, Boolean> backingMap; |
| 35 | |
Madan Jampani | 02b7fb8 | 2015-05-01 13:01:20 -0700 | [diff] [blame] | 36 | public DefaultDistributedSet(String name, Database database, Serializer serializer, boolean readOnly) { |
| 37 | backingMap = new DefaultConsistentMap<>(name, database, serializer, readOnly); |
Madan Jampani | 08706ce | 2015-04-01 14:49:28 -0700 | [diff] [blame] | 38 | } |
| 39 | |
| 40 | @Override |
| 41 | public int size() { |
| 42 | return backingMap.size(); |
| 43 | } |
| 44 | |
| 45 | @Override |
| 46 | public boolean isEmpty() { |
| 47 | return backingMap.isEmpty(); |
| 48 | } |
| 49 | |
Madan Jampani | bff6d8f | 2015-03-31 16:53:47 -0700 | [diff] [blame] | 50 | @SuppressWarnings("unchecked") |
Madan Jampani | 08706ce | 2015-04-01 14:49:28 -0700 | [diff] [blame] | 51 | @Override |
| 52 | public boolean contains(Object o) { |
| 53 | return backingMap.containsKey((E) o); |
| 54 | } |
| 55 | |
| 56 | @Override |
| 57 | public Iterator<E> iterator() { |
| 58 | return backingMap.keySet().iterator(); |
| 59 | } |
| 60 | |
| 61 | @Override |
| 62 | public Object[] toArray() { |
| 63 | return backingMap.keySet().stream().toArray(); |
| 64 | } |
| 65 | |
| 66 | @Override |
| 67 | public <T> T[] toArray(T[] a) { |
| 68 | return backingMap.keySet().stream().toArray(size -> a); |
| 69 | } |
| 70 | |
| 71 | @Override |
| 72 | public boolean add(E e) { |
| 73 | return backingMap.putIfAbsent(e, true) == null; |
| 74 | } |
| 75 | |
Madan Jampani | bff6d8f | 2015-03-31 16:53:47 -0700 | [diff] [blame] | 76 | @SuppressWarnings("unchecked") |
Madan Jampani | 08706ce | 2015-04-01 14:49:28 -0700 | [diff] [blame] | 77 | @Override |
| 78 | public boolean remove(Object o) { |
| 79 | return backingMap.remove((E) o, true); |
| 80 | } |
| 81 | |
| 82 | @Override |
| 83 | public boolean containsAll(Collection<?> c) { |
| 84 | return c.stream() |
| 85 | .allMatch(this::contains); |
| 86 | } |
| 87 | |
| 88 | @Override |
| 89 | public boolean addAll(Collection<? extends E> c) { |
| 90 | return c.stream() |
| 91 | .map(this::add) |
| 92 | .reduce(Boolean::logicalOr) |
| 93 | .orElse(false); |
| 94 | } |
| 95 | |
| 96 | @Override |
| 97 | public boolean retainAll(Collection<?> c) { |
| 98 | Set<?> retainSet = Sets.newHashSet(c); |
| 99 | return backingMap.keySet() |
| 100 | .stream() |
| 101 | .filter(k -> !retainSet.contains(k)) |
| 102 | .map(this::remove) |
| 103 | .reduce(Boolean::logicalOr) |
| 104 | .orElse(false); |
| 105 | } |
| 106 | |
| 107 | @Override |
| 108 | public boolean removeAll(Collection<?> c) { |
| 109 | Set<?> removeSet = Sets.newHashSet(c); |
| 110 | return backingMap.keySet() |
| 111 | .stream() |
| 112 | .filter(removeSet::contains) |
| 113 | .map(this::remove) |
| 114 | .reduce(Boolean::logicalOr) |
| 115 | .orElse(false); |
| 116 | } |
| 117 | |
| 118 | @Override |
| 119 | public void clear() { |
| 120 | backingMap.clear(); |
| 121 | } |
| 122 | } |