HIGUCHI Yuta | 14e865d | 2015-11-25 20:42:32 -0800 | [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 | */ |
Madan Jampani | f4c8850 | 2016-01-21 12:35:36 -0800 | [diff] [blame] | 16 | package org.onosproject.store.primitives.impl; |
HIGUCHI Yuta | 14e865d | 2015-11-25 20:42:32 -0800 | [diff] [blame] | 17 | |
| 18 | import java.util.Arrays; |
| 19 | import java.util.Collection; |
| 20 | import java.util.Iterator; |
| 21 | import java.util.Set; |
| 22 | import java.util.function.Function; |
| 23 | import java.util.stream.Collectors; |
| 24 | |
| 25 | import com.google.common.collect.Iterators; |
| 26 | |
| 27 | /** |
| 28 | * Set view backed by Set with element type {@code <BACK>} but returns |
| 29 | * element as {@code <OUT>} for convenience. |
| 30 | * |
| 31 | * @param <BACK> Backing {@link Set} element type. |
| 32 | * MappingSet will follow this type's equality behavior. |
| 33 | * @param <OUT> external facing element type. |
| 34 | * MappingSet will ignores equality defined by this type. |
| 35 | */ |
| 36 | class MappingSet<BACK, OUT> implements Set<OUT> { |
| 37 | |
| 38 | private final Set<BACK> backedSet; |
| 39 | private final Function<OUT, BACK> toBack; |
| 40 | private final Function<BACK, OUT> toOut; |
| 41 | |
| 42 | public MappingSet(Set<BACK> backedSet, |
| 43 | Function<Set<BACK>, Set<BACK>> supplier, |
| 44 | Function<OUT, BACK> toBack, Function<BACK, OUT> toOut) { |
| 45 | this.backedSet = supplier.apply(backedSet); |
| 46 | this.toBack = toBack; |
| 47 | this.toOut = toOut; |
| 48 | } |
| 49 | |
| 50 | @Override |
| 51 | public int size() { |
| 52 | return backedSet.size(); |
| 53 | } |
| 54 | |
| 55 | @Override |
| 56 | public boolean isEmpty() { |
| 57 | return backedSet.isEmpty(); |
| 58 | } |
| 59 | |
| 60 | @Override |
| 61 | public boolean contains(Object o) { |
| 62 | return backedSet.contains(toBack.apply((OUT) o)); |
| 63 | } |
| 64 | |
| 65 | @Override |
| 66 | public Iterator<OUT> iterator() { |
| 67 | return Iterators.transform(backedSet.iterator(), toOut::apply); |
| 68 | } |
| 69 | |
| 70 | @Override |
| 71 | public Object[] toArray() { |
| 72 | return backedSet.stream() |
| 73 | .map(toOut) |
| 74 | .toArray(); |
| 75 | } |
| 76 | |
| 77 | @Override |
| 78 | public <T> T[] toArray(T[] a) { |
| 79 | return backedSet.stream() |
| 80 | .map(toOut) |
| 81 | .toArray(size -> { |
| 82 | if (size < a.length) { |
| 83 | return (T[]) new Object[size]; |
| 84 | } else { |
| 85 | Arrays.fill(a, null); |
| 86 | return a; |
| 87 | } |
| 88 | }); |
| 89 | } |
| 90 | |
| 91 | @Override |
| 92 | public boolean add(OUT e) { |
| 93 | return backedSet.add(toBack.apply(e)); |
| 94 | } |
| 95 | |
| 96 | @Override |
| 97 | public boolean remove(Object o) { |
| 98 | return backedSet.remove(toBack.apply((OUT) o)); |
| 99 | } |
| 100 | |
| 101 | @Override |
| 102 | public boolean containsAll(Collection<?> c) { |
| 103 | return c.stream() |
| 104 | .map(e -> toBack.apply((OUT) e)) |
| 105 | .allMatch(backedSet::contains); |
| 106 | } |
| 107 | |
| 108 | @Override |
| 109 | public boolean addAll(Collection<? extends OUT> c) { |
| 110 | return backedSet.addAll(c.stream().map(toBack).collect(Collectors.toList())); |
| 111 | } |
| 112 | |
| 113 | @Override |
| 114 | public boolean retainAll(Collection<?> c) { |
| 115 | return backedSet.retainAll(c.stream() |
| 116 | .map(x -> toBack.apply((OUT) x)) |
| 117 | .collect(Collectors.toList())); |
| 118 | } |
| 119 | |
| 120 | @Override |
| 121 | public boolean removeAll(Collection<?> c) { |
| 122 | return backedSet.removeAll(c.stream() |
| 123 | .map(x -> toBack.apply((OUT) x)) |
| 124 | .collect(Collectors.toList())); |
| 125 | } |
| 126 | |
| 127 | @Override |
| 128 | public void clear() { |
| 129 | backedSet.clear(); |
| 130 | } |
| 131 | } |