KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javax > management > openmbean > TabularType


1 /*
2  * @(#)TabularType.java 3.22 03/12/19
3  *
4  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7
8
9 package javax.management.openmbean;
10
11
12 // java import
13
//
14
import java.io.Serializable JavaDoc;
15 import java.util.List JavaDoc;
16 import java.util.Iterator JavaDoc;
17 import java.util.Arrays JavaDoc;
18 import java.util.ArrayList JavaDoc;
19 import java.util.Collections JavaDoc;
20
21 // jmx import
22
//
23

24
25 /**
26  * The <code>TabularType</code> class is the <i> open type</i> class
27  * whose instances describe the types of {@link TabularData <code>TabularData</code>} values.
28  *
29  * @version 3.22 03/12/19
30  * @author Sun Microsystems, Inc.
31  *
32  * @since 1.5
33  * @since.unbundled JMX 1.1
34  */

35 public class TabularType
36     extends OpenType JavaDoc
37     implements Serializable JavaDoc {
38     
39     
40     /* Serial version */
41     static final long serialVersionUID = 6554071860220659261L;
42
43
44     /**
45      * @serial The composite type of rows
46      */

47     private CompositeType JavaDoc rowType;
48     
49     /**
50      * @serial The items used to index each row element, kept in the order the user gave
51      * This is an unmodifiable {@link ArrayList}
52      */

53     private List JavaDoc indexNames;
54
55
56     private transient Integer JavaDoc myHashCode = null; // As this instance is immutable, these two values
57
private transient String JavaDoc myToString = null; // need only be calculated once.
58

59
60     /* *** Constructor *** */
61
62     /**
63      * Constructs a <code>TabularType</code> instance, checking for the validity of the given parameters.
64      * The validity constraints are described below for each parameter.
65      * <p>
66      * The Java class name of tabular data values this tabular type represents
67      * (ie the class name returned by the {@link OpenType#getClassName() getClassName} method)
68      * is set to the string value returned by <code>TabularData.class.getName()</code>.
69      * <p>
70      * @param typeName The name given to the tabular type this instance represents; cannot be a null or empty string.
71      * <br>&nbsp;
72      * @param description The human readable description of the tabular type this instance represents;
73      * cannot be a null or empty string.
74      * <br>&nbsp;
75      * @param rowType The type of the row elements of tabular data values described by this tabular type instance;
76      * cannot be null.
77      * <br>&nbsp;
78      * @param indexNames The names of the items the values of which are used to uniquely index each row element in the
79      * tabular data values described by this tabular type instance;
80      * cannot be null or empty. Each element should be an item name defined in <var>rowType</var>
81      * (no null or empty string allowed).
82      * It is important to note that the <b>order</b> of the item names in <var>indexNames</var>
83      * is used by the methods {@link TabularData#get(java.lang.Object[]) <code>get</code>} and
84      * {@link TabularData#remove(java.lang.Object[]) <code>remove</code>} of class
85      * <code>TabularData</code> to match their array of values parameter to items.
86      * <br>&nbsp;
87      * @throws IllegalArgumentException if <var>rowType</var> is null,
88      * or <var>indexNames</var> is a null or empty array,
89      * or an element in <var>indexNames</var> is a null or empty string,
90      * or <var>typeName</var> or <var>description</var> is a null or empty string.
91      * <br>&nbsp;
92      * @throws OpenDataException if an element's value of <var>indexNames</var>
93      * is not an item name defined in <var>rowType</var>.
94      */

95     public TabularType(String JavaDoc typeName,
96                String JavaDoc description,
97                CompositeType JavaDoc rowType,
98                String JavaDoc[] indexNames) throws OpenDataException JavaDoc {
99     
100     // Check and initialize state defined by parent.
101
//
102
super(TabularData JavaDoc.class.getName(), typeName, description);
103     
104     // Check rowType is not null
105
//
106
if (rowType == null) {
107         throw new IllegalArgumentException JavaDoc("Argument rowType cannot be null.");
108     }
109
110     // Check indexNames is neither null nor empty and does not contain any null element or empty string
111
//
112
checkForNullElement(indexNames, "indexNames");
113     checkForEmptyString(indexNames, "indexNames");
114
115     // Check all indexNames values are valid item names for rowType
116
//
117
for (int i=0; i<indexNames.length; i++) {
118         if ( ! rowType.containsKey(indexNames[i]) ) {
119         throw new OpenDataException JavaDoc("Argument's element value indexNames["+ i +"]=\""+ indexNames[i] +
120                         "\" is not a valid item name for rowType.");
121         }
122     }
123     
124     // initialize rowType
125
//
126
this.rowType = rowType;
127
128     // initialize indexNames
129
// (copy content so that subsequent modif to the array referenced by the indexNames parameter have no impact)
130
//
131
ArrayList JavaDoc tmpList = new ArrayList JavaDoc(indexNames.length + 1);
132     for (int i=0; i<indexNames.length; i++) {
133         tmpList.add(indexNames[i]);
134     }
135     this.indexNames = Collections.unmodifiableList(tmpList);
136     }
137     
138     /**
139      * Checks that Object[] arg is neither null nor empty (ie length==0)
140      * and that it does not contain any null element.
141      */

142     private static void checkForNullElement(Object JavaDoc[] arg, String JavaDoc argName) {
143     if ( (arg == null) || (arg.length == 0) ) {
144         throw new IllegalArgumentException JavaDoc("Argument "+ argName +"[] cannot be null or empty.");
145     }
146     for (int i=0; i<arg.length; i++) {
147         if (arg[i] == null) {
148         throw new IllegalArgumentException JavaDoc("Argument's element "+ argName +"["+ i +"] cannot be null.");
149         }
150     }
151     }
152
153     /**
154      * Checks that String[] does not contain any empty (or blank characters only) string.
155      */

156     private static void checkForEmptyString(String JavaDoc[] arg, String JavaDoc argName) {
157     for (int i=0; i<arg.length; i++) {
158         if (arg[i].trim().equals("")) {
159         throw new IllegalArgumentException JavaDoc("Argument's element "+ argName +"["+ i +"] cannot be an empty string.");
160         }
161     }
162     }
163
164
165     /* *** Tabular type specific information methods *** */
166
167     /**
168      * Returns the type of the row elements of tabular data values
169      * described by this <code>TabularType</code> instance.
170      *
171      * @return the type of each row.
172      */

173     public CompositeType JavaDoc getRowType() {
174
175     return rowType;
176     }
177
178     /**
179      * <p>Returns, in the same order as was given to this instance's
180      * constructor, an unmodifiable List of the names of the items the
181      * values of which are used to uniquely index each row element of
182      * tabular data values described by this <code>TabularType</code>
183      * instance.</p>
184      *
185      * @return a List of String representing the names of the index
186      * items.
187      *
188      */

189     public List JavaDoc getIndexNames() {
190
191     return indexNames;
192     }
193
194     /**
195      * Tests whether <var>obj</var> is a value which could be described by this <code>TabularType</code> instance.
196      * <p>
197      * If <var>obj</var> is null or is not an instance of <code>javax.management.openmbean.TabularData</code>,
198      * <code>isValue</code> returns <code>false</code>.
199      * If <var>obj</var> is an instance of <code>javax.management.openmbean.TabularData</code>,
200      * its tabular type is tested for equality with this tabular type instance, and <code>isValue</code>
201      * returns <code>true</code> if and only if {@link #equals(java.lang.Object) <code>equals</code>}
202      * returns <code>true</code>.
203      * <br>&nbsp;
204      * @param obj the value whose open type is to be tested for equality with this <code>TabularType</code> instance.
205      *
206      * @return <code>true</code> if <var>obj</var> is a value for this tabular type, <code>false</code> otherwise.
207      */

208     public boolean isValue(Object JavaDoc obj) {
209
210     // if obj is null, return false
211
//
212
if (obj == null) {
213         return false;
214     }
215
216     // if obj is not a TabularData, return false
217
//
218
TabularData JavaDoc value;
219     try {
220         value = (TabularData JavaDoc) obj;
221     } catch (ClassCastException JavaDoc e) {
222         return false;
223     }
224
225     // test value's TabularType for equality with this TabularType instance
226
//
227
return this.equals(value.getTabularType());
228     }
229
230
231     /* *** Methods overriden from class Object *** */
232
233     /**
234      * Compares the specified <code>obj</code> parameter with this <code>TabularType</code> instance for equality.
235      * <p>
236      * Two <code>TabularType</code> instances are equal if and only if all of the following statements are true:
237      * <ul>
238      * <li>their type names are equal</li>
239      * <li>their row types are equal</li>
240      * <li>they use the same index names, in the same order</li>
241      * </ul>
242      * <br>&nbsp;
243      * @param obj the object to be compared for equality with this <code>TabularType</code> instance;
244      * if <var>obj</var> is <code>null</code>, <code>equals</code> returns <code>false</code>.
245      *
246      * @return <code>true</code> if the specified object is equal to this <code>TabularType</code> instance.
247      */

248     public boolean equals(Object JavaDoc obj) {
249
250     // if obj is null, return false
251
//
252
if (obj == null) {
253         return false;
254     }
255
256     // if obj is not a TabularType, return false
257
//
258
TabularType JavaDoc other;
259     try {
260         other = (TabularType JavaDoc) obj;
261     } catch (ClassCastException JavaDoc e) {
262         return false;
263     }
264
265     // Now, really test for equality between this TabularType instance and the other:
266
//
267

268     // their names should be equal
269
if ( ! this.getTypeName().equals(other.getTypeName()) ) {
270         return false;
271     }
272
273     // their row types should be equal
274
if ( ! this.rowType.equals(other.rowType) ) {
275         return false;
276     }
277
278     // their index names should be equal and in the same order (ensured by List.equals())
279
if ( ! this.indexNames.equals(other.indexNames) ) {
280         return false;
281     }
282
283     // All tests for equality were successfull
284
//
285
return true;
286     }
287
288     /**
289      * Returns the hash code value for this <code>TabularType</code> instance.
290      * <p>
291      * The hash code of a <code>TabularType</code> instance is the sum of the hash codes
292      * of all elements of information used in <code>equals</code> comparisons
293      * (ie: name, row type, index names).
294      * This ensures that <code> t1.equals(t2) </code> implies that <code> t1.hashCode()==t2.hashCode() </code>
295      * for any two <code>TabularType</code> instances <code>t1</code> and <code>t2</code>,
296      * as required by the general contract of the method
297      * {@link Object#hashCode() Object.hashCode()}.
298      * <p>
299      * As <code>TabularType</code> instances are immutable, the hash code for this instance is calculated once,
300      * on the first call to <code>hashCode</code>, and then the same value is returned for subsequent calls.
301      *
302      * @return the hash code value for this <code>TabularType</code> instance
303      */

304     public int hashCode() {
305
306     // Calculate the hash code value if it has not yet been done (ie 1st call to hashCode())
307
//
308
if (myHashCode == null) {
309         int value = 0;
310         value += this.getTypeName().hashCode();
311         value += this.rowType.hashCode();
312         for (Iterator JavaDoc k = indexNames.iterator(); k.hasNext(); ) {
313         value += k.next().hashCode();
314         }
315         myHashCode = new Integer JavaDoc(value);
316     }
317     
318     // return always the same hash code for this instance (immutable)
319
//
320
return myHashCode.intValue();
321     }
322
323     /**
324      * Returns a string representation of this <code>TabularType</code> instance.
325      * <p>
326      * The string representation consists of the name of this class (ie <code>javax.management.openmbean.TabularType</code>),
327      * the type name for this instance, the row type string representation of this instance,
328      * and the index names of this instance.
329      * <p>
330      * As <code>TabularType</code> instances are immutable, the string representation for this instance is calculated once,
331      * on the first call to <code>toString</code>, and then the same value is returned for subsequent calls.
332      *
333      * @return a string representation of this <code>TabularType</code> instance
334      */

335     public String JavaDoc toString() {
336
337     // Calculate the string representation if it has not yet been done (ie 1st call to toString())
338
//
339
if (myToString == null) {
340         StringBuffer JavaDoc result = new StringBuffer JavaDoc()
341         .append(this.getClass().getName())
342         .append("(name=")
343         .append(getTypeName())
344         .append(",rowType=")
345         .append(rowType.toString())
346         .append(",indexNames=(");
347         int i=0;
348         Iterator JavaDoc k = indexNames.iterator();
349         while( k.hasNext() ) {
350         if (i > 0) result.append(",");
351         result.append(k.next().toString());
352         i++;
353         }
354         result.append("))");
355         myToString = result.toString();
356     }
357
358     // return always the same string representation for this instance (immutable)
359
//
360
return myToString;
361     }
362
363 }
364
Popular Tags