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 * https://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 serialization. 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 * 091 * @return A new set 092 */ 093 @Override 094 protected abstract Set<V> createCollection(); 095 096 /** 097 * Gets the set of values associated with the specified key. This would 098 * return an empty set in case the mapping is not present 099 * 100 * @param key The key to retrieve 101 * @return The {@code Set} of values, will return an empty 102 * {@code Set} for no mapping 103 */ 104 @Override 105 public Set<V> get(final K key) { 106 return wrappedCollection(key); 107 } 108 109 @Override 110 @SuppressWarnings("unchecked") 111 protected Map<K, Set<V>> getMap() { 112 return (Map<K, Set<V>>) super.getMap(); 113 } 114 115 /** 116 * Removes all values associated with the specified key. 117 * <p> 118 * A subsequent {@code get(Object)} would return an empty set. 119 * </p> 120 * 121 * @param key The key to remove values from 122 * @return The {@code Set} of values removed, will return an empty, 123 * unmodifiable set for no mapping found. 124 */ 125 @Override 126 public Set<V> remove(final Object key) { 127 return SetUtils.emptyIfNull(getMap().remove(key)); 128 } 129 130 @Override 131 Set<V> wrappedCollection(final K key) { 132 return new WrappedSet(key); 133 } 134}