001    /* OpenMBeanInfoSupport.java -- Open typed info about a bean.
002       Copyright (C) 2006 Free Software Foundation, Inc.
003    
004    This file is part of GNU Classpath.
005    
006    GNU Classpath is free software; you can redistribute it and/or modify
007    it under the terms of the GNU General Public License as published by
008    the Free Software Foundation; either version 2, or (at your option)
009    any later version.
010    
011    GNU Classpath is distributed in the hope that it will be useful, but
012    WITHOUT ANY WARRANTY; without even the implied warranty of
013    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
014    General Public License for more details.
015    
016    You should have received a copy of the GNU General Public License
017    along with GNU Classpath; see the file COPYING.  If not, write to the
018    Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
019    02110-1301 USA.
020    
021    Linking this library statically or dynamically with other modules is
022    making a combined work based on this library.  Thus, the terms and
023    conditions of the GNU General Public License cover the whole
024    combination.
025    
026    As a special exception, the copyright holders of this library give you
027    permission to link this library with independent modules to produce an
028    executable, regardless of the license terms of these independent
029    modules, and to copy and distribute the resulting executable under
030    terms of your choice, provided that you also meet, for each linked
031    independent module, the terms and conditions of the license of that
032    module.  An independent module is a module which is not derived from
033    or based on this library.  If you modify this library, you may extend
034    this exception to your version of the library, but you are not
035    obligated to do so.  If you do not wish to do so, delete this
036    exception statement from your version. */
037    
038    package javax.management.openmbean;
039    
040    import java.util.Arrays;
041    import java.util.HashSet;
042    
043    import javax.management.MBeanInfo;
044    import javax.management.MBeanAttributeInfo;
045    import javax.management.MBeanConstructorInfo;
046    import javax.management.MBeanNotificationInfo;
047    import javax.management.MBeanOperationInfo;
048    
049    /**
050     * Describes an open management bean.
051     *
052     * @author Andrew John Hughes (gnu_andrew@member.fsf.org)
053     * @since 1.5
054     */
055    public class OpenMBeanInfoSupport
056      extends MBeanInfo
057      implements OpenMBeanInfo
058    {
059    
060      /**
061       * Compatible with JDK 1.5
062       */
063      private static final long serialVersionUID = 4349395935420511492L;
064    
065      /**
066       * The hash code of this instance.
067       */
068      private transient Integer hashCode;
069    
070      /**
071       * The <code>toString()</code> result of this instance.
072       */
073      private transient String string;
074    
075      /**
076       * Constructs a new {@link OpenMBeanInfo} using the supplied
077       * class name and description with the given attributes,
078       * operations, constructors and notifications.  The class
079       * name does not have to actually specify a valid class that
080       * can be loaded by the MBean server or class loader; it merely
081       * has to be a syntactically correct class name.  Any of the
082       * arrays may be <code>null</code>; this will be treated as if
083       * an empty array was supplied.  A copy of the arrays is
084       * taken, so later changes have no effect.
085       *
086       * @param name the name of the class this instance describes.
087       * @param desc a description of the bean.
088       * @param attribs the attribute descriptions for the bean,
089       *                or <code>null</code>.
090       * @param cons the constructor descriptions for the bean,
091       *             or <code>null</code>.
092       * @param ops the operation descriptions for the bean,
093       *            or <code>null</code>.
094       * @param notifs the notification descriptions for the bean,
095       *               or <code>null</code>.
096       * @throws ArrayStoreException if a members of an array
097       *                             is not assignable to the equivalent
098       *                             <code>MBeanXXXInfo</code> class.
099       */
100      public OpenMBeanInfoSupport(String name, String desc,
101                                  OpenMBeanAttributeInfo[] attribs,
102                                  OpenMBeanConstructorInfo[] cons,
103                                  OpenMBeanOperationInfo[] ops,
104                                  MBeanNotificationInfo[] notifs)
105      {
106        super(name, desc, (MBeanAttributeInfo[]) attribs,
107              (MBeanConstructorInfo[]) cons,
108              (MBeanOperationInfo[]) ops,
109              notifs);
110      }
111    
112      /**
113       * Compares this attribute with the supplied object.  This returns
114       * true iff the object is an instance of {@link OpenMBeanInfo}
115       * with the same class name and equal instances of the info classes.
116       *
117       * @param obj the object to compare.
118       * @return true if the object is a {@link OpenMBeanInfo}
119       *         instance,
120       *         <code>className.equals(object.getClassName())</code>
121       *         and each info class has an equal in the other object.
122       */
123      public boolean equals(Object obj)
124      {
125        if (!(obj instanceof OpenMBeanInfo))
126          return false;
127        OpenMBeanInfo o = (OpenMBeanInfo) obj;
128        return getClassName().equals(o.getClassName()) &&
129          getAttributes().equals(o.getAttributes()) &&
130          getConstructors().equals(o.getConstructors()) &&
131          getNotifications().equals(o.getNotifications()) &&
132          getOperations().equals(o.getOperations());
133      }
134    
135      /**
136       * <p>
137       * Returns the hashcode of the bean information as the sum of the
138       * hashcodes of the class name and each array (calculated using
139       * java.util.HashSet(<code>java.util.Arrays.asList(signature)).hashCode()</code>).
140       * </p>
141       * <p>
142       * As instances of this class are immutable, the return value
143       * is computed just once for each instance and reused
144       * throughout its life.
145       * </p>
146       *
147       * @return the hashcode of the bean information.
148       */
149      public int hashCode()
150      {
151        if (hashCode == null)
152          hashCode =
153            Integer.valueOf(getClassName().hashCode() +
154                            new HashSet<MBeanAttributeInfo>(Arrays.asList(getAttributes())).hashCode() +
155                            new HashSet<MBeanConstructorInfo>(Arrays.asList(getConstructors())).hashCode() +
156                            new HashSet<MBeanNotificationInfo>(Arrays.asList(getNotifications())).hashCode() +
157                            new HashSet<MBeanOperationInfo>(Arrays.asList(getOperations())).hashCode());
158        return hashCode.intValue();
159      }
160    
161      /**
162       * <p>
163       * Returns a textual representation of this instance.  This
164       * is constructed using the class name
165       * (<code>javax.management.openmbean.OpenMBeanInfo</code>)
166       * along with the class name and textual representations
167       * of each array.
168       * </p>
169       * <p>
170       * As instances of this class are immutable, the return value
171       * is computed just once for each instance and reused
172       * throughout its life.
173       * </p>
174       *
175       * @return a @link{java.lang.String} instance representing
176       *         the instance in textual form.
177       */
178      public String toString()
179      {
180        if (string == null)
181          string = getClass().getName()
182            + "[className=" + getClassName()
183            + ",attributes=" + Arrays.toString(getAttributes())
184            + ",constructors=" + Arrays.toString(getConstructors())
185            + ",notifications=" + Arrays.toString(getNotifications())
186            + ",operations=" + Arrays.toString(getOperations())
187            + "]";
188        return string;
189      }
190    
191    }