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.TibrvIPPort;
21
22 /***
23 * Class to represent a <tt>TibrvIPPort</tt> field.
24 * @see com.tibco.tibrv.TibrvIPPort
25 * @author Jawaid Hakim.
26 */
27 public class RFldTibrvIPPort extends RFld
28 {
29 /***
30 * Default constructor.
31 */
32 public RFldTibrvIPPort()
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 RFldTibrvIPPort(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 RFldTibrvIPPort(String name, int fieldId, String desc)
59 {
60 super(name, fieldId, desc);
61 }
62
63 /***
64 * Get field type.
65 * @return Field type <tt>IPPORT16</tt>.
66 * @see RFldType
67 */
68 public final RFldType getType()
69 {
70 return RFldType.IPPORT16;
71 }
72
73 /***
74 * Check if another field is equal to this field. Equality is defined
75 * as the fields having the same value.
76 * @param anObject Another field.
77 * @return <tt>true</tt> if another field is equal to this field.
78 * Otherwise, returns <tt>false</tt>.
79 */
80 public boolean equals(Object anObject)
81 {
82 if (this == anObject)
83 {
84 return true;
85 }
86 else if (!(anObject instanceof RFldTibrvIPPort))
87 {
88 return false;
89 }
90 RFldTibrvIPPort another = (RFldTibrvIPPort)anObject;
91 if (valSet_ != another.isValSet())
92 return false;
93 else
94 return (! valSet_ || dataObj_.equals(another.getValue()));
95 }
96
97 /***
98 * Returns the hash code value for the field.
99 *
100 * @return a hash code value for the field, delegate to the
101 * value of int TibrvIPPort.getPort() method.
102 */
103 public int hashCode()
104 {
105 return (valSet_) ? dataObj_.hashCode() : 1231;
106 }
107
108 /***
109 * Reset the field value.
110 * @see #isValSet()
111 */
112 public void reset() throws FieldValidationException
113 {
114 if (isLocked())
115 throw new FieldValidationException("Field " + getName() + " is locked");
116
117 valSet_ = false;
118 dataObj_ = null;
119 }
120
121 /***
122 * Set data.
123 * @param newData New data.
124 */
125 public RFld set(Object newData) throws FieldValidationException
126 {
127 try
128 {
129 return set((TibrvIPPort)newData);
130 }
131 catch (ClassCastException e)
132 {
133 return set(newData.toString());
134 }
135 }
136
137 /***
138 * Set data. The date must have exactly two bytes for the high and the low
139 * bytes of the IP port.
140 * @param newData New data.
141 */
142 public RFld set(byte[] newData) throws FieldValidationException
143 {
144 if (newData.length != 2)
145 throw new FieldValidationException("Expected high and low bytes: " + getName());
146 else
147 return set(new TibrvIPPort(newData[0], newData[1]));
148 }
149
150 /***
151 * Set the field value from a JDOM element.
152 * @param elem Field value as a JDOM element.
153 */
154 public final RFld set(org.jdom.Element elem) throws FieldValidationException
155 {
156 return set(elem.getAttributeValue(XML_ATTR_VALUE));
157 }
158
159 /***
160 * Set data.
161 * @param newData New data.
162 */
163 public RFld set(TibrvIPPort newData) throws FieldValidationException
164 {
165 validate(newData);
166
167 dataObj_ = newData;
168 valSet_ = true;
169
170 return this;
171 }
172
173 /***
174 * Validate against constraints. A field is valid if either it's value is set
175 * and satisfies all constraints, or the the field is optional.
176 */
177 public void validate() throws FieldValidationException
178 {
179 // Only need to check that non-optional fields have been set. If a
180 // field has been set then it must be valid since validation is done
181 // with each set.
182 if (! valSet_ && ! optional_)
183 throw new FieldValidationException("Field not set: " + getName());
184 }
185
186 /***
187 * Check if a new value will satifsy constraints.
188 * @param newData New value.
189 */
190 public void validate(TibrvIPPort newData) throws FieldValidationException
191 {
192 if (locked_)
193 throw new FieldValidationException("Cannot modify locked field: " + getName());
194
195 if (newData == null)
196 throw new FieldValidationException("New value is NULL for field: " + getName());;
197 }
198
199 /***
200 * Get data.
201 * @return data Data. Returns <tt>null</tt> if the field value is not set.
202 */
203 public TibrvIPPort getValue()
204 {
205 return dataObj_;
206 }
207
208 /***
209 * Get the field value as an <tt>Object</tt>.
210 * @return Field value as an <tt>Object</tt>. Returns <tt>null</tt> if
211 * the field value is not set.
212 */
213 public Object getValueAsObject()
214 {
215 return dataObj_;
216 }
217
218 /***
219 * Get the <tt>int</tt> field value as a <tt>String</tt>.
220 * @return Field value as a <tt>String</tt>. Returns <tt>null</tt>
221 * if the field value is not set.
222 * @see #getValueAsInt()
223 */
224 public String getValueAsString()
225 {
226 if (valSet_)
227 return String.valueOf(dataObj_.getPort());
228 else
229 return null;
230 }
231
232 /***
233 * Get the field value as an <tt>int</tt>.
234 * @return Field value as an <tt>int</tt>. Returns <tt>-l</tt> if
235 * the field value is not set.
236 */
237 public int getValueAsInt()
238 {
239 if (valSet_)
240 return dataObj_.getPort();
241 else
242 return -1;
243 }
244
245 /***
246 * Get the field value as a <tt>java.util.Hashtable</tt>. Not supported - throws
247 * an exception.
248 * @return Field value as java.util.Hashtable. Returns <tt>null</tt> if the field
249 * value is not set.
250 */
251 public java.util.Hashtable getValueAsHashtable() throws FieldValidationException
252 {
253 throw new FieldValidationException("Not supported");
254 }
255
256 /***
257 * Get the XML tag for this field type.
258 * @return XML tag for this field type.
259 */
260 public final String getTag()
261 {
262 return XML_TAG;
263 }
264
265 /***
266 * Set the XML tag for this field type.
267 * @param tag New XML tag for this field type.
268 */
269 public static void setTag(String tag)
270 {
271 XML_TAG = tag;
272 }
273
274 /***
275 * XML tag for this element type.
276 */
277 protected static transient String XML_TAG = "tibrvipport";
278
279 /***
280 * Data.
281 */
282 protected TibrvIPPort dataObj_;
283 }
This page was automatically generated by Maven