001: /* JFox, the OpenSource J2EE Application Server
002: *
003: * Copyright (C) 2002 huihoo.org
004: * Distributable under GNU LGPL license
005: * See the GNU Lesser General Public License for more details.
006: */
007:
008: package javax.management.openmbean;
009:
010: import java.io.Serializable;
011: import java.util.List;
012: import java.util.ArrayList;
013: import java.util.Iterator;
014: import java.util.Collections;
015:
016: import javax.management.openmbean.CompositeType;
017: import javax.management.openmbean.OpenDataException;
018: import javax.management.openmbean.OpenType;
019: import javax.management.openmbean.TabularData;
020:
021: /**
022: * The <code>TabularType</code> class is the <i> open type</i> class
023: * whose instances describe the types of {@link TabularData <code>TabularData</code>} values.
024: *
025: * @author <a href="mailto:young_yy@hotmail.org">Young Yang</a>
026: */
027:
028: public class TabularType extends OpenType implements Serializable {
029: /**
030: * @serial The composite type of rows
031: */
032: private CompositeType rowType;
033:
034: /**
035: * @serial The items used to index each row element, kept in the order the user gave
036: * This is an unmodifiable {@link ArrayList}
037: */
038: private List indexNames;
039:
040: private transient Integer myHashCode = null; // As this instance is immutable, these two values
041: private transient String myToString = null; // need only be calculated once.
042:
043: /* *** Constructor *** */
044:
045: /**
046: * Constructs a <code>TabularType</code> instance, checking for the validity of the given parameters.
047: * The validity constraints are described below for each parameter.
048: * <p>
049: * The Java class name of tabular data values this tabular type represents
050: * (ie the class name returned by the {@link OpenType#getClassName() getClassName} method)
051: * is set to the string value returned by <code>TabularData.class.getNameSpace()</code>.
052: * <p>
053: * @param typeName The name given to the tabular type this instance represents; cannot be a null or empty string.
054: * <br>
055: * @param description The human readable description of the tabular type this instance represents;
056: * cannot be a null or empty string.
057: * <br>
058: * @param rowType The type of the row elements of tabular data values described by this tabular type instance;
059: * cannot be null.
060: * <br>
061: * @param indexNames The names of the items the values of which are used to uniquely index each row element in the
062: * tabular data values described by this tabular type instance;
063: * cannot be null or empty. Each element should be an item name defined in <var>rowType</var>
064: * (no null or empty string allowed).
065: * It is important to note that the <b>order</b> of the item names in <var>indexNames</var>
066: * is used by the methods {@link TabularData#get(java.lang.Object[]) <code>get</code>} and
067: * {@link TabularData#remove(java.lang.Object[]) <code>remove</code>} of class
068: * <code>TabularData</code> to match their array of values parameter to items.
069: * <br>
070: * @throws IllegalArgumentException if <var>rowType</var> is null,
071: * or <var>indexNames</var> is a null or empty array,
072: * or an element in <var>indexNames</var> is a null or empty string,
073: * or <var>typeName</var> or <var>description</var> is a null or empty string.
074: * <br>
075: * @throws OpenDataException if an element's value of <var>indexNames</var>
076: * is not an item name defined in <var>rowType</var>.
077: */
078: public TabularType(String typeName, String description,
079: CompositeType rowType, String[] indexNames)
080: throws OpenDataException {
081:
082: // Check and initialize state defined by parent.
083: //
084: super (OpenType.TABULARDATA_CLASSNAME, typeName, description);
085:
086: // Check rowType is not null
087: //
088: if (rowType == null) {
089: throw new IllegalArgumentException(
090: "Argument rowType cannot be null.");
091: }
092:
093: // Check indexNames is neither null nor empty and does not contain any null element or empty string
094: //
095: checkForNullElement(indexNames, "indexNames");
096: checkForEmptyString(indexNames, "indexNames");
097:
098: // Check all indexNames values are valid item names for rowType
099: //
100: for (int i = 0; i < indexNames.length; i++) {
101: if (!rowType.containsKey(indexNames[i])) {
102: throw new OpenDataException(
103: "Argument's element value indexNames["
104: + i
105: + "]=\""
106: + indexNames[i]
107: + "\" is not a valid item name for rowType.");
108: }
109: }
110:
111: // initialize rowType
112: //
113: this .rowType = rowType;
114:
115: // initialize indexNames
116: // (copy content so that subsequent modif to the array referenced by the indexNames parameter have no impact)
117: //
118: ArrayList tmpList = new ArrayList(indexNames.length + 1);
119: for (int i = 0; i < indexNames.length; i++) {
120: tmpList.add(indexNames[i]);
121: }
122: this .indexNames = Collections.unmodifiableList(tmpList);
123: }
124:
125: /**
126: * Checks that Object[] arg is neither null nor empty (ie length==0)
127: * and that it does not contain any null element.
128: */
129: private static void checkForNullElement(Object[] arg, String argName) {
130: if ((arg == null) || (arg.length == 0)) {
131: throw new IllegalArgumentException("Argument " + argName
132: + "[] cannot be null or empty.");
133: }
134: for (int i = 0; i < arg.length; i++) {
135: if (arg[i] == null) {
136: throw new IllegalArgumentException(
137: "Argument's element " + argName + "[" + i
138: + "] cannot be null.");
139: }
140: }
141: }
142:
143: /**
144: * Checks that String[] does not contain any empty (or blank characters only) string.
145: */
146: private static void checkForEmptyString(String[] arg, String argName) {
147: for (int i = 0; i < arg.length; i++) {
148: if (arg[i].trim().equals("")) {
149: throw new IllegalArgumentException(
150: "Argument's element " + argName + "[" + i
151: + "] cannot be an empty string.");
152: }
153: }
154: }
155:
156: /* *** Tabular type specific information methods *** */
157:
158: /**
159: * Returns the type of the row elements of tabular data values
160: * described by this <code>TabularType</code> instance.
161: *
162: * @return the type of each row.
163: */
164: public CompositeType getRowType() {
165:
166: return rowType;
167: }
168:
169: /**
170: * <p>Returns, in the same order as was given to this instance's
171: * constructor, an unmodifiable List of the names of the items the
172: * values of which are used to uniquely index each row element of
173: * tabular data values described by this <code>TabularType</code>
174: * instance.</p>
175: *
176: * @return a List of String representing the names of the index
177: * items.
178: *
179: */
180: public List getIndexNames() {
181:
182: return indexNames;
183: }
184:
185: /**
186: * Tests whether <var>obj</var> is a value which could be described by this <code>TabularType</code> instance.
187: * <p>
188: * If <var>obj</var> is null or is not an instance of <code>javax.management.openmbean.TabularData</code>,
189: * <code>isValue</code> returns <code>false</code>.
190: * If <var>obj</var> is an instance of <code>javax.management.openmbean.TabularData</code>,
191: * its tabular type is tested for equality with this tabular type instance, and <code>isValue</code>
192: * returns <code>true</code> if and only if {@link #equals(java.lang.Object) <code>equals</code>}
193: * returns <code>true</code>.
194: * <br>
195: * @param obj the value whose open type is to be tested for equality with this <code>TabularType</code> instance.
196: *
197: * @return <code>true</code> if <var>obj</var> is a value for this tabular type, <code>false</code> otherwise.
198: */
199: public boolean isValue(Object obj) {
200:
201: // if obj is null, return false
202: //
203: if (obj == null) {
204: return false;
205: }
206:
207: // if obj is not a TabularData, return false
208: //
209: TabularData value;
210: try {
211: value = (TabularData) obj;
212: } catch (ClassCastException e) {
213: return false;
214: }
215:
216: // test value's TabularType for equality with this TabularType instance
217: //
218: return this .equals(value.getTabularType());
219: }
220:
221: /* *** Methods overriden from class Object *** */
222:
223: /**
224: * Compares the specified <code>obj</code> parameter with this <code>TabularType</code> instance for equality.
225: * <p>
226: * Two <code>TabularType</code> instances are equal if and only if all of the following statements are true:
227: * <ul>
228: * <li>their type names are equal</li>
229: * <li>their row types are equal</li>
230: * <li>they use the same index names, in the same order</li>
231: * </ul>
232: * <br>
233: * @param obj the object to be compared for equality with this <code>TabularType</code> instance;
234: * if <var>obj</var> is <code>null</code>, <code>equals</code> returns <code>false</code>.
235: *
236: * @return <code>true</code> if the specified object is equal to this <code>TabularType</code> instance.
237: */
238: public boolean equals(Object obj) {
239:
240: // if obj is null, return false
241: //
242: if (obj == null) {
243: return false;
244: }
245:
246: // if obj is not a TabularType, return false
247: //
248: TabularType other;
249: try {
250: other = (TabularType) obj;
251: } catch (ClassCastException e) {
252: return false;
253: }
254:
255: // Now, really test for equality between this TabularType instance and the other:
256: //
257:
258: // their names should be equal
259: if (!this .getTypeName().equals(other.getTypeName())) {
260: return false;
261: }
262:
263: // their row types should be equal
264: if (!this .rowType.equals(other.rowType)) {
265: return false;
266: }
267:
268: // their index names should be equal and in the same order (ensured by List.equals())
269: if (!this .indexNames.equals(other.indexNames)) {
270: return false;
271: }
272:
273: // All tests for equality were successfull
274: //
275: return true;
276: }
277:
278: /**
279: * Returns the hash code value for this <code>TabularType</code> instance.
280: * <p>
281: * The hash code of a <code>TabularType</code> instance is the sum of the hash codes
282: * of all elements of information used in <code>equals</code> comparisons
283: * (ie: name, row type, index names).
284: * This ensures that <code> t1.equals(t2) </code> implies that <code> t1.hashCode()==t2.hashCode() </code>
285: * for any two <code>TabularType</code> instances <code>t1</code> and <code>t2</code>,
286: * as required by the general contract of the method
287: * {@link <a href="http://java.sun.com/j2se/1.3/docs/api/java/lang/Object.html#hashCode()">
288: * <code>Object.hashCode</code> </a>}.
289: * <p>
290: * As <code>TabularType</code> instances are immutable, the hash code for this instance is calculated once,
291: * on the first call to <code>hashCode</code>, and then the same value is returned for subsequent calls.
292: *
293: * @return the hash code value for this <code>TabularType</code> instance
294: */
295: public int hashCode() {
296:
297: // Calculate the hash code value if it has not yet been done (ie 1st call to hashCode())
298: //
299: if (myHashCode == null) {
300: int value = 0;
301: value += this .getTypeName().hashCode();
302: value += this .rowType.hashCode();
303: for (Iterator k = indexNames.iterator(); k.hasNext();) {
304: value += k.next().hashCode();
305: }
306: myHashCode = new Integer(value);
307: }
308:
309: // return always the same hash code for this instance (immutable)
310: //
311: return myHashCode.intValue();
312: }
313:
314: /**
315: * Returns a string representation of this <code>TabularType</code> instance.
316: * <p>
317: * The string representation consists of the name of this class (ie <code>javax.management.openmbean.TabularType</code>),
318: * the type name for this instance, the row type string representation of this instance,
319: * and the index names of this instance.
320: * <p>
321: * As <code>TabularType</code> instances are immutable, the string representation for this instance is calculated once,
322: * on the first call to <code>toString</code>, and then the same value is returned for subsequent calls.
323: *
324: * @return a string representation of this <code>TabularType</code> instance
325: */
326: public String toString() {
327:
328: // Calculate the string representation if it has not yet been done (ie 1st call to toString())
329: //
330: if (myToString == null) {
331: StringBuffer result = new StringBuffer().append(
332: this .getClass().getName()).append("(name=").append(
333: getTypeName()).append(",rowType=").append(
334: rowType.toString()).append(",indexNames=(");
335: int i = 0;
336: Iterator k = indexNames.iterator();
337: while (k.hasNext()) {
338: if (i > 0)
339: result.append(",");
340: result.append(k.next().toString());
341: i++;
342: }
343: result.append("))");
344: myToString = result.toString();
345: }
346:
347: // return always the same string representation for this instance (immutable)
348: //
349: return myToString;
350: }
351:
352: }
|