CompositeTypepublic class CompositeType extends OpenType implements SerializableThe CompositeType class is the open type class
whose instances describe the types of {@link CompositeData CompositeData } values. |
Fields Summary |
---|
static final long | serialVersionUID | private TreeMap | nameToDescription | private TreeMap | nameToType | private transient Integer | myHashCode | private transient String | myToString | private transient Set | myNamesSet |
Constructors Summary |
---|
public CompositeType(String typeName, String description, String[] itemNames, String[] itemDescriptions, OpenType[] itemTypes)Constructs a CompositeType instance, checking for the validity of the given parameters.
The validity constraints are described below for each parameter.
Note that the contents of the three array parameters
itemNames, itemDescriptions and itemTypes
are internally copied so that any subsequent modification of these arrays by the caller of this constructor
has no impact on the constructed CompositeType instance.
The Java class name of composite data values this composite type represents
(ie the class name returned by the {@link OpenType#getClassName() getClassName} method)
is set to the string value returned by CompositeData.class.getName() .
// need only be calculated once.
/* *** Constructor *** */
// Check and construct state defined by parent
//
super(CompositeData.class.getName(), typeName, description);
// Check the 3 arrays are not null or empty (ie length==0) and that there is no null element or empty string in them
//
checkForNullElement(itemNames, "itemNames");
checkForNullElement(itemDescriptions, "itemDescriptions");
checkForNullElement(itemTypes, "itemTypes");
checkForEmptyString(itemNames, "itemNames");
checkForEmptyString(itemDescriptions, "itemDescriptions");
// Check the sizes of the 3 arrays are the same
//
if ( (itemNames.length != itemDescriptions.length) || (itemNames.length != itemTypes.length) ) {
throw new IllegalArgumentException("Array arguments itemNames[], itemDescriptions[] and itemTypes[] "+
"should be of same length (got "+ itemNames.length +", "+
itemDescriptions.length +" and "+ itemTypes.length +").");
}
// Initialize internal "names to descriptions" and "names to types" sorted maps,
// and, by doing so, check there are no duplicate item names
//
nameToDescription = new TreeMap();
nameToType = new TreeMap();
String key;
for (int i=0; i<itemNames.length; i++) {
key = itemNames[i].trim();
if (nameToDescription.containsKey(key)) {
throw new OpenDataException("Argument's element itemNames["+ i +"]=\""+ itemNames[i] +
"\" duplicates a previous item names.");
}
nameToDescription.put(key, itemDescriptions[i].trim());
nameToType.put(key, itemTypes[i]);
}
|
Methods Summary |
---|
private static void | checkForEmptyString(java.lang.String[] arg, java.lang.String argName)
for (int i=0; i<arg.length; i++) {
if (arg[i].trim().equals("")) {
throw new IllegalArgumentException("Argument's element "+ argName +"["+ i +"] cannot be an empty string.");
}
}
| private static void | checkForNullElement(java.lang.Object[] arg, java.lang.String argName)
if ( (arg == null) || (arg.length == 0) ) {
throw new IllegalArgumentException("Argument "+ argName +"[] cannot be null or empty.");
}
for (int i=0; i<arg.length; i++) {
if (arg[i] == null) {
throw new IllegalArgumentException("Argument's element "+ argName +"["+ i +"] cannot be null.");
}
}
| public boolean | containsKey(java.lang.String itemName)Returns true if this CompositeType instance defines an item
whose name is itemName.
if (itemName == null) {
return false;
}
return nameToDescription.containsKey(itemName);
| public boolean | equals(java.lang.Object obj)Compares the specified obj parameter with this CompositeType instance for equality.
Two CompositeType instances are equal if and only if all of the following statements are true:
- their type names are equal
- their items' names and types are equal
// if obj is null, return false
//
if (obj == null) {
return false;
}
// if obj is not a CompositeType, return false
//
CompositeType other;
try {
other = (CompositeType) obj;
} catch (ClassCastException e) {
return false;
}
// Now, really test for equality between this CompositeType instance and the other
//
// their names should be equal
if ( ! this.getTypeName().equals(other.getTypeName()) ) {
return false;
}
// their items names and types should be equal
if ( ! this.nameToType.equals(other.nameToType) ) {
return false;
}
// All tests for equality were successfull
//
return true;
| public java.lang.String | getDescription(java.lang.String itemName)Returns the description of the item whose name is itemName,
or null if this CompositeType instance does not define any item
whose name is itemName.
if (itemName == null) {
return null;
}
return (String) nameToDescription.get(itemName);
| public javax.management.openmbean.OpenType | getType(java.lang.String itemName)Returns the open type of the item whose name is itemName,
or null if this CompositeType instance does not define any item
whose name is itemName.
if (itemName == null) {
return null;
}
return (OpenType) nameToType.get(itemName);
| public int | hashCode()Returns the hash code value for this CompositeType instance.
The hash code of a CompositeType instance is the sum of the hash codes
of all elements of information used in equals comparisons
(ie: name, items names, items types).
This ensures that t1.equals(t2) implies that t1.hashCode()==t2.hashCode()
for any two CompositeType instances t1 and t2 ,
as required by the general contract of the method
{@link Object#hashCode() Object.hashCode()}.
As CompositeType instances are immutable, the hash code for this instance is calculated once,
on the first call to hashCode , and then the same value is returned for subsequent calls.
// Calculate the hash code value if it has not yet been done (ie 1st call to hashCode())
//
if (myHashCode == null) {
int value = 0;
value += this.getTypeName().hashCode();
String key;
for (Iterator k = nameToDescription.keySet().iterator(); k.hasNext(); ) {
key = (String) k.next();
value += key.hashCode();
value += this.nameToType.get(key).hashCode();
}
myHashCode = new Integer(value);
}
// return always the same hash code for this instance (immutable)
//
return myHashCode.intValue();
| public boolean | isValue(java.lang.Object obj)Tests whether obj is a value which could be described by this CompositeType instance.
If obj is null or is not an instance of javax.management.openmbean.CompositeData ,
isValue returns false .
If obj is an instance of javax.management.openmbean.CompositeData ,
its composite type is tested for equality with this CompositeType instance, and isValue
returns true if and only if {@link #equals(java.lang.Object) equals }
returns true .
// if obj is null, return false
//
if (obj == null) {
return false;
}
// if obj is not a CompositeData, return false
//
CompositeData value;
try {
value = (CompositeData) obj;
} catch (ClassCastException e) {
return false;
}
// test value's CompositeType for equality with this CompositeType instance
//
return this.equals(value.getCompositeType());
| public java.util.Set | keySet()Returns an unmodifiable Set view of all the item names defined by this CompositeType instance.
The set's iterator will return the item names in ascending order.
// Initializes myNamesSet on first call
if (myNamesSet == null) {
myNamesSet = Collections.unmodifiableSet(nameToDescription.keySet());
}
return myNamesSet; // always return the same value
| public java.lang.String | toString()Returns a string representation of this CompositeType instance.
The string representation consists of
the name of this class (ie javax.management.openmbean.CompositeType ), the type name for this instance,
and the list of the items names and types string representation of this instance.
As CompositeType instances are immutable, the string representation for this instance is calculated once,
on the first call to toString , and then the same value is returned for subsequent calls.
// Calculate the string representation if it has not yet been done (ie 1st call to toString())
//
if (myToString == null) {
StringBuffer result = new StringBuffer();
result.append(this.getClass().getName());
result.append("(name=");
result.append(getTypeName());
result.append(",items=(");
int i=0;
Iterator k=nameToType.keySet().iterator();
String key;
while (k.hasNext()) {
key = (String) k.next();
if (i > 0) result.append(",");
result.append("(itemName=");
result.append(key);
result.append(",itemType=");
result.append(nameToType.get(key).toString() +")");
i++;
}
result.append("))");
myToString = result.toString();
}
// return always the same string representation for this instance (immutable)
//
return myToString;
|
|