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