DualLinkedHashBidiMap.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.LinkedHashMap;
  23. import java.util.Map;

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

  25. /**
  26.  * Implements {@link BidiMap} with two {@link LinkedHashMap} instances.
  27.  * <p>
  28.  * Two {@link LinkedHashMap} instances are used in this class.
  29.  * This provides fast lookups at the expense of storing two sets of map entries and two linked lists.
  30.  * </p>
  31.  *
  32.  * @param <K> the type of the keys in the map
  33.  * @param <V> the type of the values in the map
  34.  * @since 4.0
  35.  */
  36. public class DualLinkedHashBidiMap<K, V> extends AbstractDualBidiMap<K, V> implements Serializable {

  37.     /** Ensure serialization compatibility */
  38.     private static final long serialVersionUID = 721969328361810L;

  39.     /**
  40.      * Creates an empty {@code HashBidiMap}.
  41.      */
  42.     public DualLinkedHashBidiMap() {
  43.         super(new LinkedHashMap<>(), new LinkedHashMap<>());
  44.     }

  45.     /**
  46.      * Constructs a {@code LinkedHashBidiMap} and copies the mappings from
  47.      * specified {@link Map}.
  48.      *
  49.      * @param map the map whose mappings are to be placed in this map
  50.      */
  51.     public DualLinkedHashBidiMap(final Map<? extends K, ? extends V> map) {
  52.         super(new LinkedHashMap<>(), new LinkedHashMap<>());
  53.         putAll(map);
  54.     }

  55.     /**
  56.      * Constructs a {@code LinkedHashBidiMap} that decorates the specified maps.
  57.      *
  58.      * @param normalMap      the normal direction map
  59.      * @param reverseMap     the reverse direction map
  60.      * @param inverseBidiMap the inverse BidiMap
  61.      */
  62.     protected DualLinkedHashBidiMap(final Map<K, V> normalMap, final Map<V, K> reverseMap,
  63.                                     final BidiMap<V, K> inverseBidiMap) {
  64.         super(normalMap, reverseMap, inverseBidiMap);
  65.     }

  66.     /**
  67.      * Creates a new instance of this object.
  68.      *
  69.      * @param normalMap      the normal direction map
  70.      * @param reverseMap     the reverse direction map
  71.      * @param inverseBidiMap the inverse BidiMap
  72.      * @return new bidi map
  73.      */
  74.     @Override
  75.     protected BidiMap<V, K> createBidiMap(final Map<V, K> normalMap, final Map<K, V> reverseMap,
  76.             final BidiMap<K, V> inverseBidiMap) {
  77.         return new DualLinkedHashBidiMap<>(normalMap, reverseMap, inverseBidiMap);
  78.     }

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

  94.     /**
  95.      * Serializes this object to an ObjectOutputStream.
  96.      *
  97.      * @param out the target ObjectOutputStream.
  98.      * @throws IOException thrown when an I/O errors occur writing to the target stream.
  99.      */
  100.     private void writeObject(final ObjectOutputStream out) throws IOException {
  101.         out.defaultWriteObject();
  102.         out.writeObject(normalMap);
  103.     }
  104. }