KickJava   Java API By Example, From Geeks To Geeks.

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


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
8
9 package javax.management.openmbean;
10
11
12 // java import
13
//
14
import java.io.Serializable JavaDoc;
15 import java.util.Arrays JavaDoc;
16
17
18 // jmx import
19
//
20
import javax.management.MBeanConstructorInfo JavaDoc;
21 import javax.management.MBeanParameterInfo JavaDoc;
22
23
24 /**
25  * Describes a constructor of an Open MBean.
26  *
27  * @version 3.22 03/12/19
28  * @author Sun Microsystems, Inc.
29  *
30  * @since 1.5
31  * @since.unbundled JMX 1.1
32  */

33 public class OpenMBeanConstructorInfoSupport
34     extends MBeanConstructorInfo JavaDoc
35     implements OpenMBeanConstructorInfo JavaDoc, Serializable JavaDoc {
36     
37     /* Serial version */
38     static final long serialVersionUID = -4400441579007477003L;
39
40
41     private transient Integer JavaDoc myHashCode = null; // As this instance is immutable, these two values
42
private transient String JavaDoc myToString = null; // need only be calculated once.
43

44     /**
45      * Constructs an <tt>OpenMBeanConstructorInfoSupport</tt> instance, which describes the constructor
46      * of a class of open MBeans with the specified <var>name</var>, <var>description</var> and <var>signature</var>.
47      * <p>
48      * The <var>signature</var> array parameter is internally copied, so that subsequent changes
49      * to the array referenced by <var>signature</var> have no effect on this instance.
50      *
51      * @param name cannot be a null or empty string.
52      *
53      * @param description cannot be a null or empty string.
54      *
55      * @param signature can be null or empty if there are no parameters to describe.
56      *
57      * @throws IllegalArgumentException if <var>name</var> or <var>description</var> are null or empty string.
58      *
59      * @throws ArrayStoreException If <var>signature</var> is not an array of instances of a subclass of <tt>MBeanParameterInfo</tt>.
60      */

61     public OpenMBeanConstructorInfoSupport(String JavaDoc name,
62                        String JavaDoc description,
63                        OpenMBeanParameterInfo JavaDoc[] signature) {
64
65     super(name,
66           description,
67           ( signature == null ? null : arrayCopyCast(signature) )); // may throw an ArrayStoreException
68

69     // check parameters that should not be null or empty (unfortunately it is not done in superclass :-( ! )
70
//
71
if ( (name == null) || (name.trim().equals("")) ) {
72         throw new IllegalArgumentException JavaDoc("Argument name cannot be null or empty.");
73     }
74     if ( (description == null) || (description.trim().equals("")) ) {
75         throw new IllegalArgumentException JavaDoc("Argument description cannot be null or empty.");
76     }
77
78     }
79
80     private static MBeanParameterInfo JavaDoc[] arrayCopyCast(OpenMBeanParameterInfo JavaDoc[] src) throws ArrayStoreException JavaDoc {
81
82     MBeanParameterInfo JavaDoc[] dst = new MBeanParameterInfo JavaDoc[src.length];
83     System.arraycopy(src, 0, dst, 0, src.length); // may throw an ArrayStoreException
84
return dst;
85     }
86
87
88     /* *** Commodity methods from java.lang.Object *** */
89
90
91     /**
92      * Compares the specified <var>obj</var> parameter with this <code>OpenMBeanConstructorInfoSupport</code> instance for equality.
93      * <p>
94      * Returns <tt>true</tt> if and only if all of the following statements are true:
95      * <ul>
96      * <li><var>obj</var> is non null,</li>
97      * <li><var>obj</var> also implements the <code>OpenMBeanConstructorInfo</code> interface,</li>
98      * <li>their names are equal</li>
99      * <li>their signatures are equal.</li>
100      * </ul>
101      * This ensures that this <tt>equals</tt> method works properly for <var>obj</var> parameters which are
102      * different implementations of the <code>OpenMBeanConstructorInfo</code> interface.
103      * <br>&nbsp;
104      * @param obj the object to be compared for equality with this <code>OpenMBeanConstructorInfoSupport</code> instance;
105      *
106      * @return <code>true</code> if the specified object is equal to this <code>OpenMBeanConstructorInfoSupport</code> instance.
107      */

108     public boolean equals(Object JavaDoc obj) {
109
110     // if obj is null, return false
111
//
112
if (obj == null) {
113         return false;
114     }
115
116     // if obj is not a OpenMBeanConstructorInfo, return false
117
//
118
OpenMBeanConstructorInfo JavaDoc other;
119     try {
120         other = (OpenMBeanConstructorInfo JavaDoc) obj;
121     } catch (ClassCastException JavaDoc e) {
122         return false;
123     }
124
125     // Now, really test for equality between this OpenMBeanConstructorInfo implementation and the other:
126
//
127

128     // their Name should be equal
129
if ( ! this.getName().equals(other.getName()) ) {
130         return false;
131     }
132
133     // their Signatures should be equal
134
if ( ! Arrays.equals(this.getSignature(), other.getSignature()) ) {
135         return false;
136     }
137        
138     // All tests for equality were successfull
139
//
140
return true;
141     }
142
143     /**
144      * Returns the hash code value for this <code>OpenMBeanConstructorInfoSupport</code> instance.
145      * <p>
146      * The hash code of an <code>OpenMBeanConstructorInfoSupport</code> instance is the sum of the hash codes
147      * of all elements of information used in <code>equals</code> comparisons
148      * (ie: its name and signature, where the signature hashCode is calculated by a call to
149      * <tt>java.util.Arrays.asList(this.getSignature).hashCode()</tt>).
150      * <p>
151      * This ensures that <code> t1.equals(t2) </code> implies that <code> t1.hashCode()==t2.hashCode() </code>
152      * for any two <code>OpenMBeanConstructorInfoSupport</code> instances <code>t1</code> and <code>t2</code>,
153      * as required by the general contract of the method
154      * {@link Object#hashCode() Object.hashCode()}.
155      * <p>
156      * However, note that another instance of a class implementing the <code>OpenMBeanConstructorInfo</code> interface
157      * may be equal to this <code>OpenMBeanConstructorInfoSupport</code> instance as defined by {@link #equals(java.lang.Object)},
158      * but may have a different hash code if it is calculated differently.
159      * <p>
160      * As <code>OpenMBeanConstructorInfoSupport</code> instances are immutable, the hash code for this instance is calculated once,
161      * on the first call to <code>hashCode</code>, and then the same value is returned for subsequent calls.
162      *
163      * @return the hash code value for this <code>OpenMBeanConstructorInfoSupport</code> instance
164      */

165     public int hashCode() {
166
167     // Calculate the hash code value if it has not yet been done (ie 1st call to hashCode())
168
//
169
if (myHashCode == null) {
170         int value = 0;
171         value += this.getName().hashCode();
172         value += Arrays.asList(this.getSignature()).hashCode();
173         myHashCode = new Integer JavaDoc(value);
174     }
175     
176     // return always the same hash code for this instance (immutable)
177
//
178
return myHashCode.intValue();
179     }
180
181     /**
182      * Returns a string representation of this <code>OpenMBeanConstructorInfoSupport</code> instance.
183      * <p>
184      * The string representation consists of the name of this class (ie <code>javax.management.openmbean.OpenMBeanConstructorInfoSupport</code>),
185      * and of the name and signature of the described constructor.
186      * <p>
187      * As <code>OpenMBeanConstructorInfoSupport</code> instances are immutable,
188      * the string representation for this instance is calculated once,
189      * on the first call to <code>toString</code>, and then the same value is returned for subsequent calls.
190      *
191      * @return a string representation of this <code>OpenMBeanConstructorInfoSupport</code> instance
192      */

193     public String JavaDoc toString() {
194
195     // Calculate the hash code value if it has not yet been done (ie 1st call to toString())
196
//
197
if (myToString == null) {
198         myToString = new StringBuffer JavaDoc()
199         .append(this.getClass().getName())
200         .append("(name=")
201         .append(this.getName())
202         .append(",signature=")
203         .append(Arrays.asList(this.getSignature()).toString())
204         .append(")")
205         .toString();
206     }
207
208     // return always the same string representation for this instance (immutable)
209
//
210
return myToString;
211     }
212
213 }
214
Popular Tags