/* * DictionaryTest.java - Test the implementation of Dictionaries. * * Copyright (c) 1996 Chuck McManis, All Rights Reserved. * * Permission to use, copy, modify, and distribute this software * and its documentation for NON-COMMERCIAL purposes and without * fee is hereby granted provided that this copyright notice * appears in all copies. * * CHUCK MCMANIS MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE * SUITABILITY OF THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING * BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT. CHUCK MCMANIS * SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT * OF USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES. */ import java.util.Dictionary; import java.util.Hashtable; import java.util.Enumeration; public class DictionaryTest { static String colors[] = { "red", "green", "orange", "purple", "chartreuse", "gray", "magenta", "cyan", "yellow", "tangerine", "turquoise", "brown", "black", "white", "silver", "gold", "pink", "bronze", "beige", "blue", "aquamarine", }; static ColorKey keys[]; public static void main(String args[]) { Dictionary d = new BinarySearchTree(); keys = new ColorKey[colors.length]; Object o; for (int i = 0; i < colors.length; i++) { keys[i] = new ColorKey(colors[i], (i+1)+": "+colors[i]); d.put(keys[i], keys[i]); } System.out.println("BinarySearchTree contains :"); for (Enumeration e = d.elements(); e.hasMoreElements(); ) { System.out.println(e.nextElement()); } try { System.out.println("press any key to continue."); int zz = System.in.read(); } catch (Exception e) { } o = d.get(keys[3]); System.out.println("Deleting node : "+o); if (o != null) { o = d.remove(keys[3]); System.out.println("Deleted node : "+ o); } System.out.println("Dictionary contains :"); for (Enumeration e = d.elements(); e.hasMoreElements(); ) { System.out.println(e.nextElement()); } try { System.out.println("press any key to continue."); int zz = System.in.read(); } catch (Exception e) { } } } /** * The ColorKey is both the key and the value in our enhanced * binary tree. */ class ColorKey implements SearchKey { String key; String colorValue; public ColorKey(String k, String v) { key = k; colorValue = v; } private void verifyType(SearchKey k) { if (! (k instanceof ColorKey)) throw new RuntimeException("You cannot mix keys here."); } public int compareKey(SearchKey b) { verifyType(b); return key.compareTo(((ColorKey) b).key); } public boolean equalKey(SearchKey o) { return (compareKey(o) == 0); } public String toString() { return "ColorKey:: '"+key+"' ==> '"+colorValue+"'"; } }