KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * @(#)OpenMBeanOperationInfoSupport.java 3.25 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 // jmx import
18
//
19
import javax.management.MBeanOperationInfo JavaDoc;
20 import javax.management.MBeanParameterInfo JavaDoc;
21
22
23 /**
24  * Describes an operation of an Open MBean.
25  *
26  * @version 3.25 03/12/19
27  * @author Sun Microsystems, Inc.
28  *
29  * @since 1.5
30  * @since.unbundled JMX 1.1
31  */

32 public class OpenMBeanOperationInfoSupport
33     extends MBeanOperationInfo JavaDoc
34     implements OpenMBeanOperationInfo JavaDoc, Serializable JavaDoc {
35     
36     
37     /* Serial version */
38     static final long serialVersionUID = 4996859732565369366L;
39
40     /**
41      * @serial The <i>open type</i> of the values returned by the operation
42      * described by this {@link OpenMBeanOperationInfo} instance
43      *
44      */

45     private OpenType JavaDoc returnOpenType;
46
47
48     private transient Integer JavaDoc myHashCode = null; // As this instance is immutable, these two values
49
private transient String JavaDoc myToString = null; // need only be calculated once.
50

51
52     /**
53      * Constructs an <tt>OpenMBeanOperationInfoSupport</tt> instance, which describes the operation
54      * of a class of open MBeans, with the specified
55      * <var>name</var>, <var>description</var>, <var>signature</var>, <var>returnOpenType</var> and <var>impact</var>.
56      * <p>
57      * The <var>signature</var> array parameter is internally copied, so that subsequent changes
58      * to the array referenced by <var>signature</var> have no effect on this instance.
59      * <p>
60      *
61      * @param name cannot be a null or empty string.
62      *
63      * @param description cannot be a null or empty string.
64      *
65      * @param signature can be null or empty if there are no parameters to describe.
66      *
67      * @param returnOpenType cannot be null: use <tt>SimpleType.VOID</tt> for operations that return nothing.
68      *
69      * @param impact can be only one of <tt>ACTION</tt>, <tt>ACTION_INFO</tt> or <tt>INFO</tt>.
70      *
71      * @throws IllegalArgumentException if <var>name</var> or <var>description</var> are null or empty string,
72      * or <var>returnOpenType</var> is null,
73      * or <var>impact</var> is not one of <tt>ACTION</tt>, <tt>ACTION_INFO</tt> or <tt>INFO</tt>.
74      *
75      * @throws ArrayStoreException If <var>signature</var> is not an array of instances of a subclass of <tt>MBeanParameterInfo</tt>.
76      */

77     public OpenMBeanOperationInfoSupport(String JavaDoc name,
78                      String JavaDoc description,
79                      OpenMBeanParameterInfo JavaDoc[] signature,
80                      OpenType JavaDoc returnOpenType,
81                      int impact) {
82
83     super(name,
84           description,
85           ( signature == null ? null : arrayCopyCast(signature) ), // may throw an ArrayStoreException
86
( returnOpenType == null ? null : returnOpenType.getClassName() ),
87           impact);
88
89     // check parameters that should not be null or empty (unfortunately it is not done in superclass :-( ! )
90
//
91
if ( (name == null) || (name.trim().equals("")) ) {
92         throw new IllegalArgumentException JavaDoc("Argument name cannot be null or empty.");
93     }
94     if ( (description == null) || (description.trim().equals("")) ) {
95         throw new IllegalArgumentException JavaDoc("Argument description cannot be null or empty.");
96     }
97     if (returnOpenType == null) {
98         throw new IllegalArgumentException JavaDoc("Argument returnOpenType cannot be null.");
99     }
100
101     // check impact's value is only one of the 3 allowed (UNKNOWN not allowed)
102
//
103
if ( (impact != super.ACTION) && (impact != super.ACTION_INFO) && (impact != super.INFO) ) {
104         throw new IllegalArgumentException JavaDoc("Argument impact can be only one of ACTION, ACTION_INFO or INFO.");
105     }
106
107     this.returnOpenType = returnOpenType;
108     }
109
110
111     private static MBeanParameterInfo JavaDoc[] arrayCopyCast(OpenMBeanParameterInfo JavaDoc[] src) throws ArrayStoreException JavaDoc {
112
113     MBeanParameterInfo JavaDoc[] dst = new MBeanParameterInfo JavaDoc[src.length];
114     System.arraycopy(src, 0, dst, 0, src.length); // may throw an ArrayStoreException
115
return dst;
116     }
117
118
119     // [JF]: should we add constructor with java.lang.reflect.Method method parameter ?
120
// would need to add consistency check between OpenType returnOpenType and mehtod.getReturnType().
121

122
123     /**
124      * Returns the <i>open type</i> of the values returned by the operation described by this <tt>OpenMBeanOperationInfo</tt> instance.
125      */

126     public OpenType JavaDoc getReturnOpenType() {
127
128     return returnOpenType;
129     }
130
131
132
133     /* *** Commodity methods from java.lang.Object *** */
134
135
136     /**
137      * Compares the specified <var>obj</var> parameter with this <code>OpenMBeanOperationInfoSupport</code> instance for equality.
138      * <p>
139      * Returns <tt>true</tt> if and only if all of the following statements are true:
140      * <ul>
141      * <li><var>obj</var> is non null,</li>
142      * <li><var>obj</var> also implements the <code>OpenMBeanOperationInfo</code> interface,</li>
143      * <li>their names are equal</li>
144      * <li>their signatures are equal</li>
145      * <li>their return open types are equal</li>
146      * <li>their impacts are equal</li>
147      * </ul>
148      * This ensures that this <tt>equals</tt> method works properly for <var>obj</var> parameters which are
149      * different implementations of the <code>OpenMBeanOperationInfo</code> interface.
150      * <br>&nbsp;
151      * @param obj the object to be compared for equality with this <code>OpenMBeanOperationInfoSupport</code> instance;
152      *
153      * @return <code>true</code> if the specified object is equal to this <code>OpenMBeanOperationInfoSupport</code> instance.
154      */

155     public boolean equals(Object JavaDoc obj) {
156
157     // if obj is null, return false
158
//
159
if (obj == null) {
160         return false;
161     }
162
163     // if obj is not a OpenMBeanOperationInfo, return false
164
//
165
OpenMBeanOperationInfo JavaDoc other;
166     try {
167         other = (OpenMBeanOperationInfo JavaDoc) obj;
168     } catch (ClassCastException JavaDoc e) {
169         return false;
170     }
171
172     // Now, really test for equality between this OpenMBeanOperationInfo implementation and the other:
173
//
174

175     // their Name should be equal
176
if ( ! this.getName().equals(other.getName()) ) {
177         return false;
178     }
179
180     // their Signatures should be equal
181
if ( ! Arrays.equals(this.getSignature(), other.getSignature()) ) {
182         return false;
183     }
184        
185     // their return open types should be equal
186
if ( ! this.getReturnOpenType().equals(other.getReturnOpenType()) ) {
187         return false;
188     }
189
190     // their impacts should be equal
191
if ( this.getImpact() != other.getImpact() ) {
192         return false;
193     }
194
195     // All tests for equality were successfull
196
//
197
return true;
198     }
199
200     /**
201      * Returns the hash code value for this <code>OpenMBeanOperationInfoSupport</code> instance.
202      * <p>
203      * The hash code of an <code>OpenMBeanOperationInfoSupport</code> instance is the sum of the hash codes
204      * of all elements of information used in <code>equals</code> comparisons
205      * (ie: its name, return open type, impact and signature, where the signature hashCode is calculated by a call to
206      * <tt>java.util.Arrays.asList(this.getSignature).hashCode()</tt>).
207      * <p>
208      * This ensures that <code> t1.equals(t2) </code> implies that <code> t1.hashCode()==t2.hashCode() </code>
209      * for any two <code>OpenMBeanOperationInfoSupport</code> instances <code>t1</code> and <code>t2</code>,
210      * as required by the general contract of the method
211      * {@link Object#hashCode() Object.hashCode()}.
212      * <p>
213      * However, note that another instance of a class implementing the <code>OpenMBeanOperationInfo</code> interface
214      * may be equal to this <code>OpenMBeanOperationInfoSupport</code> instance as defined by {@link #equals(java.lang.Object)},
215      * but may have a different hash code if it is calculated differently.
216      * <p>
217      * As <code>OpenMBeanOperationInfoSupport</code> instances are immutable, the hash code for this instance is calculated once,
218      * on the first call to <code>hashCode</code>, and then the same value is returned for subsequent calls.
219      *
220      * @return the hash code value for this <code>OpenMBeanOperationInfoSupport</code> instance
221      */

222     public int hashCode() {
223
224     // Calculate the hash code value if it has not yet been done (ie 1st call to hashCode())
225
//
226
if (myHashCode == null) {
227         int value = 0;
228         value += this.getName().hashCode();
229         value += Arrays.asList(this.getSignature()).hashCode();
230         value += this.getReturnOpenType().hashCode();
231         value += this.getImpact();
232         myHashCode = new Integer JavaDoc(value);
233     }
234     
235     // return always the same hash code for this instance (immutable)
236
//
237
return myHashCode.intValue();
238     }
239
240     /**
241      * Returns a string representation of this <code>OpenMBeanOperationInfoSupport</code> instance.
242      * <p>
243      * The string representation consists of the name of this class (ie <code>javax.management.openmbean.OpenMBeanOperationInfoSupport</code>),
244      * and the name, signature, return open type and impact of the described operation.
245      * <p>
246      * As <code>OpenMBeanOperationInfoSupport</code> instances are immutable,
247      * the string representation for this instance is calculated once,
248      * on the first call to <code>toString</code>, and then the same value is returned for subsequent calls.
249      *
250      * @return a string representation of this <code>OpenMBeanOperationInfoSupport</code> instance
251      */

252     public String JavaDoc toString() {
253
254     // Calculate the hash code value if it has not yet been done (ie 1st call to toString())
255
//
256
if (myToString == null) {
257         myToString = new StringBuffer JavaDoc()
258         .append(this.getClass().getName())
259         .append("(name=")
260         .append(this.getName())
261         .append(",signature=")
262         .append(Arrays.asList(this.getSignature()).toString())
263         .append(",return=")
264         .append(this.getReturnOpenType().toString())
265         .append(",impact=")
266         .append(this.getImpact())
267         .append(")")
268         .toString();
269     }
270
271     // return always the same string representation for this instance (immutable)
272
//
273
return myToString;
274     }
275
276 }
277
Popular Tags