1 /** 2 * The utillib library. 3 * More information is available at http://www.jinchess.com/. 4 * Copyright (C) 2002 Alexander Maryanovsky. 5 * All rights reserved. 6 * 7 * The utillib library is free software; you can redistribute 8 * it and/or modify it under the terms of the GNU Lesser General Public License 9 * as published by the Free Software Foundation; either version 2 of the 10 * License, or (at your option) any later version. 11 * 12 * The utillib library is distributed in the hope that it will 13 * be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser 15 * General Public License for more details. 16 * 17 * You should have received a copy of the GNU Lesser General Public License 18 * along with utillib library; if not, write to the Free Software 19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 20 */ 21 22 package free.util; 23 24 import java.util.Hashtable; 25 26 27 /** 28 * This is a convenient superclass for classes whose job is to be containers 29 * of final (constant) data. It's especially convenient for holding a large 30 * amount of fields, since it alleviates the need for instance variables, 31 * instead holding the data in a Hashtable. 32 */ 33 34 public class Struct{ 35 36 37 /** 38 * A Hashtable where we hold all the properties. 39 */ 40 41 private final Hashtable properties; 42 43 44 45 /** 46 * Creates a new Struct. 47 */ 48 Struct()49 public Struct(){ 50 this(10); 51 } 52 53 54 55 /** 56 * Creates a new struct with the specified estimated amount of properties it 57 * will contain. 58 */ 59 Struct(int propertiesCount)60 public Struct(int propertiesCount){ 61 properties = new Hashtable(propertiesCount); 62 } 63 64 65 66 /** 67 * Sets the value of the given property. A property may not have its value 68 * changed after it's been set. 69 * <P><B>Known bug:</B> Since Hashtables don't allow <code>null</code> values, 70 * you may change the value from <code>null</code> to any value. 71 */ 72 setProperty(String propertyName, Object propertyValue)73 protected final void setProperty(String propertyName, Object propertyValue){ 74 Object oldValue; 75 if (propertyValue != null) 76 oldValue = properties.put(propertyName, propertyValue); 77 else 78 oldValue = properties.get(propertyName); 79 if (oldValue != null){ 80 properties.put(propertyName, oldValue); 81 throw new IllegalArgumentException("A property's value may not be reset - attempted to change the value of "+propertyName); 82 } 83 } 84 85 86 87 88 /** 89 * Sets an integer property. 90 */ 91 setIntegerProperty(String propertyName, int propertyValue)92 protected final void setIntegerProperty(String propertyName, int propertyValue){ 93 setProperty(propertyName, new Integer(propertyValue)); 94 } 95 96 97 98 99 /** 100 * Sets a character property. 101 */ 102 setCharProperty(String propertyName, char propertyValue)103 protected final void setCharProperty(String propertyName, char propertyValue){ 104 setProperty(propertyName, new Character(propertyValue)); 105 } 106 107 108 109 110 /** 111 * Sets a boolean property. 112 */ 113 setBooleanProperty(String propertyName, boolean propertyValue)114 protected final void setBooleanProperty(String propertyName, boolean propertyValue){ 115 setProperty(propertyName, propertyValue ? Boolean.TRUE : Boolean.FALSE); 116 } 117 118 119 120 121 /** 122 * Sets a String property. 123 */ 124 setStringProperty(String propertyName, String propertyValue)125 protected final void setStringProperty(String propertyName, String propertyValue){ 126 setProperty(propertyName, propertyValue); 127 } 128 129 130 131 132 /** 133 * Returns the value of the given property. 134 */ 135 getProperty(Object property)136 protected final Object getProperty(Object property){ 137 return properties.get(property); 138 } 139 140 141 142 /** 143 * Returns the value of the given integer property. 144 */ 145 getIntegerProperty(Object property)146 protected final int getIntegerProperty(Object property){ 147 return ((Integer)getProperty(property)).intValue(); 148 } 149 150 151 152 153 /** 154 * Returns the value of the given character property. 155 */ 156 getCharProperty(Object property)157 protected final char getCharProperty(Object property){ 158 return ((Character)getProperty(property)).charValue(); 159 } 160 161 162 163 164 /** 165 * Returns the value of the given string property. 166 */ 167 getStringProperty(Object property)168 protected final String getStringProperty(Object property){ 169 return (String)getProperty(property); 170 } 171 172 173 174 /** 175 * Returns the value of the given boolean property. 176 */ 177 getBooleanProperty(Object property)178 protected final boolean getBooleanProperty(Object property){ 179 return ((Boolean)getProperty(property)).booleanValue(); 180 } 181 182 183 } 184