blob: adc9dcd0414778b91559fda62bd99d583a0ea2f7 [file] [log] [blame]
Madan Jampani08706ce2015-04-01 14:49:28 -07001/*
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 */
16package org.onosproject.store.consistent.impl;
17
18import java.util.Collection;
19import java.util.Iterator;
20import java.util.Set;
Madan Jampanibff6d8f2015-03-31 16:53:47 -070021
Madan Jampani08706ce2015-04-01 14:49:28 -070022import org.onosproject.store.service.ConsistentMap;
23import org.onosproject.store.service.Serializer;
24
25import 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 */
32public class DefaultDistributedSet<E> implements Set<E> {
33
34 private final ConsistentMap<E, Boolean> backingMap;
35
Madan Jampani02b7fb82015-05-01 13:01:20 -070036 public DefaultDistributedSet(String name, Database database, Serializer serializer, boolean readOnly) {
37 backingMap = new DefaultConsistentMap<>(name, database, serializer, readOnly);
Madan Jampani08706ce2015-04-01 14:49:28 -070038 }
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 Jampanibff6d8f2015-03-31 16:53:47 -070050 @SuppressWarnings("unchecked")
Madan Jampani08706ce2015-04-01 14:49:28 -070051 @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 Jampanibff6d8f2015-03-31 16:53:47 -070076 @SuppressWarnings("unchecked")
Madan Jampani08706ce2015-04-01 14:49:28 -070077 @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}