Source for javax.management.openmbean.OpenType

   1: /* OpenType.java -- Superclass of all open types.
   2:    Copyright (C) 2006, 2007 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.openmbean;
  39: 
  40: import java.io.Serializable;
  41: 
  42: import java.util.Arrays;
  43: import java.util.List;
  44: 
  45: /**
  46:  * The superclass of all open types, which describe the
  47:  * applicable data values for open MBeans.  An open type
  48:  * is defined by its name and description, and the name
  49:  * of the Java class it maps to.
  50:  *
  51:  * @author Andrew John Hughes (gnu_andrew@member.fsf.org)
  52:  * @since 1.5
  53:  */
  54: public abstract class OpenType<T>
  55:   implements Serializable
  56: {
  57: 
  58:   /**
  59:    * Compatible with JDK 1.5
  60:    */
  61:   private static final long serialVersionUID = -9195195325186646468L;
  62: 
  63:   /**
  64:    * The name of the Java class this type represents.
  65:    */
  66:   private String className;
  67: 
  68:   /**
  69:    * The name of this type.
  70:    */
  71:   private String typeName;
  72: 
  73:   /**
  74:    * A description of this type.
  75:    */
  76:   private String description;
  77: 
  78:   /**
  79:    * An array which defines the set of Java types which can be
  80:    * used as open types.  Note that each type is also available
  81:    * in array form, possibly with multiple dimensions.
  82:    *
  83:    * @deprecated Use {@link ALLOWED_CLASSNAMES_LIST} instead.
  84:    */
  85:   @Deprecated
  86:   public static final String[] ALLOWED_CLASSNAMES = {
  87:     "java.lang.Void",
  88:     "java.lang.Boolean",
  89:     "java.lang.Character",
  90:     "java.lang.Byte",
  91:     "java.lang.Short",
  92:     "java.lang.Integer",
  93:     "java.lang.Long",
  94:     "java.lang.Float",
  95:     "java.lang.Double",
  96:     "java.lang.String",
  97:     "java.math.BigDecimal",
  98:     "java.math.BigInteger",
  99:     "java.util.Date",
 100:     "javax.management.ObjectName",
 101:     CompositeData.class.getName(),
 102:     TabularData.class.getName()
 103:   };
 104: 
 105:   /**
 106:    * A list which defines the set of Java types that may be
 107:    * used as open types.  Note that each type is also available
 108:    * in array form, possibly with multiple dimensions.
 109:    */
 110:   public static final List<String> ALLOWED_CLASSNAMES_LIST =
 111:     Arrays.asList(ALLOWED_CLASSNAMES);
 112: 
 113:   /**
 114:    * Constructs a new {@link OpenType} for the specified class
 115:    * with the given name and description.  The name of the class
 116:    * must be taken from the list of {@link ALLOWED_CLASSNAMES}.
 117:    * Arrays are implictly included in this, and follow the usual
 118:    * syntax of {@link java.lang.Class#getName()} with the name
 119:    * preceded by n instances of '[' (where n is the number of
 120:    * dimensions) and an L.  The name and description can not be
 121:    * <code>null</code> or the empty string.
 122:    *
 123:    * @param className the name of the Java class this type
 124:    *                  represents.
 125:    * @param name the name of the type.
 126:    * @param desc the description of the type.
 127:    * @throws IllegalArgumentException if either of <code>name</code>
 128:    *                                  or <code>desc</code> are
 129:    *                                  <code>null</code> or the empty
 130:    *                                  string.
 131:    * @throws OpenDataException if the class name does not reference
 132:    *                           a listed class (from @{link ALLOWED_CLASSNAMES})
 133:    */
 134:   protected OpenType(String className, String name, String desc)
 135:     throws OpenDataException
 136:   {
 137:     if (name == null || name.equals(""))
 138:       throw new IllegalArgumentException("The name can not be null " +
 139:                                          "or the empty string.");
 140:     if (desc == null || desc.equals(""))
 141:       throw new IllegalArgumentException("The description can not " +
 142:                                          "be null or the empty string.");
 143:     Class<?> type;
 144:     try
 145:       {
 146:         type = Class.forName(className);
 147:       }
 148:     catch (ClassNotFoundException e)
 149:       {
 150:         throw (OpenDataException) new OpenDataException("The class name, " + className +
 151:                                                         ", is unavailable.").initCause(e);
 152:       }
 153:     while (type.isArray())
 154:       type = type.getComponentType();
 155:     if (!(type.isPrimitive() || ALLOWED_CLASSNAMES_LIST.contains(type.getName())))
 156:       throw new OpenDataException("The class name, " + className +
 157:                                   ", does not specify a valid open type.");
 158:     this.className = className;
 159:     typeName = name;
 160:     description = desc;
 161:   }
 162: 
 163:   /**
 164:    * Performs an equality test on this object and the one specified.
 165:    *
 166:    * @param obj the object to test against this one.
 167:    * @return true if the two objects are equivalent.
 168:    * @see java.lang.Object#hashCode()
 169:    */
 170:   public abstract boolean equals(Object obj);
 171: 
 172:   /**
 173:    * Returns the name of the Java class this type represents.  This must
 174:    * be one of the {@link ALLOWED_CLASSNAMES} or an array of one of them.
 175:    * The specification of arrays follows the standard set by
 176:    * {@link java.lang.Class#getName()} i.e. the name is the class name
 177:    * preceded by n instances of '[' and an 'L', where n is number of
 178:    * dimensions used by the array.
 179:    *
 180:    * @return the class name.
 181:    */
 182:   public String getClassName()
 183:   {
 184:     return className;
 185:   }
 186: 
 187:   /**
 188:    * Returns a description of this open type.
 189:    *
 190:    * @return the description.
 191:    */
 192:   public String getDescription()
 193:   {
 194:     return description;
 195:   }
 196: 
 197:   /**
 198:    * Returns the name of this open type.
 199:    *
 200:    * @return the type name.
 201:    */
 202:   public String getTypeName()
 203:   {
 204:     return typeName;
 205:   }
 206: 
 207:   /**
 208:    * Returns a hash code for this open type.  The hash code
 209:    * should be consistent with the {@link equals()} method.
 210:    * Thus, it should continue to return the same value while
 211:    * the values used by the {@link equals()} method remain
 212:    * the same, and should return different hash codes for
 213:    * objects which are judged to be different using the
 214:    * {@link equals()} method.
 215:    *
 216:    * @return the hash code of this instance.
 217:    */
 218:   public abstract int hashCode();
 219: 
 220:   /**
 221:    * Returns true if this open type represents an array type.
 222:    *
 223:    * @return true if this open type represents an array type.
 224:    */
 225:   public boolean isArray()
 226:   {
 227:     return className.startsWith("[");
 228:   }
 229: 
 230:   /**
 231:    * Returns true if the specified object is a member of this
 232:    * type.
 233:    *
 234:    * @param obj the object to test for membership.
 235:    * @return true if the object is a member of this type.
 236:    */
 237:   public abstract boolean isValue(Object obj);
 238: 
 239:   /**
 240:    * Returns a textual representation of this type.
 241:    *
 242:    * @return a {@link java.lang.String} representation of this
 243:    *         type.
 244:    */
 245:   public abstract String toString();
 246: 
 247: }