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.map; 018 019import java.util.Collection; 020import java.util.Map; 021import java.util.Set; 022 023/** 024 * Provides a base decorator that enables additional functionality to be added 025 * to a Map via decoration. 026 * <p> 027 * Methods are forwarded directly to the decorated map. 028 * <p> 029 * This implementation does not perform any special processing with 030 * {@link #entrySet()}, {@link #keySet()} or {@link #values()}. Instead 031 * it simply returns the set/collection from the wrapped map. This may be 032 * undesirable, for example if you are trying to write a validating 033 * implementation it would provide a loophole around the validation. 034 * But, you might want that loophole, so this class is kept simple. 035 * 036 * @param <K> the type of the keys in the map 037 * @param <V> the type of the values in the map 038 * @since 3.0 039 * @version $Id: AbstractMapDecorator.html 972421 2015-11-14 20:00:04Z tn $ 040 */ 041public abstract class AbstractMapDecorator<K, V> extends AbstractIterableMap<K, V> { 042 043 /** The map to decorate */ 044 transient Map<K, V> map; 045 046 /** 047 * Constructor only used in deserialization, do not use otherwise. 048 * @since 3.1 049 */ 050 protected AbstractMapDecorator() { 051 super(); 052 } 053 054 /** 055 * Constructor that wraps (not copies). 056 * 057 * @param map the map to decorate, must not be null 058 * @throws IllegalArgumentException if the collection is null 059 */ 060 protected AbstractMapDecorator(final Map<K, V> map) { 061 if (map == null) { 062 throw new IllegalArgumentException("Map must not be null"); 063 } 064 this.map = map; 065 } 066 067 /** 068 * Gets the map being decorated. 069 * 070 * @return the decorated map 071 */ 072 protected Map<K, V> decorated() { 073 return map; 074 } 075 076 //----------------------------------------------------------------------- 077 public void clear() { 078 decorated().clear(); 079 } 080 081 public boolean containsKey(final Object key) { 082 return decorated().containsKey(key); 083 } 084 085 public boolean containsValue(final Object value) { 086 return decorated().containsValue(value); 087 } 088 089 public Set<Map.Entry<K, V>> entrySet() { 090 return decorated().entrySet(); 091 } 092 093 public V get(final Object key) { 094 return decorated().get(key); 095 } 096 097 public boolean isEmpty() { 098 return decorated().isEmpty(); 099 } 100 101 public Set<K> keySet() { 102 return decorated().keySet(); 103 } 104 105 public V put(final K key, final V value) { 106 return decorated().put(key, value); 107 } 108 109 public void putAll(final Map<? extends K, ? extends V> mapToCopy) { 110 decorated().putAll(mapToCopy); 111 } 112 113 public V remove(final Object key) { 114 return decorated().remove(key); 115 } 116 117 public int size() { 118 return decorated().size(); 119 } 120 121 public Collection<V> values() { 122 return decorated().values(); 123 } 124 125 @Override 126 public boolean equals(final Object object) { 127 if (object == this) { 128 return true; 129 } 130 return decorated().equals(object); 131 } 132 133 @Override 134 public int hashCode() { 135 return decorated().hashCode(); 136 } 137 138 @Override 139 public String toString() { 140 return decorated().toString(); 141 } 142 143}