001package com.identityworksllc.iiq.common; 002 003import java.util.Collection; 004import java.util.Map; 005import java.util.Set; 006 007/** 008 * A Map interface that delegates all calls by default to the contained Map. This 009 * is useful for situations where the implementation of the Map is the new behavior, 010 * not the implementation of the individual methods. 011 * 012 * The delegate Map is whatever is returned from {@link #getDelegate()}. 013 * 014 * @param <K> The key type 015 * @param <V> The value type 016 */ 017public interface DelegatedMap<K, V> extends Map<K, V> { 018 /** 019 * Removes all mappings from the map 020 * @see Map#clear() 021 */ 022 default void clear() { 023 getDelegate().clear(); 024 } 025 026 /** 027 * Returns true if this map contains a mapping for the specified key. 028 * 029 * @param key key whose presence in this map is to be tested 030 * @return true if this map contains a mapping for the specified key 031 * @see Map#containsKey(Object) 032 */ 033 default boolean containsKey(Object key) { 034 return getDelegate().containsKey(key); 035 } 036 037 /** 038 * Returns true if this map maps one or more keys to the specified value. 039 * @param value value whose presence in this map is to be tested 040 * @return true if this map maps one or more keys to the specified value 041 * @see Map#containsValue(Object) 042 */ 043 default boolean containsValue(Object value) { 044 return getDelegate().containsValue(value); 045 } 046 047 /** 048 * Returns a {@link Set} view of the mappings contained in this map. 049 * @return a set view of the mappings contained in this map 050 * @see Map#entrySet() 051 */ 052 default Set<Entry<K, V>> entrySet() { 053 return getDelegate().entrySet(); 054 } 055 056 /** 057 * Gets the value to which the specified key is mapped. 058 * The underlying Map will determine the behavior if there is no key. 059 * @param key the key whose associated value is to be returned 060 * @return the value to which the specified key is mapped, or null if this map contains no mapping for the key 061 * @see Map#get(Object) 062 */ 063 default V get(Object key) { 064 return getDelegate().get(key); 065 } 066 067 /** 068 * Gets the map to which all other {@link Map} calls are delegated 069 * @return The Map to which all calls are delegated 070 */ 071 Map<K, V> getDelegate(); 072 073 /** 074 * Returns true if this map contains no key-value mappings. 075 * @return true if this map contains no key-value mappings 076 */ 077 default boolean isEmpty() { 078 return getDelegate().isEmpty(); 079 } 080 081 /** 082 * Returns a {@link Set} view of the keys contained in this map. The behavior 083 * of the {@link Set} is determined by the underlying Map. 084 * @return a set view of the keys contained in this map 085 * @see Map#keySet() 086 */ 087 default Set<K> keySet() { 088 return getDelegate().keySet(); 089 } 090 091 /** 092 * 093 * @param key key with which the specified value is to be associated 094 * @param value value to be associated with the specified key 095 * @return the previous value associated with key, or null if there was no mapping for key. 096 */ 097 default V put(K key, V value) { 098 return getDelegate().put(key, value); 099 } 100 101 /** 102 * Copies all of the mappings from the specified map to this map. 103 * @param m mappings to be stored in this map 104 * @see Map#putAll(Map) 105 */ 106 default void putAll(Map<? extends K, ? extends V> m) { 107 getDelegate().putAll(m); 108 } 109 110 /** 111 * Removes the mapping for a key from this map if it is present. 112 * @param key key whose mapping is to be removed from the map 113 * @return the previous value associated with key, or null if there was no mapping for key. 114 */ 115 default V remove(Object key) { 116 return getDelegate().remove(key); 117 } 118 119 /** 120 * Returns the number of key-value mappings in this map. 121 * @return the number of key-value mappings in this map 122 * @see Map#size() 123 */ 124 default int size() { 125 return getDelegate().size(); 126 } 127 128 /** 129 * Returns a {@link Collection} view of the values contained in this map. 130 * @return a collection view of the values contained in this map 131 */ 132 default Collection<V> values() { 133 return getDelegate().values(); 134 } 135 136}