View Javadoc
1 /*** 2 * Copyright (c) 2002, CodeStreet LLC. All rights reserved.<p> 3 * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following 4 * conditions are met:<p> 5 * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 6 * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer 7 * in the documentation and/or other materials provided with the distribution. Neither the name of CodeStreet LLC. nor the 8 * names of its contributors may be used to endorse or promote products derived from this software without specific prior written 9 * permission.<p> 10 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT 11 * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL 12 * THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 13 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 14 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 15 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.<p> 16 */ 17 18 package com.codestreet.messageforge; 19 20 /*** 21 * Class to represent a <tt>boolean</tt> field. 22 * @author Jawaid Hakim. 23 */ 24 public class RFldBool extends RFld 25 { 26 static final long serialVersionUID = -6961256003220528650L; //Generated by jserial 27 28 /*** 29 * Constructor. 30 * @param name Field name. 31 * @param fieldId Field id. Field ids must be either <tt>0</tt> 32 * to indicate that there is no id on the field, or greater. In addition, 33 * field ids must be unique within a messages - no two fields are allowed 34 * to have the same field id. 35 */ 36 public RFldBool(String name, int fieldId) 37 { 38 super(name, fieldId); 39 } 40 41 /*** 42 * Constructor. 43 * @param name Field name. 44 * @param fieldId Field id. Field ids must be either <tt>0</tt> 45 * to indicate that there is no id on the field, or greater. In addition, 46 * field ids must be unique within a messages - no two fields are allowed 47 * to have the same field id. 48 * @param desc Field description. 49 */ 50 public RFldBool(String name, int fieldId, String desc) 51 { 52 super(name, fieldId, desc); 53 } 54 55 /*** 56 * Get field type. 57 * @return Field type <tt>BOOL</tt>. 58 * @see RFldType 59 */ 60 public final RFldType getType() 61 { 62 return RFldType.BOOL; 63 } 64 65 /*** 66 * Check if another field is equal to this field. Equality is defined 67 * as the fields having the same value. 68 * @param anObject Another field. 69 * @return <tt>true</tt> if another field is equal to this field. 70 * Otherwise, returns <tt>false</tt>. 71 */ 72 public final boolean equals(Object anObject) 73 { 74 if (this == anObject) 75 { 76 return true; 77 } 78 else if (! (anObject instanceof RFldBool)) 79 { 80 return false; 81 } 82 83 RFldBool another = (RFldBool)anObject; 84 if (valSet_ != another.isValSet()) 85 return false; 86 87 return (! valSet_ || dataObj_.booleanValue() == another.getValue()); 88 } 89 90 /*** 91 * Returns a hash code for this field. 92 * This implementation is the same as for java.lang.Boolean. 93 * 94 * @return the integer <tt>17</tt> if this object represents 95 * <tt>true</tt>; returns the integer <tt>19</tt> if this 96 * object represents <tt>false</tt>. If this field has not been 97 * set then returns <tt>1231</tt>. 98 */ 99 public final int hashCode() 100 { 101 if (valSet_) 102 return dataObj_.booleanValue() ? 17 : 19; 103 else 104 return 1231; 105 } 106 107 /*** 108 * Reset the field value. 109 * @see #isValSet() 110 */ 111 public final void reset() throws FieldValidationException 112 { 113 if (isLocked()) 114 throw new FieldValidationException("Field " + getName() + " is locked"); 115 116 valSet_ = false; 117 } 118 119 /*** 120 * Set data. 121 * @return Reference to self so method chaining can be used. 122 * @param newData New data. 123 */ 124 public final RFld set(Object newData) throws FieldValidationException 125 { 126 try 127 { 128 return set((Boolean)newData); 129 } 130 catch (ClassCastException e) 131 { 132 try 133 { 134 return set(Boolean.valueOf(newData.toString())); 135 } 136 catch (NumberFormatException ex) 137 { 138 throw new FieldValidationException(ex); 139 } 140 } 141 } 142 143 /*** 144 * Set data. 145 * @return Reference to self so method chaining can be used. 146 * @param newData New data. 147 */ 148 public final RFldBool set(boolean newData) throws FieldValidationException 149 { 150 return set(new Boolean(newData)); 151 } 152 153 /*** 154 * Set the field value from a JDOM element. 155 * @return Reference to self so method chaining can be used. 156 * @param elem Field value as a JDOM element. 157 */ 158 public final RFld set(org.jdom.Element elem) throws FieldValidationException 159 { 160 return set(elem.getAttributeValue(XML_ATTR_VALUE)); 161 } 162 163 /*** 164 * Set data. 165 * @param newData New data. 166 * @return Reference to self so method chaining can be used. 167 * @throws NullPointerException if the new data is <tt>null</tt>. 168 */ 169 public final RFldBool set(Boolean newData) throws FieldValidationException 170 { 171 validate(newData.booleanValue()); 172 173 dataObj_ = newData; 174 valSet_ = true; 175 176 return this; 177 } 178 179 /*** 180 * Validate against constraints. A field is valid if either it's value is set 181 * and satisfies all constraints, or the the field is optional. 182 */ 183 public final void validate() throws FieldValidationException 184 { 185 // Only need to check that non-optional fields have been set. If a 186 // field has been set then it must be valid since validation is done 187 // with each set. 188 if (! valSet_ && ! optional_) 189 throw new FieldValidationException("Field not set: " + getName()); 190 } 191 192 /*** 193 * Check if a new value will satifsy constraints. 194 * @param newData New value. 195 */ 196 public final void validate(boolean newData) throws FieldValidationException 197 { 198 if (locked_) 199 throw new FieldValidationException("Cannot modify locked field: " + getName()); 200 } 201 202 /*** 203 * Get data. 204 * @return Field value. 205 * @throws NullPointerException if the field value is not set. 206 */ 207 public final boolean getValue() 208 { 209 return dataObj_.booleanValue(); 210 } 211 212 /*** 213 * Get the field value as an object. Returns <tt>null</tt> if the field value 214 * is not set. 215 */ 216 public final Object getValueAsObject() 217 { 218 return dataObj_; 219 } 220 221 /*** 222 * Get the field value as a string. Returns <tt>false</tt> if the field value 223 * is not set. 224 * @throws NullPointerException if the field value is not set. 225 */ 226 public final String getValueAsString() 227 { 228 return dataObj_.toString(); 229 } 230 231 /*** 232 * Get the field value as a <tt>java.util.Hashtable</tt>. Not supported - throws 233 * an exception. 234 * @return Field value as java.util.Hashtable. Returns <tt>null</tt> if the field 235 * value is not set. 236 */ 237 public final java.util.Hashtable getValueAsHashtable() throws FieldValidationException 238 { 239 throw new FieldValidationException("Not supported"); 240 } 241 242 /*** 243 * Get the XML tag for this field type. 244 * @return XML tag for this field type. 245 */ 246 public final String getTag() 247 { 248 return XML_TAG; 249 } 250 251 /*** 252 * Set the XML tag for this field type. 253 * @param tag New XML tag for this field type. 254 */ 255 public static void setTag(String tag) 256 { 257 XML_TAG = tag; 258 } 259 260 /*** 261 * XML tag for this element type. 262 */ 263 protected static transient String XML_TAG = "bool"; 264 265 /*** 266 * Data. 267 */ 268 protected java.lang.Boolean dataObj_; 269 }

This page was automatically generated by Maven