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 import java.util.Date; 21 22 /*** 23 * Class to represent a <tt>Date</tt> field. 24 * @author Jawaid Hakim. 25 */ 26 public class RFldDatetime extends RFld 27 { 28 static final long serialVersionUID = -993143017976152419L; //Generated by jserial 29 30 /*** 31 * Default constructor. 32 */ 33 public RFldDatetime() 34 { 35 } 36 37 /*** 38 * Constructor. 39 * @param name Field name. 40 * @param fieldId Field id. Field ids must be either <tt>0</tt> 41 * to indicate that there is no id on the field, or greater. In addition, 42 * field ids must be unique within a messages - no two fields are allowed 43 * to have the same field id. 44 */ 45 public RFldDatetime(String name, int fieldId) 46 { 47 super(name, fieldId); 48 } 49 50 /*** 51 * Constructor. 52 * @param name Field name. 53 * @param fieldId Field id. Field ids must be either <tt>0</tt> 54 * to indicate that there is no id on the field, or greater. In addition, 55 * field ids must be unique within a messages - no two fields are allowed 56 * to have the same field id. 57 * @param desc Field description. 58 */ 59 public RFldDatetime(String name, int fieldId, String desc) 60 { 61 super(name, fieldId, desc); 62 } 63 64 /*** 65 * Get field type. 66 * @return Field type <tt>DATETIME</tt>. 67 * @see RFldType 68 */ 69 public final RFldType getType() 70 { 71 return RFldType.DATETIME; 72 } 73 74 /*** 75 * Check if another field is equal to this field. Equality is defined 76 * as the fields having the same value. 77 * @param anObject Another field. 78 * @return <tt>true</tt> if another field is equal to this field. 79 * Otherwise, returns <tt>false</tt>. 80 */ 81 public boolean equals(Object anObject) 82 { 83 if (this == anObject) 84 { 85 return true; 86 } 87 else if (! (anObject instanceof RFldDatetime)) 88 { 89 return false; 90 } 91 92 RFldDatetime another = (RFldDatetime)anObject; 93 if (valSet_ != another.isValSet()) 94 return false; 95 else 96 return (! valSet_ || dataObj_.equals(another.getValue())); 97 } 98 99 100 /*** 101 * Returns the hash code value for the field. 102 * @return A hash code value for the field, delegate to the 103 * java.util.Date hashCode method. 104 */ 105 public int hashCode() 106 { 107 return (valSet_) ? dataObj_.hashCode() : 1231; 108 } 109 110 /*** 111 * Add a constraint. 112 * @param cons Constraint. 113 */ 114 public void addConstraint(RFldConstraintDatetime cons) throws FieldValidationException 115 { 116 super.addConstraint(cons); 117 } 118 119 /*** 120 * Reset the field value. 121 * @see #isValSet() 122 */ 123 public void reset() throws FieldValidationException 124 { 125 if (isLocked()) 126 throw new FieldValidationException("Field " + getName() + " is locked"); 127 128 valSet_ = false; 129 dataObj_ = null; 130 } 131 132 /*** 133 * Set data from a string. The specified DateFormat is used to parse 134 * the string. 135 * @param dateFmt Date format to use for parsing the data. 136 * @param newData New data. 137 */ 138 public RFld set(java.text.DateFormat dateFmt, String newData) throws FieldValidationException 139 { 140 try 141 { 142 return set(dateFmt.parse(newData)); 143 } 144 catch (java.text.ParseException ex) 145 { 146 throw new FieldValidationException(ex.getMessage()); 147 } 148 } 149 150 /*** 151 * Set data. 152 * @param newData New data. 153 */ 154 public RFld set(Object newData) throws FieldValidationException 155 { 156 try 157 { 158 String s = ((Date)newData).toString(); 159 return set((Date)newData); 160 } 161 catch (ClassCastException e) 162 { 163 return set(newData.toString()); 164 } 165 } 166 167 /*** 168 * Set data. 169 * @param newData New data. 170 */ 171 public RFld set(long newData) throws FieldValidationException 172 { 173 return set(new Date(newData)); 174 } 175 176 /*** 177 * Set data. 178 * @param newData New data. 179 */ 180 public RFld set(java.util.Date newData) throws FieldValidationException 181 { 182 validate(newData); 183 184 dataObj_ = newData; 185 valSet_ = true; 186 187 return this; 188 } 189 190 /*** 191 * Set the field value from a JDOM element. 192 * @param elem Field value as a JDOM element. 193 */ 194 public final RFld set(org.jdom.Element elem) throws FieldValidationException 195 { 196 try 197 { 198 String sVal = elem.getAttributeValue(XML_ATTR_VALUE); 199 return set(RDateFormat.getInstance().parse(sVal)); 200 } 201 catch (Exception ex) 202 { 203 throw new FieldValidationException(ex); 204 } 205 } 206 207 /*** 208 * Validate against constraints. A field is valid if either it's value is set 209 * and satisfies all constraints, or the the field is optional. 210 */ 211 public void validate() throws FieldValidationException 212 { 213 // Only need to check that non-optional fields have been set. If a 214 // field has been set then it must be valid since validation is done 215 // with each set. 216 if (! valSet_ && ! optional_) 217 throw new FieldValidationException("Field not set: " + getName()); 218 } 219 220 /*** 221 * Check if a new value will satifsy constraints. 222 * @param newData New value. 223 */ 224 public void validate(java.util.Date newData) throws FieldValidationException 225 { 226 if (locked_) 227 throw new FieldValidationException("Cannot modify locked field: " + getName()); 228 229 if (newData == null) 230 throw new FieldValidationException("New value is NULL for field: " + getName());; 231 232 for (int i = getConstraintCount() - 1; i >= 0; --i) 233 ((RFldConstraintDatetime)getConstraint(i)).validate(this, newData); 234 } 235 236 /*** 237 * Get data. 238 * @return Field value. Returns <tt>false</tt> if the field value is not set. 239 */ 240 public Date getValue() 241 { 242 return dataObj_; 243 } 244 245 /*** 246 * Get the field value as an object. 247 * @return Field value as an object. Reference to data is passed out so be 248 * very careful about modifying the data. Returns <tt>null</tt> if the 249 * field value is not set. 250 */ 251 public Object getValueAsObject() 252 { 253 return dataObj_; 254 } 255 256 /*** 257 * Get the field value as a string. 258 * @return Field value as a string. 259 * @throws NullPointerException if the field value has not been set. 260 */ 261 public String getValueAsString() 262 { 263 return RDateFormat.getInstance().format(dataObj_); 264 } 265 266 /*** 267 * Get the field value as a <tt>java.util.Hashtable</tt>. Not supported - throws 268 * an exception. 269 * @return Field value as java.util.Hashtable. Returns <tt>null</tt> if the field 270 * value is not set. 271 */ 272 public java.util.Hashtable getValueAsHashtable() throws FieldValidationException 273 { 274 throw new FieldValidationException("Not supported"); 275 } 276 277 /*** 278 * Write the field as XML to target writer. 279 * @param writer Output target. 280 * @param indent Indentation. 281 * @param newLines Newlines are inserted after each element if <tt>true</tt>. 282 * @param expandEmptyElements Empty elements - elements with no content - are expanded if <tt>true</tt>. 283 */ 284 public void marshal(java.io.Writer writer, int indentLevel, String indent, boolean newLines, boolean expandEmptyElements) throws ConverterException 285 { 286 if (! isValSet()) 287 throw new ConverterException("Field not set: " + getName()); 288 289 marshal(writer, getTag(), getName(), RDateFormat.getInstance().format(getValue()), getType().toString(), indentLevel, indent, newLines, expandEmptyElements); 290 } 291 292 /*** 293 * Get the XML tag for this field type. 294 * @return XML tag for this field type. 295 */ 296 public final String getTag() 297 { 298 return XML_TAG; 299 } 300 301 /*** 302 * Set the XML tag for this field type. 303 * @param tag New XML tag for this field type. 304 */ 305 public static void setTag(String tag) 306 { 307 XML_TAG = tag; 308 } 309 310 /*** 311 * XML tag for this element type. 312 */ 313 protected static transient String XML_TAG = "date"; 314 315 /*** 316 * Data. 317 */ 318 protected java.util.Date dataObj_; 319 }

This page was automatically generated by Maven