AbstractIterableGetMapDecorator.java

  1. /*
  2.  * Licensed to the Apache Software Foundation (ASF) under one or more
  3.  * contributor license agreements.  See the NOTICE file distributed with
  4.  * this work for additional information regarding copyright ownership.
  5.  * The ASF licenses this file to You under the Apache License, Version 2.0
  6.  * (the "License"); you may not use this file except in compliance with
  7.  * the License.  You may obtain a copy of the License at
  8.  *
  9.  *      http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */
  17. package org.apache.commons.collections4.splitmap;

  18. import java.util.Collection;
  19. import java.util.Map;
  20. import java.util.Objects;
  21. import java.util.Set;

  22. import org.apache.commons.collections4.IterableGet;
  23. import org.apache.commons.collections4.MapIterator;
  24. import org.apache.commons.collections4.map.EntrySetToMapIteratorAdapter;

  25. /**
  26.  * {@link IterableGet} that uses a {@link Map}<K, V> for the
  27.  * {@link org.apache.commons.collections4.Get Get}<K, V> implementation.
  28.  *
  29.  * @param <K> the type of the keys in this map
  30.  * @param <V> the type of the values in this map
  31.  * @since 4.0
  32.  */
  33. public class AbstractIterableGetMapDecorator<K, V> implements IterableGet<K, V> {

  34.     /** The map to decorate */
  35.     transient Map<K, V> map;

  36.     /**
  37.      * Constructor only used in deserialization, do not use otherwise.
  38.      */
  39.     protected AbstractIterableGetMapDecorator() {
  40.     }

  41.     /**
  42.      * Create a new AbstractSplitMapDecorator.
  43.      * @param map the map to decorate, must not be null
  44.      * @throws NullPointerException if map is null
  45.      */
  46.     public AbstractIterableGetMapDecorator(final Map<K, V> map) {
  47.         this.map = Objects.requireNonNull(map, "map");
  48.     }

  49.     @Override
  50.     public boolean containsKey(final Object key) {
  51.         return decorated().containsKey(key);
  52.     }

  53.     @Override
  54.     public boolean containsValue(final Object value) {
  55.         return decorated().containsValue(value);
  56.     }

  57.     /**
  58.      * Gets the map being decorated.
  59.      *
  60.      * @return the decorated map
  61.      */
  62.     protected Map<K, V> decorated() {
  63.         return map;
  64.     }

  65.     @Override
  66.     public Set<Map.Entry<K, V>> entrySet() {
  67.         return decorated().entrySet();
  68.     }

  69.     @Override
  70.     public boolean equals(final Object object) {
  71.         if (object == this) {
  72.             return true;
  73.         }
  74.         return decorated().equals(object);
  75.     }

  76.     @Override
  77.     public V get(final Object key) {
  78.         return decorated().get(key);
  79.     }

  80.     @Override
  81.     public int hashCode() {
  82.         return decorated().hashCode();
  83.     }

  84.     @Override
  85.     public boolean isEmpty() {
  86.         return decorated().isEmpty();
  87.     }

  88.     @Override
  89.     public Set<K> keySet() {
  90.         return decorated().keySet();
  91.     }

  92.     /**
  93.      * Gets a MapIterator over this Get.
  94.      * @return MapIterator&lt;K, V&gt;
  95.      */
  96.     @Override
  97.     public MapIterator<K, V> mapIterator() {
  98.         return new EntrySetToMapIteratorAdapter<>(entrySet());
  99.     }

  100.     @Override
  101.     public V remove(final Object key) {
  102.         return decorated().remove(key);
  103.     }

  104.     @Override
  105.     public int size() {
  106.         return decorated().size();
  107.     }

  108.     @Override
  109.     public String toString() {
  110.         return decorated().toString();
  111.     }

  112.     @Override
  113.     public Collection<V> values() {
  114.         return decorated().values();
  115.     }

  116. }