DualHashBidiMap.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.bidimap;

  18. import java.io.IOException;
  19. import java.io.ObjectInputStream;
  20. import java.io.ObjectOutputStream;
  21. import java.io.Serializable;
  22. import java.util.HashMap;
  23. import java.util.Map;

  24. import org.apache.commons.collections4.BidiMap;

  25. /**
  26.  * Implements {@link BidiMap} with two {@link HashMap} instances.
  27.  * <p>
  28.  * Two {@link HashMap} instances are used in this class.
  29.  * This provides fast lookups at the expense of storing two sets of map entries.
  30.  * Commons Collections would welcome the addition of a direct hash-based
  31.  * implementation of the {@link BidiMap} interface.
  32.  * </p>
  33.  * <p>
  34.  * NOTE: From Commons Collections 3.1, all subclasses will use {@link HashMap}
  35.  * and the flawed {@code createMap} method is ignored.
  36.  * </p>
  37.  *
  38.  * @param <K> the type of the keys in the map
  39.  * @param <V> the type of the values in the map
  40.  * @since 3.0
  41.  */
  42. public class DualHashBidiMap<K, V> extends AbstractDualBidiMap<K, V> implements Serializable {

  43.     /** Ensure serialization compatibility */
  44.     private static final long serialVersionUID = 721969328361808L;

  45.     /**
  46.      * Creates an empty {@code HashBidiMap}.
  47.      */
  48.     public DualHashBidiMap() {
  49.         super(new HashMap<>(), new HashMap<>());
  50.     }

  51.     /**
  52.      * Constructs a {@code HashBidiMap} and copies the mappings from
  53.      * specified {@code Map}.
  54.      *
  55.      * @param map  the map whose mappings are to be placed in this map
  56.      */
  57.     public DualHashBidiMap(final Map<? extends K, ? extends V> map) {
  58.         super(new HashMap<>(), new HashMap<>());
  59.         putAll(map);
  60.     }

  61.     /**
  62.      * Constructs a {@code HashBidiMap} that decorates the specified maps.
  63.      *
  64.      * @param normalMap  the normal direction map
  65.      * @param reverseMap  the reverse direction map
  66.      * @param inverseBidiMap  the inverse BidiMap
  67.      */
  68.     protected DualHashBidiMap(final Map<K, V> normalMap, final Map<V, K> reverseMap,
  69.                               final BidiMap<V, K> inverseBidiMap) {
  70.         super(normalMap, reverseMap, inverseBidiMap);
  71.     }

  72.     /**
  73.      * Creates a new instance of this object.
  74.      *
  75.      * @param normalMap  the normal direction map
  76.      * @param reverseMap  the reverse direction map
  77.      * @param inverseBidiMap  the inverse BidiMap
  78.      * @return new bidi map
  79.      */
  80.     @Override
  81.     protected BidiMap<V, K> createBidiMap(final Map<V, K> normalMap, final Map<K, V> reverseMap,
  82.                                           final BidiMap<K, V> inverseBidiMap) {
  83.         return new DualHashBidiMap<>(normalMap, reverseMap, inverseBidiMap);
  84.     }

  85.     /**
  86.      * Deserializes an instance from an ObjectInputStream.
  87.      *
  88.      * @param in The source ObjectInputStream.
  89.      * @throws IOException            Any of the usual Input/Output related exceptions.
  90.      * @throws ClassNotFoundException A class of a serialized object cannot be found.
  91.      */
  92.     private void readObject(final ObjectInputStream in) throws IOException, ClassNotFoundException {
  93.         in.defaultReadObject();
  94.         normalMap = new HashMap<>();
  95.         reverseMap = new HashMap<>();
  96.         @SuppressWarnings("unchecked") // will fail at runtime if stream is incorrect
  97.         final Map<K, V> map = (Map<K, V>) in.readObject();
  98.         putAll(map);
  99.     }

  100.     /**
  101.      * Serializes this object to an ObjectOutputStream.
  102.      *
  103.      * @param out the target ObjectOutputStream.
  104.      * @throws IOException thrown when an I/O errors occur writing to the target stream.
  105.      */
  106.     private void writeObject(final ObjectOutputStream out) throws IOException {
  107.         out.defaultWriteObject();
  108.         out.writeObject(normalMap);
  109.     }

  110. }