1. /*
  2. * @(#)OpenMBeanConstructorInfoSupport.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.Arrays;
  12. // jmx import
  13. //
  14. import javax.management.MBeanConstructorInfo;
  15. import javax.management.MBeanParameterInfo;
  16. /**
  17. * Describes a constructor of an Open MBean.
  18. *
  19. * @version 3.22 03/12/19
  20. * @author Sun Microsystems, Inc.
  21. *
  22. * @since 1.5
  23. * @since.unbundled JMX 1.1
  24. */
  25. public class OpenMBeanConstructorInfoSupport
  26. extends MBeanConstructorInfo
  27. implements OpenMBeanConstructorInfo, Serializable {
  28. /* Serial version */
  29. static final long serialVersionUID = -4400441579007477003L;
  30. private transient Integer myHashCode = null; // As this instance is immutable, these two values
  31. private transient String myToString = null; // need only be calculated once.
  32. /**
  33. * Constructs an <tt>OpenMBeanConstructorInfoSupport</tt> instance, which describes the constructor
  34. * of a class of open MBeans with the specified <var>name</var>, <var>description</var> and <var>signature</var>.
  35. * <p>
  36. * The <var>signature</var> array parameter is internally copied, so that subsequent changes
  37. * to the array referenced by <var>signature</var> have no effect on this instance.
  38. *
  39. * @param name cannot be a null or empty string.
  40. *
  41. * @param description cannot be a null or empty string.
  42. *
  43. * @param signature can be null or empty if there are no parameters to describe.
  44. *
  45. * @throws IllegalArgumentException if <var>name</var> or <var>description</var> are null or empty string.
  46. *
  47. * @throws ArrayStoreException If <var>signature</var> is not an array of instances of a subclass of <tt>MBeanParameterInfo</tt>.
  48. */
  49. public OpenMBeanConstructorInfoSupport(String name,
  50. String description,
  51. OpenMBeanParameterInfo[] signature) {
  52. super(name,
  53. description,
  54. ( signature == null ? null : arrayCopyCast(signature) )); // may throw an ArrayStoreException
  55. // check parameters that should not be null or empty (unfortunately it is not done in superclass :-( ! )
  56. //
  57. if ( (name == null) || (name.trim().equals("")) ) {
  58. throw new IllegalArgumentException("Argument name cannot be null or empty.");
  59. }
  60. if ( (description == null) || (description.trim().equals("")) ) {
  61. throw new IllegalArgumentException("Argument description cannot be null or empty.");
  62. }
  63. }
  64. private static MBeanParameterInfo[] arrayCopyCast(OpenMBeanParameterInfo[] src) throws ArrayStoreException {
  65. MBeanParameterInfo[] dst = new MBeanParameterInfo[src.length];
  66. System.arraycopy(src, 0, dst, 0, src.length); // may throw an ArrayStoreException
  67. return dst;
  68. }
  69. /* *** Commodity methods from java.lang.Object *** */
  70. /**
  71. * Compares the specified <var>obj</var> parameter with this <code>OpenMBeanConstructorInfoSupport</code> instance for equality.
  72. * <p>
  73. * Returns <tt>true</tt> if and only if all of the following statements are true:
  74. * <ul>
  75. * <li><var>obj</var> is non null,</li>
  76. * <li><var>obj</var> also implements the <code>OpenMBeanConstructorInfo</code> interface,</li>
  77. * <li>their names are equal</li>
  78. * <li>their signatures are equal.</li>
  79. * </ul>
  80. * This ensures that this <tt>equals</tt> method works properly for <var>obj</var> parameters which are
  81. * different implementations of the <code>OpenMBeanConstructorInfo</code> interface.
  82. * <br> 
  83. * @param obj the object to be compared for equality with this <code>OpenMBeanConstructorInfoSupport</code> instance;
  84. *
  85. * @return <code>true</code> if the specified object is equal to this <code>OpenMBeanConstructorInfoSupport</code> instance.
  86. */
  87. public boolean equals(Object obj) {
  88. // if obj is null, return false
  89. //
  90. if (obj == null) {
  91. return false;
  92. }
  93. // if obj is not a OpenMBeanConstructorInfo, return false
  94. //
  95. OpenMBeanConstructorInfo other;
  96. try {
  97. other = (OpenMBeanConstructorInfo) obj;
  98. } catch (ClassCastException e) {
  99. return false;
  100. }
  101. // Now, really test for equality between this OpenMBeanConstructorInfo implementation and the other:
  102. //
  103. // their Name should be equal
  104. if ( ! this.getName().equals(other.getName()) ) {
  105. return false;
  106. }
  107. // their Signatures should be equal
  108. if ( ! Arrays.equals(this.getSignature(), other.getSignature()) ) {
  109. return false;
  110. }
  111. // All tests for equality were successfull
  112. //
  113. return true;
  114. }
  115. /**
  116. * Returns the hash code value for this <code>OpenMBeanConstructorInfoSupport</code> instance.
  117. * <p>
  118. * The hash code of an <code>OpenMBeanConstructorInfoSupport</code> instance is the sum of the hash codes
  119. * of all elements of information used in <code>equals</code> comparisons
  120. * (ie: its name and signature, where the signature hashCode is calculated by a call to
  121. * <tt>java.util.Arrays.asList(this.getSignature).hashCode()</tt>).
  122. * <p>
  123. * This ensures that <code> t1.equals(t2) </code> implies that <code> t1.hashCode()==t2.hashCode() </code>
  124. * for any two <code>OpenMBeanConstructorInfoSupport</code> instances <code>t1</code> and <code>t2</code>,
  125. * as required by the general contract of the method
  126. * {@link Object#hashCode() Object.hashCode()}.
  127. * <p>
  128. * However, note that another instance of a class implementing the <code>OpenMBeanConstructorInfo</code> interface
  129. * may be equal to this <code>OpenMBeanConstructorInfoSupport</code> instance as defined by {@link #equals(java.lang.Object)},
  130. * but may have a different hash code if it is calculated differently.
  131. * <p>
  132. * As <code>OpenMBeanConstructorInfoSupport</code> instances are immutable, the hash code for this instance is calculated once,
  133. * on the first call to <code>hashCode</code>, and then the same value is returned for subsequent calls.
  134. *
  135. * @return the hash code value for this <code>OpenMBeanConstructorInfoSupport</code> instance
  136. */
  137. public int hashCode() {
  138. // Calculate the hash code value if it has not yet been done (ie 1st call to hashCode())
  139. //
  140. if (myHashCode == null) {
  141. int value = 0;
  142. value += this.getName().hashCode();
  143. value += Arrays.asList(this.getSignature()).hashCode();
  144. myHashCode = new Integer(value);
  145. }
  146. // return always the same hash code for this instance (immutable)
  147. //
  148. return myHashCode.intValue();
  149. }
  150. /**
  151. * Returns a string representation of this <code>OpenMBeanConstructorInfoSupport</code> instance.
  152. * <p>
  153. * The string representation consists of the name of this class (ie <code>javax.management.openmbean.OpenMBeanConstructorInfoSupport</code>),
  154. * and of the name and signature of the described constructor.
  155. * <p>
  156. * As <code>OpenMBeanConstructorInfoSupport</code> instances are immutable,
  157. * the string representation for this instance is calculated once,
  158. * on the first call to <code>toString</code>, and then the same value is returned for subsequent calls.
  159. *
  160. * @return a string representation of this <code>OpenMBeanConstructorInfoSupport</code> instance
  161. */
  162. public String toString() {
  163. // Calculate the hash code value if it has not yet been done (ie 1st call to toString())
  164. //
  165. if (myToString == null) {
  166. myToString = new StringBuffer()
  167. .append(this.getClass().getName())
  168. .append("(name=")
  169. .append(this.getName())
  170. .append(",signature=")
  171. .append(Arrays.asList(this.getSignature()).toString())
  172. .append(")")
  173. .toString();
  174. }
  175. // return always the same string representation for this instance (immutable)
  176. //
  177. return myToString;
  178. }
  179. }