1 /* 2 * CDDL HEADER START 3 * 4 * The contents of this file are subject to the terms of the 5 * Common Development and Distribution License, Version 1.0 only 6 * (the "License"). You may not use this file except in compliance 7 * with the License. 8 * 9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 10 * or http://www.opensolaris.org/os/licensing. 11 * See the License for the specific language governing permissions 12 * and limitations under the License. 13 * 14 * When distributing Covered Code, include this CDDL HEADER in each 15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 16 * If applicable, add the following below this CDDL HEADER, with the 17 * fields enclosed by brackets "[]" replaced with your own identifying 18 * information: Portions Copyright [yyyy] [name of copyright owner] 19 * 20 * CDDL HEADER END 21 */ 22 /* 23 * ident "%Z%%M% %I% %E% SMI" 24 * 25 * Copyright (c) 2000 by Sun Microsystems, Inc. 26 * All rights reserved. 27 */ 28 29 /* 30 * Copyright (C) 1996 Active Software, Inc. 31 * All rights reserved. 32 * 33 * @(#) BaseEnumHelper.java 1.5 - last change made 07/18/96 34 */ 35 36 package sunsoft.jws.visual.rt.type; 37 38 import java.util.Hashtable; 39 import java.util.Vector; 40 import java.util.Enumeration; 41 42 /** 43 * Keeps track of the string/int pairs in sub-classes of BaseEnum. 44 * This is meant to be a class-wide member for each subclass of 45 * BaseEnum. Each subclass of BaseEnum should provide a single 46 * instance of BaseEnumHelper for use by all instances of that subclass. 47 * 48 * @see BaseEnum 49 * @version 1.5, 07/18/96 50 */ 51 public class BaseEnumHelper { 52 /** 53 * Ordered list of the enum names. 54 */ 55 private Vector names = new Vector(); 56 57 /** 58 * Pairs keyed by the description. 59 */ 60 private Hashtable keyedByString = new Hashtable(); 61 62 /** 63 * Pairs keyed by the integer value. 64 */ 65 private Hashtable keyedByInteger = new Hashtable(); 66 67 /** 68 * The default if no choice is initially specified. Sub-classers 69 * shouldn't rely on this and should call setDefaultChoice in their 70 * static initializer. 71 */ 72 private int defaultChoice = 0; 73 74 /** 75 * Sets the default choice for newly constructed 76 * instances of the enum. 77 */ setDefaultChoice(int value)78 public void setDefaultChoice(int value) { 79 defaultChoice = value; 80 } 81 82 /** 83 * Gets the default choice for newly constructed 84 * instances of the enum. 85 */ getDefaultChoice()86 public int getDefaultChoice() { 87 return (defaultChoice); 88 } 89 90 /** 91 * Adds a new string/int pair to the internal hashtable. 92 */ add(int value, String name)93 public void add(int value, String name) { 94 Integer I = new Integer(value); 95 names.addElement(name); 96 keyedByString.put(name, I); 97 keyedByInteger.put(I, name); 98 } 99 100 /** 101 * Returns true if the choice is valid for this enum type. 102 */ isValid(int choice)103 public boolean isValid(int choice) { 104 return (keyedByInteger.containsKey(new Integer(choice))); 105 } 106 107 /** 108 * Returns true if the choice is valid for this enum type. 109 */ isValid(String choice)110 public boolean isValid(String choice) { 111 return (keyedByString.containsKey(choice)); 112 } 113 114 /** 115 * Returns the Integer associated with a String key. 116 */ getInteger(String key)117 public Integer getInteger(String key) { 118 return ((Integer) keyedByString.get(key)); 119 } 120 121 /** 122 * Returns the String associated with an Integer key. 123 */ getString(Integer key)124 public String getString(Integer key) { 125 return ((String) keyedByInteger.get(key)); 126 } 127 128 /** 129 * Returns an Enumeration of the String descriptions 130 * available in this enum. 131 */ elements()132 public Enumeration elements() { 133 return (names.elements()); 134 } 135 136 /** 137 * Returns an array containing all of the String descriptions 138 * available in this enum. 139 */ descriptions()140 public String[] descriptions() { 141 String retval[] = new String[names.size()]; 142 names.copyInto(retval); 143 return (retval); 144 } 145 } 146