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 com.tibco.tibrv.TibrvIPAddr; 21 22 /*** 23 * Class to represent a <tt>TibrvIPAddr</tt> field. 24 * @see com.tibco.tibrv.TibrvIPAddr 25 * @author Jawaid Hakim. 26 */ 27 public class RFldTibrvIPAddr extends RFld 28 { 29 /*** 30 * Default constructor. 31 */ 32 public RFldTibrvIPAddr() 33 { 34 } 35 36 /*** 37 * Constructor. 38 * @param name Field name. 39 * @param fieldId Field id. Field ids must be either <tt>0</tt> 40 * to indicate that there is no id on the field, or greater. In addition, 41 * field ids must be unique within a messages - no two fields are allowed 42 * to have the same field id. 43 */ 44 public RFldTibrvIPAddr(String name, int fieldId) 45 { 46 super(name, fieldId); 47 } 48 49 /*** 50 * Constructor. 51 * @param name Field name. 52 * @param fieldId Field id. Field ids must be either <tt>0</tt> 53 * to indicate that there is no id on the field, or greater. In addition, 54 * field ids must be unique within a messages - no two fields are allowed 55 * to have the same field id. 56 * @param desc Field description. 57 */ 58 public RFldTibrvIPAddr(String name, int fieldId, String desc) 59 { 60 super(name, fieldId, desc); 61 } 62 63 /*** 64 * Constructor. 65 * @param name Field name. 66 * @param fieldId Field id. Field ids must be either <tt>0</tt> 67 * to indicate that there is no id on the field, or greater. In addition, 68 * field ids must be unique within a messages - no two fields are allowed 69 * to have the same field id. 70 * @param desc Field description. 71 * @param value Create an IP address from an array of 4 bytes. For aaa.bbb.ccc.ddd, let bytes[0] 72 * be the high byte aaa, and bytes[3] be the low byte ddd. 73 */ 74 public RFldTibrvIPAddr(String name, int fieldId, String desc, byte[] value) throws FieldValidationException 75 { 76 super(name, fieldId, desc); 77 set(new TibrvIPAddr(value)); 78 } 79 80 /*** 81 * Constructor. 82 * @param name Field name. 83 * @param fieldId Field id. Field ids must be either <tt>0</tt> 84 * to indicate that there is no id on the field, or greater. In addition, 85 * field ids must be unique within a messages - no two fields are allowed 86 * to have the same field id. 87 * @param desc Field description. 88 * @param value Value. No copy is made - be careful when modifying the 89 * data. 90 */ 91 public RFldTibrvIPAddr(String name, int fieldId, String desc, java.net.InetAddress value) throws FieldValidationException 92 { 93 super(name, fieldId, desc); 94 set(new TibrvIPAddr(value)); 95 } 96 97 /*** 98 * Constructor. 99 * @param name Field name. 100 * @param fieldId Field id. Field ids must be either <tt>0</tt> 101 * to indicate that there is no id on the field, or greater. In addition, 102 * field ids must be unique within a messages - no two fields are allowed 103 * to have the same field id. 104 * @param desc Field description. 105 * @param value Value. No copy is made - be careful when modifying the 106 * data. 107 */ 108 public RFldTibrvIPAddr(String name, int fieldId, String desc, int value) throws FieldValidationException 109 { 110 super(name, fieldId, desc); 111 set(new TibrvIPAddr(value)); 112 } 113 114 /*** 115 * Constructor. Create an IP address from these 4 bytes. For aaa.bbb.ccc.ddd, let b1 be 116 * the high byte aaa, and b4 be the low byte ddd. 117 * @param name Field name. 118 * @param fieldId Field id. Field ids must be either <tt>0</tt> 119 * to indicate that there is no id on the field, or greater. In addition, 120 * field ids must be unique within a messages - no two fields are allowed 121 * to have the same field id. 122 * @param desc Field description. 123 * @param b1 High byte. 124 * @param b2 Byte 2. 125 * @param b3 Byte 3. 126 * @param b4 Low byte. 127 */ 128 public RFldTibrvIPAddr(String name, int fieldId, String desc, byte b1, byte b2, byte b3, byte b4) throws FieldValidationException 129 { 130 super(name, fieldId, desc); 131 set(new TibrvIPAddr(b1, b2, b3, b4)); 132 } 133 134 /*** 135 * Get field type. 136 * @return Field type <tt>IPADDR32</tt>. 137 * @see RFldType 138 */ 139 public final RFldType getType() 140 { 141 return RFldType.IPADDR32; 142 } 143 144 /*** 145 * Check if another field is equal to this field. Equality is defined 146 * as the fields having the same value. 147 * @param anObject Another field. 148 * @return <tt>true</tt> if another field is equal to this field. 149 * Otherwise, returns <tt>false</tt>. 150 */ 151 public boolean equals(Object anObject) 152 { 153 if (this == anObject) 154 { 155 return true; 156 } 157 else if (!(anObject instanceof RFldTibrvIPAddr)) 158 { 159 return false; 160 } 161 162 RFldTibrvIPAddr another = (RFldTibrvIPAddr)anObject; 163 if (valSet_ != another.isValSet()) 164 return false; 165 else 166 return (! valSet_ || dataObj_.equals(another.getValue())); 167 } 168 169 /*** 170 * Returns the hash code value for the field. 171 * 172 * @return a hash code value for the field, delegate to the 173 * value of int TibrvIPAddr.getAddr() method. 174 */ 175 public int hashCode() 176 { 177 return (valSet_) ? dataObj_.hashCode() : 1231; 178 } 179 180 /*** 181 * Reset the field value. 182 * @see #isValSet() 183 */ 184 public void reset() throws FieldValidationException 185 { 186 if (isLocked()) 187 throw new FieldValidationException("Field " + getName() + " is locked"); 188 189 valSet_ = false; 190 dataObj_ = null; 191 } 192 193 /*** 194 * Set data. 195 * @param newData New data. 196 */ 197 public RFld set(Object newData) throws FieldValidationException 198 { 199 try 200 { 201 return set((TibrvIPAddr)newData); 202 } 203 catch (ClassCastException e) 204 { 205 return set(newData.toString()); 206 } 207 } 208 209 /*** 210 * Set data. 211 * @param newData New data. 212 */ 213 public RFld set(byte[] newData) throws FieldValidationException 214 { 215 return set(new TibrvIPAddr(newData)); 216 } 217 218 /*** 219 * Set the field value from a JDOM element. 220 * @param elem Field value as a JDOM element. 221 */ 222 public final RFld set(org.jdom.Element elem) throws FieldValidationException 223 { 224 return set(elem.getAttributeValue(XML_ATTR_VALUE)); 225 } 226 227 /*** 228 * Set data. 229 * @param newData New data. 230 */ 231 public RFld set(TibrvIPAddr newData) throws FieldValidationException 232 { 233 validate(newData); 234 235 dataObj_ = newData; 236 valSet_ = true; 237 238 return this; 239 } 240 241 /*** 242 * Validate against constraints. A field is valid if either it's value is set 243 * and satisfies all constraints, or the the field is optional. 244 */ 245 public void validate() throws FieldValidationException 246 { 247 // Only need to check that non-optional fields have been set. If a 248 // field has been set then it must be valid since validation is done 249 // with each set. 250 if (! valSet_ && ! optional_) 251 throw new FieldValidationException("Field not set: " + getName()); 252 } 253 254 /*** 255 * Check if a new value will satifsy constraints. 256 * @param newData New value. 257 */ 258 public void validate(TibrvIPAddr newData) throws FieldValidationException 259 { 260 if (locked_) 261 throw new FieldValidationException("Cannot modify locked field: " + getName()); 262 263 if (newData == null) 264 throw new FieldValidationException("New value is NULL for field: " + getName());; 265 } 266 267 /*** 268 * Get data. 269 * @return data Data. Returns <tt>null</tt> if the field value is not set. 270 */ 271 public TibrvIPAddr getValue() 272 { 273 return dataObj_; 274 } 275 276 /*** 277 * Get the field value as an <tt>Object</tt>. 278 * @return Field value as an <tt>Object</tt>. Returns <tt>null</tt> if 279 * the field value is not set. 280 */ 281 public Object getValueAsObject() 282 { 283 return dataObj_; 284 } 285 286 /*** 287 * Get the <tt>int</tt> field value as a <tt>String</tt>. 288 * @return Field value as a <tt>String</tt>. Returns <tt>null</tt> 289 * if the field value is not set. 290 * @see #getValueAsInt() 291 */ 292 public String getValueAsString() 293 { 294 if (valSet_) 295 return String.valueOf(dataObj_.getAddr()); 296 else 297 return null; 298 } 299 300 /*** 301 * Get the field value as an <tt>int</tt>. 302 * @return Field value as an <tt>int</tt>. Returns <tt>-1</tt> 303 * if the field value is not set. 304 */ 305 public int getValueAsInt() 306 { 307 if (valSet_) 308 return dataObj_.getAddr(); 309 else 310 return -1; 311 } 312 313 /*** 314 * Get the field value as a <tt>java.util.Hashtable</tt>. Not supported - throws 315 * an exception. 316 * @return Field value as java.util.Hashtable. Returns <tt>null</tt> if the field 317 * value is not set. 318 */ 319 public java.util.Hashtable getValueAsHashtable() throws FieldValidationException 320 { 321 throw new FieldValidationException("Not supported"); 322 } 323 324 /*** 325 * Get the XML tag for this field type. 326 * @return XML tag for this field type. 327 */ 328 public final String getTag() 329 { 330 return XML_TAG; 331 } 332 333 /*** 334 * Set the XML tag for this field type. 335 * @param tag New XML tag for this field type. 336 */ 337 public static void setTag(String tag) 338 { 339 XML_TAG = tag; 340 } 341 342 /*** 343 * XML tag for this element type. 344 */ 345 protected static transient String XML_TAG = "tibrvipaddr"; 346 347 /*** 348 * Data. 349 */ 350 protected TibrvIPAddr dataObj_; 351 }

This page was automatically generated by Maven