1 /* 2 * [The "BSD license"] 3 * Copyright (c) 2010 Terence Parr 4 * All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 3. The name of the author may not be used to endorse or promote products 15 * derived from this software without specific prior written permission. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27 */ 28 package org.antlr.misc; 29 30 import java.util.ArrayList; 31 import java.util.Iterator; 32 import java.util.LinkedHashSet; 33 import java.util.List; 34 35 /** A HashMap that remembers the order that the elements were added. 36 * You can alter the ith element with set(i,value) too :) Unique list. 37 * I need the replace/set-element-i functionality so I'm subclassing 38 * OrderedHashSet. 39 */ 40 public class OrderedHashSet<T> extends LinkedHashSet<T> { 41 /** Track the elements as they are added to the set */ 42 protected List<T> elements = new ArrayList<T>(); 43 get(int i)44 public T get(int i) { 45 return elements.get(i); 46 } 47 48 /** Replace an existing value with a new value; updates the element 49 * list and the hash table, but not the key as that has not changed. 50 */ set(int i, T value)51 public T set(int i, T value) { 52 T oldElement = elements.get(i); 53 elements.set(i,value); // update list 54 super.remove(oldElement); // now update the set: remove/add 55 super.add(value); 56 return oldElement; 57 } 58 59 /** Add a value to list; keep in hashtable for consistency also; 60 * Key is object itself. Good for say asking if a certain string is in 61 * a list of strings. 62 */ 63 @Override add(T value)64 public boolean add(T value) { 65 boolean result = super.add(value); 66 if ( result ) { // only track if new element not in set 67 elements.add(value); 68 } 69 return result; 70 } 71 72 @Override remove(Object o)73 public boolean remove(Object o) { 74 throw new UnsupportedOperationException(); 75 /* 76 elements.remove(o); 77 return super.remove(o); 78 */ 79 } 80 81 @Override clear()82 public void clear() { 83 elements.clear(); 84 super.clear(); 85 } 86 87 /** Return the List holding list of table elements. Note that you are 88 * NOT getting a copy so don't write to the list. 89 */ elements()90 public List<T> elements() { 91 return elements; 92 } 93 94 @Override iterator()95 public Iterator<T> iterator() { 96 return elements.iterator(); 97 } 98 99 @Override toArray()100 public Object[] toArray() { 101 return elements.toArray(); 102 } 103 104 @Override size()105 public int size() { 106 /* 107 if ( elements.size()!=super.size() ) { 108 ErrorManager.internalError("OrderedHashSet: elements and set size differs; "+ 109 elements.size()+"!="+super.size()); 110 } 111 */ 112 return elements.size(); 113 } 114 115 @Override toString()116 public String toString() { 117 return elements.toString(); 118 } 119 } 120