001: /*
002: * Copyright 2003-2004 The Apache Software Foundation
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.apache.commons.collections.bidimap;
017:
018: import java.io.IOException;
019: import java.io.ObjectInputStream;
020: import java.io.ObjectOutputStream;
021: import java.io.Serializable;
022: import java.util.HashMap;
023: import java.util.Map;
024:
025: import org.apache.commons.collections.BidiMap;
026:
027: /**
028: * Implementation of <code>BidiMap</code> that uses two <code>HashMap</code> instances.
029: * <p>
030: * Two <code>HashMap</code> instances are used in this class.
031: * This provides fast lookups at the expense of storing two sets of map entries.
032: * Commons Collections would welcome the addition of a direct hash-based
033: * implementation of the <code>BidiMap</code> interface.
034: * <p>
035: * NOTE: From Commons Collections 3.1, all subclasses will use <code>HashMap</code>
036: * and the flawed <code>createMap</code> method is ignored.
037: *
038: * @since Commons Collections 3.0
039: * @version $Id: DualHashBidiMap.java 155406 2005-02-26 12:55:26Z dirkv $
040: *
041: * @author Matthew Hawthorne
042: * @author Stephen Colebourne
043: */
044: public class DualHashBidiMap extends AbstractDualBidiMap implements
045: Serializable {
046:
047: /** Ensure serialization compatibility */
048: private static final long serialVersionUID = 721969328361808L;
049:
050: /**
051: * Creates an empty <code>HashBidiMap</code>.
052: */
053: public DualHashBidiMap() {
054: super (new HashMap(), new HashMap());
055: }
056:
057: /**
058: * Constructs a <code>HashBidiMap</code> and copies the mappings from
059: * specified <code>Map</code>.
060: *
061: * @param map the map whose mappings are to be placed in this map
062: */
063: public DualHashBidiMap(Map map) {
064: super (new HashMap(), new HashMap());
065: putAll(map);
066: }
067:
068: /**
069: * Constructs a <code>HashBidiMap</code> that decorates the specified maps.
070: *
071: * @param normalMap the normal direction map
072: * @param reverseMap the reverse direction map
073: * @param inverseBidiMap the inverse BidiMap
074: */
075: protected DualHashBidiMap(Map normalMap, Map reverseMap,
076: BidiMap inverseBidiMap) {
077: super (normalMap, reverseMap, inverseBidiMap);
078: }
079:
080: /**
081: * Creates a new instance of this object.
082: *
083: * @param normalMap the normal direction map
084: * @param reverseMap the reverse direction map
085: * @param inverseBidiMap the inverse BidiMap
086: * @return new bidi map
087: */
088: protected BidiMap createBidiMap(Map normalMap, Map reverseMap,
089: BidiMap inverseBidiMap) {
090: return new DualHashBidiMap(normalMap, reverseMap,
091: inverseBidiMap);
092: }
093:
094: // Serialization
095: //-----------------------------------------------------------------------
096: private void writeObject(ObjectOutputStream out) throws IOException {
097: out.defaultWriteObject();
098: out.writeObject(maps[0]);
099: }
100:
101: private void readObject(ObjectInputStream in) throws IOException,
102: ClassNotFoundException {
103: in.defaultReadObject();
104: maps[0] = new HashMap();
105: maps[1] = new HashMap();
106: Map map = (Map) in.readObject();
107: putAll(map);
108: }
109:
110: }
|