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.splitmap; 018 019import java.util.Collection; 020import java.util.Map; 021import java.util.Objects; 022import java.util.Set; 023 024import org.apache.commons.collections4.IterableGet; 025import org.apache.commons.collections4.MapIterator; 026import org.apache.commons.collections4.map.EntrySetToMapIteratorAdapter; 027 028/** 029 * {@link IterableGet} that uses a {@link Map}<K, V> for the 030 * {@link org.apache.commons.collections4.Get Get}<K, V> implementation. 031 * 032 * @param <K> the type of the keys in this map 033 * @param <V> the type of the values in this map 034 * @since 4.0 035 */ 036public class AbstractIterableGetMapDecorator<K, V> implements IterableGet<K, V> { 037 038 /** The map to decorate */ 039 transient Map<K, V> map; 040 041 /** 042 * Constructor only used in deserialization, do not use otherwise. 043 */ 044 protected AbstractIterableGetMapDecorator() { 045 } 046 047 /** 048 * Create a new AbstractSplitMapDecorator. 049 * @param map the map to decorate, must not be null 050 * @throws NullPointerException if map is null 051 */ 052 public AbstractIterableGetMapDecorator(final Map<K, V> map) { 053 this.map = Objects.requireNonNull(map, "map"); 054 } 055 056 @Override 057 public boolean containsKey(final Object key) { 058 return decorated().containsKey(key); 059 } 060 061 @Override 062 public boolean containsValue(final Object value) { 063 return decorated().containsValue(value); 064 } 065 066 /** 067 * Gets the map being decorated. 068 * 069 * @return the decorated map 070 */ 071 protected Map<K, V> decorated() { 072 return map; 073 } 074 075 @Override 076 public Set<Map.Entry<K, V>> entrySet() { 077 return decorated().entrySet(); 078 } 079 080 @Override 081 public boolean equals(final Object object) { 082 if (object == this) { 083 return true; 084 } 085 return decorated().equals(object); 086 } 087 088 @Override 089 public V get(final Object key) { 090 return decorated().get(key); 091 } 092 093 @Override 094 public int hashCode() { 095 return decorated().hashCode(); 096 } 097 098 @Override 099 public boolean isEmpty() { 100 return decorated().isEmpty(); 101 } 102 103 @Override 104 public Set<K> keySet() { 105 return decorated().keySet(); 106 } 107 108 /** 109 * Gets a MapIterator over this Get. 110 * @return MapIterator<K, V> 111 */ 112 @Override 113 public MapIterator<K, V> mapIterator() { 114 return new EntrySetToMapIteratorAdapter<>(entrySet()); 115 } 116 117 @Override 118 public V remove(final Object key) { 119 return decorated().remove(key); 120 } 121 122 @Override 123 public int size() { 124 return decorated().size(); 125 } 126 127 @Override 128 public String toString() { 129 return decorated().toString(); 130 } 131 132 @Override 133 public Collection<V> values() { 134 return decorated().values(); 135 } 136 137}