1. /*
  2. * Copyright 2003-2004 The Apache Software Foundation
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package org.apache.commons.collections.bidimap;
  17. import java.io.IOException;
  18. import java.io.ObjectInputStream;
  19. import java.io.ObjectOutputStream;
  20. import java.io.Serializable;
  21. import java.util.HashMap;
  22. import java.util.Map;
  23. import org.apache.commons.collections.BidiMap;
  24. /**
  25. * Implementation of <code>BidiMap</code> that uses two <code>HashMap</code> instances.
  26. * <p>
  27. * Two <code>HashMap</code> instances are used in this class.
  28. * This provides fast lookups at the expense of storing two sets of map entries.
  29. * Commons Collections would welcome the addition of a direct hash-based
  30. * implementation of the <code>BidiMap</code> interface.
  31. * <p>
  32. * NOTE: From Commons Collections 3.1, all subclasses will use <code>HashMap</code>
  33. * and the flawed <code>createMap</code> method is ignored.
  34. *
  35. * @since Commons Collections 3.0
  36. * @version $Id: DualHashBidiMap.java,v 1.7 2004/06/11 23:27:37 scolebourne Exp $
  37. *
  38. * @author Matthew Hawthorne
  39. * @author Stephen Colebourne
  40. */
  41. public class DualHashBidiMap
  42. extends AbstractDualBidiMap implements Serializable {
  43. /** Ensure serialization compatibility */
  44. private static final long serialVersionUID = 721969328361808L;
  45. /**
  46. * Creates an empty <code>HashBidiMap</code>.
  47. */
  48. public DualHashBidiMap() {
  49. super(new HashMap(), new HashMap());
  50. }
  51. /**
  52. * Constructs a <code>HashBidiMap</code> and copies the mappings from
  53. * specified <code>Map</code>.
  54. *
  55. * @param map the map whose mappings are to be placed in this map
  56. */
  57. public DualHashBidiMap(Map map) {
  58. super(new HashMap(), new HashMap());
  59. putAll(map);
  60. }
  61. /**
  62. * Constructs a <code>HashBidiMap</code> 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(Map normalMap, Map reverseMap, BidiMap inverseBidiMap) {
  69. super(normalMap, reverseMap, inverseBidiMap);
  70. }
  71. /**
  72. * Creates a new instance of this object.
  73. *
  74. * @param normalMap the normal direction map
  75. * @param reverseMap the reverse direction map
  76. * @param inverseBidiMap the inverse BidiMap
  77. * @return new bidi map
  78. */
  79. protected BidiMap createBidiMap(Map normalMap, Map reverseMap, BidiMap inverseBidiMap) {
  80. return new DualHashBidiMap(normalMap, reverseMap, inverseBidiMap);
  81. }
  82. // Serialization
  83. //-----------------------------------------------------------------------
  84. private void writeObject(ObjectOutputStream out) throws IOException {
  85. out.defaultWriteObject();
  86. out.writeObject(maps[0]);
  87. }
  88. private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException {
  89. in.defaultReadObject();
  90. maps[0] = new HashMap();
  91. maps[1] = new HashMap();
  92. Map map = (Map) in.readObject();
  93. putAll(map);
  94. }
  95. }