ImmutableAbstractMap.java

  1. package org.opentrafficsim.core.immutablecollections;

  2. import java.util.Map;

  3. /**
  4.  * An abstract base class for an immutable wrapper for a Map.
  5.  * <p>
  6.  * Copyright (c) 2013-2016 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
  7.  * BSD-style license. See <a href="http://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
  8.  * </p>
  9.  * $LastChangedDate: 2015-07-24 02:58:59 +0200 (Fri, 24 Jul 2015) $, @version $Revision: 1147 $, by $Author: averbraeck $,
  10.  * initial version May 7, 2016 <br>
  11.  * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
  12.  * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
  13.  * @author <a href="http://www.transport.citg.tudelft.nl">Wouter Schakel</a>
  14.  * @param <K> the key type of content of this Map
  15.  * @param <V> the value type of content of this Map
  16.  */
  17. public abstract class ImmutableAbstractMap<K, V> implements ImmutableMap<K, V>
  18. {
  19.     /** */
  20.     private static final long serialVersionUID = 20160507L;

  21.     /** the map that is wrapped, without giving access to methods that can change it. */
  22.     private final Map<K, V> map;

  23.     /**
  24.      * Construct an abstract immutable map. Make sure that the argument is a safe copy of the map of the right type!
  25.      * @param map a safe copy of the map to use as the immutable map
  26.      */
  27.     protected ImmutableAbstractMap(final Map<K, V> map)
  28.     {
  29.         this.map = map;
  30.     }

  31.     /**
  32.      * Prepare the map of the right type for use a subclass. Implement e.g. as follows:
  33.      *
  34.      * <pre>
  35.      * {@literal @}Override
  36.      * protected HashMap&lt;E&gt; getMap()
  37.      * {
  38.      *     return (HashMap&lt;E&gt;) super.getMap();
  39.      * }
  40.      * </pre>
  41.      * @return the map of the right type for use a subclass
  42.      */
  43.     @SuppressWarnings("checkstyle:designforextension")
  44.     protected Map<K, V> getMap()
  45.     {
  46.         return this.map;
  47.     }

  48.     /** {@inheritDoc} */
  49.     @Override
  50.     public final int size()
  51.     {
  52.         return this.map.size();
  53.     }

  54.     /** {@inheritDoc} */
  55.     @Override
  56.     public final boolean isEmpty()
  57.     {
  58.         return this.map.isEmpty();
  59.     }

  60.     /** {@inheritDoc} */
  61.     @Override
  62.     public final boolean containsKey(final Object key)
  63.     {
  64.         return this.map.containsKey(key);
  65.     }

  66.     /** {@inheritDoc} */
  67.     @Override
  68.     public final boolean containsValue(final Object value)
  69.     {
  70.         return this.map.containsValue(value);
  71.     }

  72.     /** {@inheritDoc} */
  73.     @Override
  74.     public final V get(final Object key)
  75.     {
  76.         return this.map.get(key);
  77.     }

  78.     /** {@inheritDoc} */
  79.     @Override
  80.     public final ImmutableCollection<V> values()
  81.     {
  82.         return new ImmutableHashSet<>(this.map.values());
  83.     }

  84.     /** {@inheritDoc} */
  85.     @Override
  86.     @SuppressWarnings("checkstyle:designforextension")
  87.     public int hashCode()
  88.     {
  89.         final int prime = 31;
  90.         int result = 1;
  91.         result = prime * result + ((this.map == null) ? 0 : this.map.hashCode());
  92.         return result;
  93.     }

  94.     /** {@inheritDoc} */
  95.     @Override
  96.     @SuppressWarnings({ "checkstyle:designforextension", "checkstyle:needbraces" })
  97.     public boolean equals(final Object obj)
  98.     {
  99.         if (this == obj)
  100.             return true;
  101.         if (obj == null)
  102.             return false;
  103.         if (getClass() != obj.getClass())
  104.             return false;
  105.         ImmutableAbstractMap<?, ?> other = (ImmutableAbstractMap<?, ?>) obj;
  106.         if (this.map == null)
  107.         {
  108.             if (other.map != null)
  109.                 return false;
  110.         }
  111.         else if (!this.map.equals(other.map))
  112.             return false;
  113.         return true;
  114.     }

  115.     /** {@inheritDoc} */
  116.     @Override
  117.     @SuppressWarnings("checkstyle:designforextension")
  118.     public String toString()
  119.     {
  120.         return "Immutable[" + this.map.toString() + "]";
  121.     }
  122. }