AbstractMapDecorator.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.map;

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

  22. /**
  23.  * Provides a base decorator that enables additional functionality to be added
  24.  * to a Map via decoration.
  25.  * <p>
  26.  * Methods are forwarded directly to the decorated map.
  27.  * </p>
  28.  * <p>
  29.  * This implementation does not perform any special processing with
  30.  * {@link #entrySet()}, {@link #keySet()} or {@link #values()}. Instead
  31.  * it simply returns the set/collection from the wrapped map. This may be
  32.  * undesirable, for example if you are trying to write a validating
  33.  * implementation it would provide a loophole around the validation.
  34.  * But, you might want that loophole, so this class is kept simple.
  35.  * </p>
  36.  *
  37.  * @param <K> the type of the keys in the map
  38.  * @param <V> the type of the values in the map
  39.  * @since 3.0
  40.  */
  41. public abstract class AbstractMapDecorator<K, V> extends AbstractIterableMap<K, V> {

  42.     /** The map to decorate */
  43.     transient Map<K, V> map;

  44.     /**
  45.      * Constructor only used in deserialization, do not use otherwise.
  46.      * @since 3.1
  47.      */
  48.     protected AbstractMapDecorator() {
  49.     }

  50.     /**
  51.      * Constructor that wraps (not copies).
  52.      *
  53.      * @param map  the map to decorate, must not be null
  54.      * @throws NullPointerException if the map is null
  55.      */
  56.     protected AbstractMapDecorator(final Map<K, V> map) {
  57.         this.map = Objects.requireNonNull(map, "map");
  58.     }

  59.     @Override
  60.     public void clear() {
  61.         decorated().clear();
  62.     }

  63.     @Override
  64.     public boolean containsKey(final Object key) {
  65.         return decorated().containsKey(key);
  66.     }

  67.     @Override
  68.     public boolean containsValue(final Object value) {
  69.         return decorated().containsValue(value);
  70.     }

  71.     /**
  72.      * Gets the map being decorated.
  73.      *
  74.      * @return the decorated map
  75.      */
  76.     protected Map<K, V> decorated() {
  77.         return map;
  78.     }

  79.     @Override
  80.     public Set<Map.Entry<K, V>> entrySet() {
  81.         return decorated().entrySet();
  82.     }

  83.     @Override
  84.     public boolean equals(final Object object) {
  85.         if (object == this) {
  86.             return true;
  87.         }
  88.         return decorated().equals(object);
  89.     }

  90.     @Override
  91.     public V get(final Object key) {
  92.         return decorated().get(key);
  93.     }

  94.     @Override
  95.     public int hashCode() {
  96.         return decorated().hashCode();
  97.     }

  98.     @Override
  99.     public boolean isEmpty() {
  100.         return decorated().isEmpty();
  101.     }

  102.     @Override
  103.     public Set<K> keySet() {
  104.         return decorated().keySet();
  105.     }

  106.     @Override
  107.     public V put(final K key, final V value) {
  108.         return decorated().put(key, value);
  109.     }

  110.     @Override
  111.     public void putAll(final Map<? extends K, ? extends V> mapToCopy) {
  112.         decorated().putAll(mapToCopy);
  113.     }

  114.     @Override
  115.     public V remove(final Object key) {
  116.         return decorated().remove(key);
  117.     }

  118.     @Override
  119.     public int size() {
  120.         return decorated().size();
  121.     }

  122.     @Override
  123.     public String toString() {
  124.         return decorated().toString();
  125.     }

  126.     @Override
  127.     public Collection<V> values() {
  128.         return decorated().values();
  129.     }

  130. }