Tải bản đầy đủ - 0 (trang)
Example 5-1. Describing a complex data type using CompositeType

Example 5-1. Describing a complex data type using CompositeType

Tải bản đầy đủ - 0trang




// next describe the attribute descriptions

String[] itemDescriptions = {

"Name of the building",

"The number of floors (stories) the building has",

"The height of the building in feet",

"Whether or not the building has underground parking",

"The total number of elevators in the building",

"The amount of office space in square feet"


// next describe the data type of each item

OpenType[] itemTypes = {








CompositeType buildingType = new CompositeType(


"CompositeType that represents a Building.",





} catch (OpenDataException e) {

// . . .


What we have done, in essence, is to create an entirely new data type. Because we used

the open MBean class CompositeType to do it, the new type can be instrumented in an

application and be managed by a JMX-compliant management application. CompositeData

The CompositeData interface describes how to access the contents of a complex object.

This interface is defined as:

public interface CompositeData {

public CompositeType getCompositeType();

public Object get(String key);

public Object[] getAll(String[] keys);

public boolean containsKey(String key);

public boolean containsValue(Object value);

public Collection values();

public boolean equals(Object obj);

public int hashCode();

public String toString();



Whereas CompositeType describes one or more instances of CompositeData, the

CompositeData class itself contains values. Two methods are provided to access the

values inside a CompositeData object:


Retrieves the object (i.e., the value) of a complex data structure that has the

specified key


Retrieves an array of objects of a complex data structure that have the specified


Recall from our discussion of CompositeType that each member of a complex

structure—at a particular index—described by a CompositeType object is assigned a

name, located in the item names attribute at that index. That name is the key for retrieval.

In Example 5-1, we saw how to create a CompositeType object for a complex data type

called Building. Suppose that we need to manipulate an instance of a CompositeData

object that is based on BuildingCompositeType (the name given to the new type in

Example 5-1) and that this instance is passed to some method we have created. If we

needed to access the Name attribute, we would use the get() method:

public void someMethod(CompositeData buildingData) {

String buildingName = (String)buildingData.get("Name");

// . . .


By the same token, we could retrieve several attributes at once by using the getAll()


public void someMethod(CompositeData buildingData) {

String[] attributeNames = {





Object[] attributeValues = buildingData.getAll(attributeNames);

String name = (String)attributeValues[0];

int height = ((Integer)attributeValues[1]).intValue();

long officeSpace = ((Long)attributeValues[2]).longValue();

// . . .


The order of the values in the array is the same as the order in which the keys were

specified. Notice that we must unwrap any fundamental types (after casting them out of

the Object array) to get the Java primitive.


The get() and getAll() methods will probably be the ones you use most often to

manipulate CompositeData objects. However, the following methods may also come in



Retrieves the CompositeType object that describes this CompositeData instance.

This might be useful, for example, when you need to display descriptions of the

item names.


Returns a boolean indicating whether or not the specified key value is one of the

item names of the CompositeType. For example, the following code from our

Building example would return true:

public void someMethod(CompositeData buildingData) {

if (buildingData.containsKey("Height"))



and the doSomething() method would be invoked. This code would return false:

public void someMethod(CompositeData buildingData) {

if (buildingData.containsKey("Width"))



and doSomething() would not be executed, because Width is not a valid key.


Returns true if any value in the CompositeData object contains the specified



Returns an object that implements the Collection interface and contains all of

the values in the CompositeData object. The values are in the same order as their

corresponding keys (i.e., in alphabetical, ascending key order).


Tests the equality of this instance of a CompositeData object with another. For

this method to return true, the two CompositeData objects must be of the same

CompositeType, and all of their item values must be equal. If the specified object


does not implement the CompositeData interface, or is a null reference, this

method returns false.


Returns a hashcode for this CompositeData object.


Returns a string representation of this CompositeData object. The format of the

representation depends on the implementation. CompositeDataSupport

This class implements the CompositeData interface. If we need to create a

CompositeData object, we use one of this class's constructors:

public CompositeDataSupport(CompositeType compositeType,

String[] itemNames, Object[] itemValues)

throws OpenDataException {

// . . .


public CompositeDataSupport(CompositeType compositeType, Map items)

throws OpenDataException {

// . . .


The first constructor takes a CompositeType object, a String array of item names, and

an Object array of item values in the exact order of the item names. Each of the item

names in itemNames must be the same as one of the item names in the CompositeType

object, but the order does not have to be the same.

The RI checks the parameters to make sure that all of the following are true:

compositeType is not null.

itemNames and itemValues are not empty arrays or null, none of their elements

are empty strings or null, and itemNames and itemValues are of the same length.

itemNames and itemValues are both of the size specified in compositeType (as its

itemNames attribute).

Each of the elements in itemNames is one of the elements in the itemNames

attribute of compositeType and is of the correct type for that item name.

If the first two conditions are not true, an IllegalArgumentException will be thrown. If

either of the last two is not true, an OpenDataException will be thrown. Using our

Building example, suppose we wanted to create an instance of CompositeDataSupport

using the attribute values listed in Table 5-1.

Table 5-1. Attribute values for new CompositeDataSupport instance


Attribute name







Attribute value

"Fictitious Life Building"






Using these values, we can create an instance of CompositeDataSupport (as shown in

Example 5-2), using the buildingType CompositeType created in Example 5-1.

Example 5-2. Creating a CompositeData object using CompositeDataSupport

public CompositeData createCompositeDataObject() {

try {

String[] itemNames = {








Object[] itemValues = {

"Building A",

new Short(3),

new Integer(45),

new Boolean(false),

new Short(1),

new Long(10000)


CompositeData buildingData = new CompositeDataSupport(

buildingType, // See Example 5-1




return buildingData;

} catch (Exception e) {

// . . .



We can also create a CompositeData object through CompositeDataSupport's second

constructor, passing a Map object that contains the name/value pairs:

public CompositeData createCompositeDataObject() {

try {

Map items = new HashMap();

items.put("Name", "Building A");

items.put("NumberOfFloors", new Short(3));

items.put("Height", new Integer(45));

items.put("UndergroundParking", new Boolean(false));


Tài liệu bạn tìm kiếm đã sẵn sàng tải về

Example 5-1. Describing a complex data type using CompositeType

Tải bản đầy đủ ngay(0 tr)