Source for javax.management.MBeanNotificationInfo

   1: /* MBeanNotificationInfo.java -- Information about a bean's notification.
   2:    Copyright (C) 2006 Free Software Foundation, Inc.
   3: 
   4: This file is part of GNU Classpath.
   5: 
   6: GNU Classpath is free software; you can redistribute it and/or modify
   7: it under the terms of the GNU General Public License as published by
   8: the Free Software Foundation; either version 2, or (at your option)
   9: any later version.
  10: 
  11: GNU Classpath is distributed in the hope that it will be useful, but
  12: WITHOUT ANY WARRANTY; without even the implied warranty of
  13: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  14: General Public License for more details.
  15: 
  16: You should have received a copy of the GNU General Public License
  17: along with GNU Classpath; see the file COPYING.  If not, write to the
  18: Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  19: 02110-1301 USA.
  20: 
  21: Linking this library statically or dynamically with other modules is
  22: making a combined work based on this library.  Thus, the terms and
  23: conditions of the GNU General Public License cover the whole
  24: combination.
  25: 
  26: As a special exception, the copyright holders of this library give you
  27: permission to link this library with independent modules to produce an
  28: executable, regardless of the license terms of these independent
  29: modules, and to copy and distribute the resulting executable under
  30: terms of your choice, provided that you also meet, for each linked
  31: independent module, the terms and conditions of the license of that
  32: module.  An independent module is a module which is not derived from
  33: or based on this library.  If you modify this library, you may extend
  34: this exception to your version of the library, but you are not
  35: obligated to do so.  If you do not wish to do so, delete this
  36: exception statement from your version. */
  37: 
  38: package javax.management;
  39: 
  40: import java.util.Arrays;
  41: 
  42: /**
  43:  * <p>
  44:  * Describes the notifications emitted by a management bean.
  45:  * An instance of this class is specific to notifications
  46:  * involving a particular type of object.  A new instance
  47:  * should be created for each Java class used for notifications,
  48:  * and the Java class name forms the name of the instance.
  49:  * Each instance lists a number of notification types; these
  50:  * are not types in the sense of different Java classes, but
  51:  * instead form the names of notifications following the same
  52:  * syntax as Java property and package names.
  53:  * </p>
  54:  * <p>
  55:  * For instance, a management bean may emit two notifications
  56:  * containing {@link java.lang.String} objects.  Both would be described
  57:  * using one instance of this class, with a member of the array
  58:  * returned by {@link #getNotifTypes()} for each one.  If another
  59:  * notification containing a {@link java.util.Date} object were to
  60:  * be added, this would require a new instance of this class.
  61:  * </p>
  62:  * <p>
  63:  * The information in this class is immutable as standard.
  64:  * Of course, subclasses may change this, but this
  65:  * behaviour is not recommended.
  66:  * </p>
  67:  *
  68:  * @author Andrew John Hughes (gnu_andrew@member.fsf.org)
  69:  * @since 1.5
  70:  */
  71: public class MBeanNotificationInfo
  72:   extends MBeanFeatureInfo
  73:   implements Cloneable
  74: {
  75: 
  76:   /**
  77:    * Compatible with JDK 1.5
  78:    */
  79:   private static final long serialVersionUID = -3888371564530107064L;
  80: 
  81:   /**
  82:    * The types of notification described by this instance.
  83:    *
  84:    * @serial the types of notification.
  85:    */
  86:   private String[] types;
  87: 
  88:   /**
  89:    * Constructs a new {@link MBeanNotificationInfo} with the
  90:    * specified name, description and notification types. The
  91:    * notification types array may be <code>null</code> or of
  92:    * zero length, in order to indicate the absence of any types.
  93:    *
  94:    * @param types an array of {@link java.lang.String} objects,
  95:    *              containing the names of the notifications emitted
  96:    *              of this Java type.  The names use the dot notation
  97:    *              familiar from Java property and package names.
  98:    * @param name the name of the Java class the notifications described
  99:    *             by this object are instances of.
 100:    * @param description a description of the data.
 101:    * @throws IllegalArgumentException for some reason...
 102:    */
 103:   public MBeanNotificationInfo(String[] types, String name,
 104:                    String description)
 105:   {
 106:     super(name, description);
 107:     this.types = types;
 108:   }
 109: 
 110:   /**
 111:    * Returns a clone of this instance.  The clone is created
 112:    * using just the method provided by {@link java.lang.Object}.
 113:    * Thus, the clone is just a shallow clone as returned by
 114:    * that method, and does not contain any deeper cloning based
 115:    * on the subject of this class.
 116:    *
 117:    * @return a clone of this instance.
 118:    * @see java.lang.Cloneable
 119:    */
 120:   public Object clone()
 121:   {
 122:     try
 123:       {
 124:     return super.clone();
 125:       }
 126:     catch (CloneNotSupportedException e)
 127:       {
 128:     /* This shouldn't happen; we implement Cloneable */
 129:     throw new IllegalStateException("clone() called on " +
 130:                     "non-cloneable object.");
 131:       }
 132:   }
 133: 
 134:   /**
 135:    * Compares this feature with the supplied object.  This returns
 136:    * true iff the object is an instance of {@link
 137:    * MBeanNotificationInfo}, {@link Object#equals()} returns true for
 138:    * a comparison of both the name and description of this
 139:    * notification with that of the specified object, and the two
 140:    * notification type arrays contain the same elements in the same
 141:    * order (but one may be longer than the other).
 142:    *
 143:    * @param obj the object to compare.
 144:    * @return true if the object is a {@link MBeanNotificationInfo}
 145:    *         instance, 
 146:    *         <code>name.equals(object.getName())</code>,
 147:    *         <code>description.equals(object.getDescription())</code>
 148:    *         and the corresponding elements of the type arrays are
 149:    *         equal.
 150:    */
 151:   public boolean equals(Object obj)
 152:   {
 153:     if (obj instanceof MBeanNotificationInfo)
 154:       {
 155:     if (!(super.equals(obj)))
 156:       return false;
 157:     MBeanNotificationInfo o = (MBeanNotificationInfo) obj;
 158:     String[] oTypes = o.getNotifTypes();
 159:     for (int a = 0; a < types.length; ++a)
 160:       {
 161:         if (a == oTypes.length)
 162:           return true;
 163:         if (!(types[a].equals(oTypes[a])))
 164:           return false;
 165:       }
 166:     return true;
 167:       }
 168:     else
 169:       return false;
 170:   }
 171: 
 172:   /**
 173:    * Returns the notification types that the management bean may emit.
 174:    * The notification types are strings using the dot notation
 175:    * familiar from Java property and package names.  Changing the
 176:    * returned array does not affect the values retained by this
 177:    * instance.
 178:    *
 179:    * @return the notification types.
 180:    */
 181:   public String[] getNotifTypes()
 182:   {
 183:     return types;
 184:   }
 185: 
 186:   /**
 187:    * Returns the hashcode of the notification information as the sum
 188:    * of the hashcode of the superclass and the hashcode of the types
 189:    * array.
 190:    *
 191:    * @return the hashcode of the notification information.
 192:    */
 193:   public int hashCode()
 194:   {
 195:     return super.hashCode() + Arrays.hashCode(types);
 196:   }
 197: 
 198:   /**
 199:    * <p>
 200:    * Returns a textual representation of this instance.  This
 201:    * is constructed using the class name
 202:    * (<code>javax.management.MBeanNotificationInfo</code>),
 203:    * the name and description of the notification and the 
 204:    * contents of the array of types.
 205:    * </p>
 206:    * <p>
 207:    * As instances of this class are immutable, the return value
 208:    * is computed just once for each instance and reused
 209:    * throughout its life.
 210:    * </p>
 211:    *
 212:    * @return a @link{java.lang.String} instance representing
 213:    *         the instance in textual form.
 214:    */
 215:   public String toString()
 216:   {
 217:     if (string == null)
 218:       {
 219:     super.toString();
 220:     string = string.substring(0, string.length() - 1) 
 221:       + ",types=" + Arrays.toString(types)
 222:       + "]";
 223:       }
 224:     return string;
 225:   }
 226: 
 227: }