001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.commons.collections4.set;
018
019import java.io.Serializable;
020import java.util.Collection;
021import java.util.Iterator;
022import java.util.Map;
023import java.util.Set;
024
025/**
026 * Decorates a <code>Map</code> to obtain <code>Set</code> behaviour.
027 * <p>
028 * This class is used to create a <code>Set</code> with the same properties as
029 * the key set of any map. Thus, a ReferenceSet can be created by wrapping a
030 * <code>ReferenceMap</code> in an instance of this class.
031 * <p>
032 * Most map implementation can be used to create a set by passing in dummy values.
033 * Exceptions include <code>BidiMap</code> implementations, as they require unique values.
034 *
035 * @since 3.1
036 * @version $Id: MapBackedSet.html 972421 2015-11-14 20:00:04Z tn $
037 */
038public final class MapBackedSet<E, V> implements Set<E>, Serializable {
039
040    /** Serialization version */
041    private static final long serialVersionUID = 6723912213766056587L;
042
043    /** The map being used as the backing store */
044    private final Map<E, ? super V> map;
045
046    /** The dummyValue to use */
047    private final V dummyValue;
048
049    /**
050     * Factory method to create a set from a map.
051     *
052     * @param <E> the element type
053     * @param <V> the dummy value type in the map
054     * @param map  the map to decorate, must not be null
055     * @return a new map backed set
056     * @throws IllegalArgumentException if set is null
057     * @since 4.0
058     */
059    public static <E, V> MapBackedSet<E, V> mapBackedSet(final Map<E, ? super V> map) {
060        return mapBackedSet(map, null);
061    }
062
063    /**
064     * Factory method to create a set from a map.
065     *
066     * @param <E> the element type
067     * @param <V> the dummy value type in the map
068     * @param map  the map to decorate, must not be null
069     * @param dummyValue  the dummy value to use
070     * @return a new map backed set
071     * @throws IllegalArgumentException if map is null
072     * @since 4.0
073     */
074    public static <E, V> MapBackedSet<E, V> mapBackedSet(final Map<E, ? super V> map, final V dummyValue) {
075        if (map == null) {
076            throw new IllegalArgumentException("The map must not be null");
077        }
078        return new MapBackedSet<E, V>(map, dummyValue);
079    }
080
081    //-----------------------------------------------------------------------
082    /**
083     * Constructor that wraps (not copies).
084     *
085     * @param map  the map to decorate, must not be null
086     * @param dummyValue  the dummy value to use
087     * @throws IllegalArgumentException if map is null
088     */
089    private MapBackedSet(final Map<E, ? super V> map, final V dummyValue) {
090        super();
091        this.map = map;
092        this.dummyValue = dummyValue;
093    }
094
095    //-----------------------------------------------------------------------
096    public int size() {
097        return map.size();
098    }
099
100    public boolean isEmpty() {
101        return map.isEmpty();
102    }
103
104    public Iterator<E> iterator() {
105        return map.keySet().iterator();
106    }
107
108    public boolean contains(final Object obj) {
109        return map.containsKey(obj);
110    }
111
112    public boolean containsAll(final Collection<?> coll) {
113        return map.keySet().containsAll(coll);
114    }
115
116    public boolean add(final E obj) {
117        final int size = map.size();
118        map.put(obj, dummyValue);
119        return map.size() != size;
120    }
121
122    public boolean addAll(final Collection<? extends E> coll) {
123        final int size = map.size();
124        for (final E e : coll) {
125            map.put(e, dummyValue);
126        }
127        return map.size() != size;
128    }
129
130    public boolean remove(final Object obj) {
131        final int size = map.size();
132        map.remove(obj);
133        return map.size() != size;
134    }
135
136    public boolean removeAll(final Collection<?> coll) {
137        return map.keySet().removeAll(coll);
138    }
139
140    public boolean retainAll(final Collection<?> coll) {
141        return map.keySet().retainAll(coll);
142    }
143
144    public void clear() {
145        map.clear();
146    }
147
148    public Object[] toArray() {
149        return map.keySet().toArray();
150    }
151
152    public <T> T[] toArray(final T[] array) {
153        return map.keySet().toArray(array);
154    }
155
156    @Override
157    public boolean equals(final Object obj) {
158        return map.keySet().equals(obj);
159    }
160
161    @Override
162    public int hashCode() {
163        return map.keySet().hashCode();
164    }
165
166}