001 /*
002 * Copyright 2000-2007 Sun Microsystems, Inc. All Rights Reserved.
003 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004 *
005 * This code is free software; you can redistribute it and/or modify it
006 * under the terms of the GNU General Public License version 2 only, as
007 * published by the Free Software Foundation. Sun designates this
008 * particular file as subject to the "Classpath" exception as provided
009 * by Sun in the LICENSE file that accompanied this code.
010 *
011 * This code is distributed in the hope that it will be useful, but WITHOUT
012 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014 * version 2 for more details (a copy is included in the LICENSE file that
015 * accompanied this code).
016 *
017 * You should have received a copy of the GNU General Public License version
018 * 2 along with this work; if not, write to the Free Software Foundation,
019 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020 *
021 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022 * CA 95054 USA or visit www.sun.com if you need additional information or
023 * have any questions.
024 */
025
026 package javax.management.openmbean;
027
028 // java import
029 //
030 import java.util.Arrays;
031
032 import javax.management.Descriptor;
033 import javax.management.MBeanConstructorInfo;
034 import javax.management.MBeanParameterInfo;
035
036 /**
037 * Describes a constructor of an Open MBean.
038 *
039 *
040 * @since 1.5
041 */
042 public class OpenMBeanConstructorInfoSupport extends
043 MBeanConstructorInfo implements OpenMBeanConstructorInfo {
044
045 /* Serial version */
046 static final long serialVersionUID = -4400441579007477003L;
047
048 // As this instance is immutable,
049 // these two values need only be calculated once.
050 private transient Integer myHashCode = null;
051 private transient String myToString = null;
052
053 /**
054 * <p>Constructs an {@code OpenMBeanConstructorInfoSupport}
055 * instance, which describes the constructor of a class of open
056 * MBeans with the specified {@code name}, {@code description} and
057 * {@code signature}.</p>
058 *
059 * <p>The {@code signature} array parameter is internally copied,
060 * so that subsequent changes to the array referenced by {@code
061 * signature} have no effect on this instance.</p>
062 *
063 * @param name cannot be a null or empty string.
064 *
065 * @param description cannot be a null or empty string.
066 *
067 * @param signature can be null or empty if there are no
068 * parameters to describe.
069 *
070 * @throws IllegalArgumentException if {@code name} or {@code
071 * description} are null or empty string.
072 *
073 * @throws ArrayStoreException If {@code signature} is not an
074 * array of instances of a subclass of {@code MBeanParameterInfo}.
075 */
076 public OpenMBeanConstructorInfoSupport(String name,
077 String description, OpenMBeanParameterInfo[] signature) {
078 this (name, description, signature, (Descriptor) null);
079 }
080
081 /**
082 * <p>Constructs an {@code OpenMBeanConstructorInfoSupport}
083 * instance, which describes the constructor of a class of open
084 * MBeans with the specified {@code name}, {@code description},
085 * {@code signature}, and {@code descriptor}.</p>
086 *
087 * <p>The {@code signature} array parameter is internally copied,
088 * so that subsequent changes to the array referenced by {@code
089 * signature} have no effect on this instance.</p>
090 *
091 * @param name cannot be a null or empty string.
092 *
093 * @param description cannot be a null or empty string.
094 *
095 * @param signature can be null or empty if there are no
096 * parameters to describe.
097 *
098 * @param descriptor The descriptor for the constructor. This may
099 * be null which is equivalent to an empty descriptor.
100 *
101 * @throws IllegalArgumentException if {@code name} or {@code
102 * description} are null or empty string.
103 *
104 * @throws ArrayStoreException If {@code signature} is not an
105 * array of instances of a subclass of {@code MBeanParameterInfo}.
106 *
107 * @since 1.6
108 */
109 public OpenMBeanConstructorInfoSupport(String name,
110 String description, OpenMBeanParameterInfo[] signature,
111 Descriptor descriptor) {
112 super (name, description, arrayCopyCast(signature), // may throw an ArrayStoreException
113 descriptor);
114
115 // check parameters that should not be null or empty
116 // (unfortunately it is not done in superclass :-( ! )
117 //
118 if (name == null || name.trim().equals("")) {
119 throw new IllegalArgumentException(
120 "Argument name cannot be " + "null or empty");
121 }
122 if (description == null || description.trim().equals("")) {
123 throw new IllegalArgumentException(
124 "Argument description cannot " + "be null or empty");
125 }
126
127 }
128
129 private static MBeanParameterInfo[] arrayCopyCast(
130 OpenMBeanParameterInfo[] src) {
131 if (src == null)
132 return null;
133
134 MBeanParameterInfo[] dst = new MBeanParameterInfo[src.length];
135 System.arraycopy(src, 0, dst, 0, src.length);
136 // may throw an ArrayStoreException
137 return dst;
138 }
139
140 /* *** Commodity methods from java.lang.Object *** */
141
142 /**
143 * <p>Compares the specified {@code obj} parameter with this
144 * {@code OpenMBeanConstructorInfoSupport} instance for
145 * equality.</p>
146 *
147 * <p>Returns {@code true} if and only if all of the following
148 * statements are true:
149 *
150 * <ul>
151 * <li>{@code obj} is non null,</li>
152 * <li>{@code obj} also implements the {@code
153 * OpenMBeanConstructorInfo} interface,</li>
154 * <li>their names are equal</li>
155 * <li>their signatures are equal.</li>
156 * </ul>
157 *
158 * This ensures that this {@code equals} method works properly for
159 * {@code obj} parameters which are different implementations of
160 * the {@code OpenMBeanConstructorInfo} interface.
161 *
162 * @param obj the object to be compared for equality with this
163 * {@code OpenMBeanConstructorInfoSupport} instance;
164 *
165 * @return {@code true} if the specified object is equal to this
166 * {@code OpenMBeanConstructorInfoSupport} instance.
167 */
168 public boolean equals(Object obj) {
169
170 // if obj is null, return false
171 //
172 if (obj == null) {
173 return false;
174 }
175
176 // if obj is not a OpenMBeanConstructorInfo, return false
177 //
178 OpenMBeanConstructorInfo other;
179 try {
180 other = (OpenMBeanConstructorInfo) obj;
181 } catch (ClassCastException e) {
182 return false;
183 }
184
185 // Now, really test for equality between this
186 // OpenMBeanConstructorInfo implementation and the other:
187 //
188
189 // their Name should be equal
190 if (!this .getName().equals(other.getName())) {
191 return false;
192 }
193
194 // their Signatures should be equal
195 if (!Arrays.equals(this .getSignature(), other.getSignature())) {
196 return false;
197 }
198
199 // All tests for equality were successfull
200 //
201 return true;
202 }
203
204 /**
205 * <p>Returns the hash code value for this {@code
206 * OpenMBeanConstructorInfoSupport} instance.</p>
207 *
208 * <p>The hash code of an {@code OpenMBeanConstructorInfoSupport}
209 * instance is the sum of the hash codes of all elements of
210 * information used in {@code equals} comparisons (ie: its name
211 * and signature, where the signature hashCode is calculated by a
212 * call to {@code
213 * java.util.Arrays.asList(this.getSignature).hashCode()}).</p>
214 *
215 * <p>This ensures that {@code t1.equals(t2)} implies that {@code
216 * t1.hashCode()==t2.hashCode()} for any two {@code
217 * OpenMBeanConstructorInfoSupport} instances {@code t1} and
218 * {@code t2}, as required by the general contract of the method
219 * {@link Object#hashCode() Object.hashCode()}.</p>
220 *
221 * <p>However, note that another instance of a class implementing
222 * the {@code OpenMBeanConstructorInfo} interface may be equal to
223 * this {@code OpenMBeanConstructorInfoSupport} instance as
224 * defined by {@link #equals(java.lang.Object)}, but may have a
225 * different hash code if it is calculated differently.</p>
226 *
227 * <p>As {@code OpenMBeanConstructorInfoSupport} instances are
228 * immutable, the hash code for this instance is calculated once,
229 * on the first call to {@code hashCode}, and then the same value
230 * is returned for subsequent calls.</p>
231 *
232 * @return the hash code value for this {@code
233 * OpenMBeanConstructorInfoSupport} instance
234 */
235 public int hashCode() {
236
237 // Calculate the hash code value if it has not yet been done
238 // (ie 1st call to hashCode())
239 //
240 if (myHashCode == null) {
241 int value = 0;
242 value += this .getName().hashCode();
243 value += Arrays.asList(this .getSignature()).hashCode();
244 myHashCode = new Integer(value);
245 }
246
247 // return always the same hash code for this instance (immutable)
248 //
249 return myHashCode.intValue();
250 }
251
252 /**
253 * <p>Returns a string representation of this {@code
254 * OpenMBeanConstructorInfoSupport} instance.</p>
255 *
256 * <p>The string representation consists of the name of this class
257 * (ie {@code
258 * javax.management.openmbean.OpenMBeanConstructorInfoSupport}),
259 * the name and signature of the described constructor and the
260 * string representation of its descriptor.</p>
261 *
262 * <p>As {@code OpenMBeanConstructorInfoSupport} instances are
263 * immutable, the string representation for this instance is
264 * calculated once, on the first call to {@code toString}, and
265 * then the same value is returned for subsequent calls.</p>
266 *
267 * @return a string representation of this {@code
268 * OpenMBeanConstructorInfoSupport} instance
269 */
270 public String toString() {
271
272 // Calculate the string value if it has not yet been done (ie
273 // 1st call to toString())
274 //
275 if (myToString == null) {
276 myToString = new StringBuilder().append(
277 this .getClass().getName()).append("(name=").append(
278 this .getName()).append(",signature=").append(
279 Arrays.asList(this .getSignature()).toString())
280 .append(",descriptor=")
281 .append(this .getDescriptor()).append(")")
282 .toString();
283 }
284
285 // return always the same string representation for this
286 // instance (immutable)
287 //
288 return myToString;
289 }
290
291 }
|