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

"NumberOfElevators",

"OfficeSpace"

};

// 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 = {

SimpleType.STRING,

SimpleType.SHORT,

SimpleType.INTEGER,

SimpleType.BOOLEAN,

SimpleType.SHORT,

SimpleType.LONG

};

CompositeType buildingType = new CompositeType(

"BuildingCompositeType",

"CompositeType that represents a Building.",

itemNames,

itemDescriptions,

itemTypes

);

} 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.

5.1.2.2 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();

}



155



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:

get()

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

specified key

getAll()

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

keys

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()

method:

public void someMethod(CompositeData buildingData) {

String[] attributeNames = {

"Name",

"Height",

"OfficeSpace"

};

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.



156



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

handy:

getCompositeType()

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.

containsKey()

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"))

doSomething();

}



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

public void someMethod(CompositeData buildingData) {

if (buildingData.containsKey("Width"))

doSomething();

}



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

containsValue()

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

value.

values()

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).

equals()

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



157



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

method returns false.

hashCode()

Returns a hashcode for this CompositeData object.

toString()

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

representation depends on the implementation.

5.1.2.3 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



158



Attribute name

Name

NumberOfFloors

Height

UndergroundParking

NumberOfElevators

OfficeSpace



Attribute value

"Fictitious Life Building"

3

45

false

1

10000



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 = {

"Name",

"NumberOfFloors",

"Height",

"UndergroundParking",

"NumberOfElevators",

"OfficeSpace"

};

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

itemNames,

itemValues

);

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));



159



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)

×