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.multimap;
018
019import java.util.Collections;
020import java.util.Map;
021import java.util.Set;
022
023import org.apache.commons.collections4.SetUtils;
024import org.apache.commons.collections4.SetValuedMap;
025
026/**
027 * Abstract implementation of the {@link SetValuedMap} interface to simplify the
028 * creation of subclass implementations.
029 * <p>
030 * Subclasses specify a Map implementation to use as the internal storage and
031 * the Set implementation to use as values.
032 * </p>
033 *
034 * @param <K> the type of the keys in this map
035 * @param <V> the type of the values in this map
036 * @since 4.1
037 */
038public abstract class AbstractSetValuedMap<K, V> extends AbstractMultiValuedMap<K, V>
039    implements SetValuedMap<K, V> {
040
041    /**
042     * Wrapped set to handle add and remove on the collection returned by
043     * {@code get(Object)}.
044     */
045    private final class WrappedSet extends WrappedCollection implements Set<V> {
046
047        WrappedSet(final K key) {
048            super(key);
049        }
050
051        @Override
052        public boolean equals(final Object other) {
053            final Set<V> set = (Set<V>) getMapping();
054            if (set == null) {
055                return Collections.emptySet().equals(other);
056            }
057            if (!(other instanceof Set)) {
058                return false;
059            }
060            final Set<?> otherSet = (Set<?>) other;
061            return SetUtils.isEqualSet(set, otherSet);
062        }
063
064        @Override
065        public int hashCode() {
066            final Set<V> set = (Set<V>) getMapping();
067            return SetUtils.hashCodeForSet(set);
068        }
069
070    }
071
072    /**
073     * Constructor needed for subclass serialisation.
074     */
075    protected AbstractSetValuedMap() {
076    }
077
078    /**
079     * A constructor that wraps, not copies
080     *
081     * @param map  the map to wrap, must not be null
082     * @throws NullPointerException if the map is null
083     */
084    protected AbstractSetValuedMap(final Map<K, ? extends Set<V>> map) {
085        super(map);
086    }
087
088    /**
089     * Creates a new value collection using the provided factory.
090     * @return a new set
091     */
092    @Override
093    protected abstract Set<V> createCollection();
094
095    /**
096     * Gets the set of values associated with the specified key. This would
097     * return an empty set in case the mapping is not present
098     *
099     * @param key  the key to retrieve
100     * @return the {@code Set} of values, will return an empty
101     *   {@code Set} for no mapping
102     */
103    @Override
104    public Set<V> get(final K key) {
105        return wrappedCollection(key);
106    }
107
108    @Override
109    @SuppressWarnings("unchecked")
110    protected Map<K, Set<V>> getMap() {
111        return (Map<K, Set<V>>) super.getMap();
112    }
113
114    /**
115     * Removes all values associated with the specified key.
116     * <p>
117     * A subsequent {@code get(Object)} would return an empty set.
118     *
119     * @param key the key to remove values from
120     * @return the {@code Set} of values removed, will return an empty,
121     *   unmodifiable set for no mapping found.
122     */
123    @Override
124    public Set<V> remove(final Object key) {
125        return SetUtils.emptyIfNull(getMap().remove(key));
126    }
127
128    @Override
129    Set<V> wrappedCollection(final K key) {
130        return new WrappedSet(key);
131    }
132}