001: /*BEGIN_COPYRIGHT_BLOCK
002: *
003: * Copyright (c) 2001-2007, JavaPLT group at Rice University (javaplt@rice.edu)
004: * All rights reserved.
005: *
006: * Redistribution and use in source and binary forms, with or without
007: * modification, are permitted provided that the following conditions are met:
008: * * Redistributions of source code must retain the above copyright
009: * notice, this list of conditions and the following disclaimer.
010: * * Redistributions in binary form must reproduce the above copyright
011: * notice, this list of conditions and the following disclaimer in the
012: * documentation and/or other materials provided with the distribution.
013: * * Neither the names of DrJava, the JavaPLT group, Rice University, nor the
014: * names of its contributors may be used to endorse or promote products
015: * derived from this software without specific prior written permission.
016: *
017: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
018: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
019: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
020: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
021: * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
022: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
023: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
024: * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
025: * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
026: * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
027: * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
028: *
029: * This software is Open Source Initiative approved Open Source Software.
030: * Open Source Initative Approved is a trademark of the Open Source Initiative.
031: *
032: * This file is part of DrJava. Download the current version of this project
033: * from http://www.drjava.org/ or http://sourceforge.net/projects/drjava/
034: *
035: * END_COPYRIGHT_BLOCK*/
036:
037: package edu.rice.cs.util;
038:
039: import java.util.*;
040:
041: public class OrderedBidirectionalHashMap<Type1, Type2> extends
042: BidirectionalHashMap<Type1, Type2> {
043: private ArrayList<Type2> order = new ArrayList<Type2>();
044:
045: public OrderedBidirectionalHashMap() {
046: super ();
047: }
048:
049: public void put(Type1 key, Type2 value) {
050: super .put(key, value);
051: order.add(value);
052: }
053:
054: public Type2 removeValue(Type1 key) {
055: Type2 value = super .removeValue(key);
056: order.remove(value);
057: return value;
058: }
059:
060: public Type1 removeKey(Type2 value) {
061: Type1 key = super .removeKey(value);
062: order.remove(value);
063: return key;
064: }
065:
066: public Iterator<Type2> valuesIterator() {
067: return new OBHMIterator();
068: }
069:
070: public Collection<Type2> values() {
071: return order;
072: }
073:
074: public void clear() {
075: super .clear();
076: order.clear();
077: }
078:
079: /** Iterator class for BiDirectionalHashMap */
080: class OBHMIterator implements Iterator<Type2> {
081:
082: Iterator<Type2> it = order.iterator();
083: // OrderedBidirectionalHashMap<Type1,Type2> OBHMthis = OrderedBidirectionalHashMap.this;
084:
085: /** Cached values of last key and value visited */
086: Type1 lastKey = null;
087: Type2 lastValue = null;
088:
089: public boolean hasNext() {
090: return it.hasNext();
091: }
092:
093: public Type2 next() {
094: lastValue = it.next();
095: return lastValue;
096: }
097:
098: /** Removes last element returned by next(); throws IllegalStateException if no such element */
099: public void remove() {
100: it.remove(); /* throws exception if lastValue is null */
101: lastKey = backward.get(lastValue);
102: forward.remove(lastKey); /* cannot fail because lastKey is not null */
103: backward.remove(lastValue); /* cannot fail because lastValue is not null */
104: lastValue = null;
105: }
106: }
107: }
|