MapBackedSet.java

  1. /*
  2.  * Licensed to the Apache Software Foundation (ASF) under one or more
  3.  * contributor license agreements.  See the NOTICE file distributed with
  4.  * this work for additional information regarding copyright ownership.
  5.  * The ASF licenses this file to You under the Apache License, Version 2.0
  6.  * (the "License"); you may not use this file except in compliance with
  7.  * the License.  You may obtain a copy of the License at
  8.  *
  9.  *      http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */
  17. package org.apache.commons.collections4.set;

  18. import java.io.Serializable;
  19. import java.util.Collection;
  20. import java.util.Iterator;
  21. import java.util.Map;
  22. import java.util.Objects;
  23. import java.util.Set;
  24. import java.util.function.Predicate;

  25. /**
  26.  * Decorates a {@code Map} to obtain {@code Set} behavior.
  27.  * <p>
  28.  * This class is used to create a {@code Set} with the same properties as
  29.  * the key set of any map. Thus, a ReferenceSet can be created by wrapping a
  30.  * {@code ReferenceMap} in an instance of this class.
  31.  * </p>
  32.  * <p>
  33.  * Most map implementation can be used to create a set by passing in dummy values.
  34.  * Exceptions include {@code BidiMap} implementations, as they require unique values.
  35.  * </p>
  36.  *
  37.  * @param <E> the type of the elements in this set
  38.  * @param <V> the dummy value type in this map
  39.  * @since 3.1
  40.  */
  41. public final class MapBackedSet<E, V> implements Set<E>, Serializable {

  42.     /** Serialization version */
  43.     private static final long serialVersionUID = 6723912213766056587L;

  44.     /**
  45.      * Factory method to create a set from a map.
  46.      *
  47.      * @param <E> the element type
  48.      * @param <V> the dummy value type in the map
  49.      * @param map  the map to decorate, must not be null
  50.      * @return a new map backed set
  51.      * @throws NullPointerException if map is null
  52.      * @since 4.0
  53.      */
  54.     public static <E, V> MapBackedSet<E, V> mapBackedSet(final Map<E, ? super V> map) {
  55.         return mapBackedSet(map, null);
  56.     }

  57.     /**
  58.      * Factory method to create a set from a map.
  59.      *
  60.      * @param <E> the element type
  61.      * @param <V> the dummy value type in the map
  62.      * @param map  the map to decorate, must not be null
  63.      * @param dummyValue  the dummy value to use
  64.      * @return a new map backed set
  65.      * @throws NullPointerException if map is null
  66.      * @since 4.0
  67.      */
  68.     public static <E, V> MapBackedSet<E, V> mapBackedSet(final Map<E, ? super V> map, final V dummyValue) {
  69.         return new MapBackedSet<>(map, dummyValue);
  70.     }

  71.     /** The map being used as the backing store */
  72.     private final Map<E, ? super V> map;

  73.     /** The dummyValue to use */
  74.     private final V dummyValue;

  75.     /**
  76.      * Constructor that wraps (not copies).
  77.      *
  78.      * @param map  the map to decorate, must not be null
  79.      * @param dummyValue  the dummy value to use
  80.      * @throws NullPointerException if map is null
  81.      */
  82.     private MapBackedSet(final Map<E, ? super V> map, final V dummyValue) {
  83.         this.map = Objects.requireNonNull(map, "map");
  84.         this.dummyValue = dummyValue;
  85.     }

  86.     @Override
  87.     public boolean add(final E obj) {
  88.         final int size = map.size();
  89.         map.put(obj, dummyValue);
  90.         return map.size() != size;
  91.     }

  92.     @Override
  93.     public boolean addAll(final Collection<? extends E> coll) {
  94.         final int size = map.size();
  95.         for (final E e : coll) {
  96.             map.put(e, dummyValue);
  97.         }
  98.         return map.size() != size;
  99.     }

  100.     @Override
  101.     public void clear() {
  102.         map.clear();
  103.     }

  104.     @Override
  105.     public boolean contains(final Object obj) {
  106.         return map.containsKey(obj);
  107.     }

  108.     @Override
  109.     public boolean containsAll(final Collection<?> coll) {
  110.         return map.keySet().containsAll(coll);
  111.     }

  112.     @Override
  113.     public boolean equals(final Object obj) {
  114.         return map.keySet().equals(obj);
  115.     }

  116.     @Override
  117.     public int hashCode() {
  118.         return map.keySet().hashCode();
  119.     }

  120.     @Override
  121.     public boolean isEmpty() {
  122.         return map.isEmpty();
  123.     }

  124.     @Override
  125.     public Iterator<E> iterator() {
  126.         return map.keySet().iterator();
  127.     }

  128.     @Override
  129.     public boolean remove(final Object obj) {
  130.         final int size = map.size();
  131.         map.remove(obj);
  132.         return map.size() != size;
  133.     }

  134.     @Override
  135.     public boolean removeAll(final Collection<?> coll) {
  136.         return map.keySet().removeAll(coll);
  137.     }

  138.     /**
  139.      * @since 4.4
  140.      */
  141.     @Override
  142.     public boolean removeIf(final Predicate<? super E> filter) {
  143.         return map.keySet().removeIf(filter);
  144.     }

  145.     @Override
  146.     public boolean retainAll(final Collection<?> coll) {
  147.         return map.keySet().retainAll(coll);
  148.     }

  149.     @Override
  150.     public int size() {
  151.         return map.size();
  152.     }

  153.     @Override
  154.     public Object[] toArray() {
  155.         return map.keySet().toArray();
  156.     }

  157.     @Override
  158.     public <T> T[] toArray(final T[] array) {
  159.         return map.keySet().toArray(array);
  160.     }

  161. }