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. package javax.management.openmbean;
  8. // java import
  9. //
  10. import java.io.Serializable;
  11. import java.util.List;
  12. import java.util.Iterator;
  13. import java.util.Arrays;
  14. import java.util.ArrayList;
  15. import java.util.Collections;
  16. // jmx import
  17. //
  18. /**
  19. * The <code>TabularType</code> class is the <i> open type</i> class
  20. * whose instances describe the types of {@link TabularData <code>TabularData</code>} values.
  21. *
  22. * @version 3.22 03/12/19
  23. * @author Sun Microsystems, Inc.
  24. *
  25. * @since 1.5
  26. * @since.unbundled JMX 1.1
  27. */
  28. public class TabularType
  29. extends OpenType
  30. implements Serializable {
  31. /* Serial version */
  32. static final long serialVersionUID = 6554071860220659261L;
  33. /**
  34. * @serial The composite type of rows
  35. */
  36. private CompositeType rowType;
  37. /**
  38. * @serial The items used to index each row element, kept in the order the user gave
  39. * This is an unmodifiable {@link ArrayList}
  40. */
  41. private List indexNames;
  42. private transient Integer myHashCode = null; // As this instance is immutable, these two values
  43. private transient String myToString = null; // need only be calculated once.
  44. /* *** Constructor *** */
  45. /**
  46. * Constructs a <code>TabularType</code> instance, checking for the validity of the given parameters.
  47. * The validity constraints are described below for each parameter.
  48. * <p>
  49. * The Java class name of tabular data values this tabular type represents
  50. * (ie the class name returned by the {@link OpenType#getClassName() getClassName} method)
  51. * is set to the string value returned by <code>TabularData.class.getName()</code>.
  52. * <p>
  53. * @param typeName The name given to the tabular type this instance represents; cannot be a null or empty string.
  54. * <br> 
  55. * @param description The human readable description of the tabular type this instance represents;
  56. * cannot be a null or empty string.
  57. * <br> 
  58. * @param rowType The type of the row elements of tabular data values described by this tabular type instance;
  59. * cannot be null.
  60. * <br> 
  61. * @param indexNames The names of the items the values of which are used to uniquely index each row element in the
  62. * tabular data values described by this tabular type instance;
  63. * cannot be null or empty. Each element should be an item name defined in <var>rowType</var>
  64. * (no null or empty string allowed).
  65. * It is important to note that the <b>order</b> of the item names in <var>indexNames</var>
  66. * is used by the methods {@link TabularData#get(java.lang.Object[]) <code>get</code>} and
  67. * {@link TabularData#remove(java.lang.Object[]) <code>remove</code>} of class
  68. * <code>TabularData</code> to match their array of values parameter to items.
  69. * <br> 
  70. * @throws IllegalArgumentException if <var>rowType</var> is null,
  71. * or <var>indexNames</var> is a null or empty array,
  72. * or an element in <var>indexNames</var> is a null or empty string,
  73. * or <var>typeName</var> or <var>description</var> is a null or empty string.
  74. * <br> 
  75. * @throws OpenDataException if an element's value of <var>indexNames</var>
  76. * is not an item name defined in <var>rowType</var>.
  77. */
  78. public TabularType(String typeName,
  79. String description,
  80. CompositeType rowType,
  81. String[] indexNames) throws OpenDataException {
  82. // Check and initialize state defined by parent.
  83. //
  84. super(TabularData.class.getName(), typeName, description);
  85. // Check rowType is not null
  86. //
  87. if (rowType == null) {
  88. throw new IllegalArgumentException("Argument rowType cannot be null.");
  89. }
  90. // Check indexNames is neither null nor empty and does not contain any null element or empty string
  91. //
  92. checkForNullElement(indexNames, "indexNames");
  93. checkForEmptyString(indexNames, "indexNames");
  94. // Check all indexNames values are valid item names for rowType
  95. //
  96. for (int i=0; i<indexNames.length; i++) {
  97. if ( ! rowType.containsKey(indexNames[i]) ) {
  98. throw new OpenDataException("Argument's element value indexNames["+ i +"]=\""+ indexNames[i] +
  99. "\" is not a valid item name for rowType.");
  100. }
  101. }
  102. // initialize rowType
  103. //
  104. this.rowType = rowType;
  105. // initialize indexNames
  106. // (copy content so that subsequent modif to the array referenced by the indexNames parameter have no impact)
  107. //
  108. ArrayList tmpList = new ArrayList(indexNames.length + 1);
  109. for (int i=0; i<indexNames.length; i++) {
  110. tmpList.add(indexNames[i]);
  111. }
  112. this.indexNames = Collections.unmodifiableList(tmpList);
  113. }
  114. /**
  115. * Checks that Object[] arg is neither null nor empty (ie length==0)
  116. * and that it does not contain any null element.
  117. */
  118. private static void checkForNullElement(Object[] arg, String argName) {
  119. if ( (arg == null) || (arg.length == 0) ) {
  120. throw new IllegalArgumentException("Argument "+ argName +"[] cannot be null or empty.");
  121. }
  122. for (int i=0; i<arg.length; i++) {
  123. if (arg[i] == null) {
  124. throw new IllegalArgumentException("Argument's element "+ argName +"["+ i +"] cannot be null.");
  125. }
  126. }
  127. }
  128. /**
  129. * Checks that String[] does not contain any empty (or blank characters only) string.
  130. */
  131. private static void checkForEmptyString(String[] arg, String argName) {
  132. for (int i=0; i<arg.length; i++) {
  133. if (arg[i].trim().equals("")) {
  134. throw new IllegalArgumentException("Argument's element "+ argName +"["+ i +"] cannot be an empty string.");
  135. }
  136. }
  137. }
  138. /* *** Tabular type specific information methods *** */
  139. /**
  140. * Returns the type of the row elements of tabular data values
  141. * described by this <code>TabularType</code> instance.
  142. *
  143. * @return the type of each row.
  144. */
  145. public CompositeType getRowType() {
  146. return rowType;
  147. }
  148. /**
  149. * <p>Returns, in the same order as was given to this instance's
  150. * constructor, an unmodifiable List of the names of the items the
  151. * values of which are used to uniquely index each row element of
  152. * tabular data values described by this <code>TabularType</code>
  153. * instance.</p>
  154. *
  155. * @return a List of String representing the names of the index
  156. * items.
  157. *
  158. */
  159. public List getIndexNames() {
  160. return indexNames;
  161. }
  162. /**
  163. * Tests whether <var>obj</var> is a value which could be described by this <code>TabularType</code> instance.
  164. * <p>
  165. * If <var>obj</var> is null or is not an instance of <code>javax.management.openmbean.TabularData</code>,
  166. * <code>isValue</code> returns <code>false</code>.
  167. * If <var>obj</var> is an instance of <code>javax.management.openmbean.TabularData</code>,
  168. * its tabular type is tested for equality with this tabular type instance, and <code>isValue</code>
  169. * returns <code>true</code> if and only if {@link #equals(java.lang.Object) <code>equals</code>}
  170. * returns <code>true</code>.
  171. * <br> 
  172. * @param obj the value whose open type is to be tested for equality with this <code>TabularType</code> instance.
  173. *
  174. * @return <code>true</code> if <var>obj</var> is a value for this tabular type, <code>false</code> otherwise.
  175. */
  176. public boolean isValue(Object obj) {
  177. // if obj is null, return false
  178. //
  179. if (obj == null) {
  180. return false;
  181. }
  182. // if obj is not a TabularData, return false
  183. //
  184. TabularData value;
  185. try {
  186. value = (TabularData) obj;
  187. } catch (ClassCastException e) {
  188. return false;
  189. }
  190. // test value's TabularType for equality with this TabularType instance
  191. //
  192. return this.equals(value.getTabularType());
  193. }
  194. /* *** Methods overriden from class Object *** */
  195. /**
  196. * Compares the specified <code>obj</code> parameter with this <code>TabularType</code> instance for equality.
  197. * <p>
  198. * Two <code>TabularType</code> instances are equal if and only if all of the following statements are true:
  199. * <ul>
  200. * <li>their type names are equal</li>
  201. * <li>their row types are equal</li>
  202. * <li>they use the same index names, in the same order</li>
  203. * </ul>
  204. * <br> 
  205. * @param obj the object to be compared for equality with this <code>TabularType</code> instance;
  206. * if <var>obj</var> is <code>null</code>, <code>equals</code> returns <code>false</code>.
  207. *
  208. * @return <code>true</code> if the specified object is equal to this <code>TabularType</code> instance.
  209. */
  210. public boolean equals(Object obj) {
  211. // if obj is null, return false
  212. //
  213. if (obj == null) {
  214. return false;
  215. }
  216. // if obj is not a TabularType, return false
  217. //
  218. TabularType other;
  219. try {
  220. other = (TabularType) obj;
  221. } catch (ClassCastException e) {
  222. return false;
  223. }
  224. // Now, really test for equality between this TabularType instance and the other:
  225. //
  226. // their names should be equal
  227. if ( ! this.getTypeName().equals(other.getTypeName()) ) {
  228. return false;
  229. }
  230. // their row types should be equal
  231. if ( ! this.rowType.equals(other.rowType) ) {
  232. return false;
  233. }
  234. // their index names should be equal and in the same order (ensured by List.equals())
  235. if ( ! this.indexNames.equals(other.indexNames) ) {
  236. return false;
  237. }
  238. // All tests for equality were successfull
  239. //
  240. return true;
  241. }
  242. /**
  243. * Returns the hash code value for this <code>TabularType</code> instance.
  244. * <p>
  245. * The hash code of a <code>TabularType</code> instance is the sum of the hash codes
  246. * of all elements of information used in <code>equals</code> comparisons
  247. * (ie: name, row type, index names).
  248. * This ensures that <code> t1.equals(t2) </code> implies that <code> t1.hashCode()==t2.hashCode() </code>
  249. * for any two <code>TabularType</code> instances <code>t1</code> and <code>t2</code>,
  250. * as required by the general contract of the method
  251. * {@link Object#hashCode() Object.hashCode()}.
  252. * <p>
  253. * As <code>TabularType</code> instances are immutable, the hash code for this instance is calculated once,
  254. * on the first call to <code>hashCode</code>, and then the same value is returned for subsequent calls.
  255. *
  256. * @return the hash code value for this <code>TabularType</code> instance
  257. */
  258. public int hashCode() {
  259. // Calculate the hash code value if it has not yet been done (ie 1st call to hashCode())
  260. //
  261. if (myHashCode == null) {
  262. int value = 0;
  263. value += this.getTypeName().hashCode();
  264. value += this.rowType.hashCode();
  265. for (Iterator k = indexNames.iterator(); k.hasNext(); ) {
  266. value += k.next().hashCode();
  267. }
  268. myHashCode = new Integer(value);
  269. }
  270. // return always the same hash code for this instance (immutable)
  271. //
  272. return myHashCode.intValue();
  273. }
  274. /**
  275. * Returns a string representation of this <code>TabularType</code> instance.
  276. * <p>
  277. * The string representation consists of the name of this class (ie <code>javax.management.openmbean.TabularType</code>),
  278. * the type name for this instance, the row type string representation of this instance,
  279. * and the index names of this instance.
  280. * <p>
  281. * As <code>TabularType</code> instances are immutable, the string representation for this instance is calculated once,
  282. * on the first call to <code>toString</code>, and then the same value is returned for subsequent calls.
  283. *
  284. * @return a string representation of this <code>TabularType</code> instance
  285. */
  286. public String toString() {
  287. // Calculate the string representation if it has not yet been done (ie 1st call to toString())
  288. //
  289. if (myToString == null) {
  290. StringBuffer result = new StringBuffer()
  291. .append(this.getClass().getName())
  292. .append("(name=")
  293. .append(getTypeName())
  294. .append(",rowType=")
  295. .append(rowType.toString())
  296. .append(",indexNames=(");
  297. int i=0;
  298. Iterator k = indexNames.iterator();
  299. while( k.hasNext() ) {
  300. if (i > 0) result.append(",");
  301. result.append(k.next().toString());
  302. i++;
  303. }
  304. result.append("))");
  305. myToString = result.toString();
  306. }
  307. // return always the same string representation for this instance (immutable)
  308. //
  309. return myToString;
  310. }
  311. }