001/* 002 * $Source: /cvsroot2/open/projects/WebARTS/ca/bc/webarts/widgets/ClassConstants.java,v $ 003 * $Name: $ 004 * Current File Status: $Revision: 567 $ 005 * $Date: 2012-11-03 20:36:02 -0700 (Sat, 03 Nov 2012) $ 006 * $Locker: $ 007 * Copyright (C) 2001-2004 WebARTS Design, North Vancouver Canada 008 */ 009/* 010 * This program is free software; you can redistribute it and/or modify 011 * it under the terms of the GNU General Public License as published by 012 * the Free Software Foundation; version 2 of the License. 013 * 014 * This program is distributed in the hope that it will be useful, 015 * but WITHOUT ANY WARRANTY; without even the implied warranty of 016 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 017 * GNU General Public License for more details. 018 * 019 * You should have received a copy of the GNU General Public License 020 * along with this program; if not, write to the Free Software 021 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 022 */ 023package ca.bc.webarts.widgets; 024 025import java.util.Collection; 026import java.util.Enumeration; 027import java.util.Hashtable; 028import java.util.NoSuchElementException; 029 030/** 031 * A class to automate the assignment and usage of enumerated Class Constants. 032 * 033 * <P>It is a combination Enumeration and Hashtable. 034 * 035 * <P>It takes a list of constant names (Strings) and Hashes a unique int with them. 036 * This allows the user to access the Constants via an Enum mechanism or a Hash 037 * mechanism. 038 * 039 * @author tom gutwin 040 * @created June 9, 2001 041 */ 042public class ClassConstants extends Hashtable 043 implements Enumeration 044{ 045 /** 046 * The current index into the Enumeration of Constants. 047 * 048 * @since 049 */ 050 private int currentIndex_ = 0; 051 052 053 /** 054 * The number of Constants defined/assigned. 055 * 056 * @since 057 */ 058 private int numConstants_ = 0; 059 060 /** 061 * The enumeration of constant names. 062 * 063 * @since 064 */ 065 private Enumeration enum_ = null; 066 067 068 /** 069 * The collection of int valkues assigned to the instantiated class 070 * constants. 071 * 072 * @since 073 */ 074 private Collection values_ = null; 075 076 077 /** 078 * Default constructor that takes a list of names and assigns constant numbers 079 * starting from 0 and incrementing by 1. 080 * 081 * @param constantNames the list of names to give the constants 082 * @since 083 */ 084 public ClassConstants(String[] constantNames) 085 { 086 this(0, 1, constantNames); 087 } 088 089 090 /** 091 * Constructor that takes a list of names and assigns constant numbers starting 092 * from the passed in 'startVal' and incrementing by 1. 093 * 094 * @param startVal the int to start the enumeration at 095 * @param constantNames the list of names to give the constants 096 * @since 097 */ 098 public ClassConstants(int startVal, String[] constantNames) 099 { 100 this(startVal, 1, constantNames); 101 } 102 103 104 /** 105 * Constructor that takes a list of names and assigns constant numbers starting 106 * from the passed in 'startVal' and incrementing by the passed in stepVal. 107 * 108 * @param startVal the int to start the enumeration at 109 * @param stepVal the int to step the enumeration by 110 * @param constantNames the list of names to give the constants 111 * @since 112 */ 113 public ClassConstants(int startVal, 114 int stepVal, 115 String[] constantNames) 116 { 117 for (int i = startVal; i < constantNames.length * stepVal; i += stepVal) 118 { 119 put(constantNames, new Integer(i)); 120 } 121 values_ = values(); 122 enum_ = keys(); 123 } 124 125 126 /** 127 * Gets the Constant valueof the ClassConstants object 128 * 129 * @param constName the constant to get 130 * @return The Value Of the Constant 131 * @since 132 */ 133 public int getValueOf(String constName) 134 { 135 return ((Integer)(this.get(constName))).intValue() ; 136 } 137 138 139 /** 140 * Does this classes enumeration of constants have any more elements. 141 * 142 * @return true if the enum has mor elements, false if done 143 * @since 144 */ 145 public boolean hasMoreElements() 146 { 147 boolean retVal = false; 148 if (enum_ != null) 149 { 150 retVal = enum_.hasMoreElements(); 151 } 152 return retVal; 153 } 154 155 156 /** 157 * Returns the next enumerated Constant as an Object. 158 * 159 * @return Description of the Returned Value 160 * @exception NoSuchElementException Description of Exception 161 * @since 162 */ 163 public Object nextElement() throws java.util.NoSuchElementException 164 { 165 Object retVal = null; 166 if (enum_ != null && hasMoreElements()) 167 { 168 retVal = enum_.nextElement(); 169 } 170 if (retVal == null) 171 { 172 throw new java.util.NoSuchElementException(); 173 } 174 return retVal; 175 } 176 177 178 /** 179 * Gets the next Enumerated Clas Constant Name. 180 * 181 * @return the next Enumerated Clas Constant Name. 182 * @since 183 * @throws NoSuchElementException 184 */ 185 public String nextConstantName() throws java.util.NoSuchElementException 186 { 187 String retVal = null; 188 if (enum_ != null && hasMoreElements()) 189 { 190 retVal = (String)enum_.nextElement(); 191 } 192 if (retVal == null) 193 { 194 throw new java.util.NoSuchElementException(); 195 } 196 return retVal; 197 } 198 199 200 /** 201 * Compares the passed in Constant name with 202 * 203 * @param constName Constant name to compare 204 * @param val the int value to compare to the passed in Constant. 205 * @return true if the same false if not. 206 * @since 207 */ 208 public boolean equals(String constName, int val) 209 { 210 return (getValueOf(constName) == val); 211 } 212 213 214 /** 215 * Description of the Method 216 * 217 * @param constName Description of Parameter 218 * @return Description of the Returned Value 219 * @since 220 */ 221 public boolean contains(String constName) 222 { 223 return this.containsKey(constName); 224 } 225} 226