|
JavaTM 2 Platform Standard Ed. 6 |
|||||||||
| 上一個類別 下一個類別 | 框架 無框架 | |||||||||
| 摘要: 巢狀 | 欄位 | 建構子 | 方法 | 詳細資訊: 欄位 | 建構子 | 方法 | |||||||||
public interface CompositeDataView
Java 類別可以實作此介面,以指示如何通過 MXBean 框架將其轉換為 CompositeData。
使用此類別的典型方式是,除了已經在 MXBean 框架提供的 CompositeType 中宣告的項外,還要向 CompositeData 添加額外的項。要做到這一點,必須創建另一個 CompositeType,其中包含所有相同的項以及額外的項。
例如,假定您有一個由名為 units 的 String 和一個 value(可以是 long 或 double)組成的 Measure 類別。其形式可能如下所示:
public class Measure implements CompositeDataView {
private String units;
private Number value; // a Long or a Double
public Measure(String units, Number value) {
this.units = units;
this.value = value;
}
public static Measure from(CompositeData cd) {
return new Measure((String) cd.get("units"),
(Number) cd.get("value"));
}
public String getUnits() {
return units;
}
// Can't be called getValue(), because Number is not a valid type
// in an MXBean, so the implied "value" property would be rejected.
public Number _getValue() {
return value;
}
public CompositeData toCompositeData(CompositeType ct) {
try {
List<String> itemNames = new ArrayList<String>(ct.keySet());
List<String> itemDescriptions = new ArrayList<String>();
List<OpenType<?>> itemTypes = new ArrayList<OpenType<?>>();
for (String item :itemNames) {
itemDescriptions.add(ct.getDescription(item));
itemTypes.add(ct.getType(item));
}
itemNames.add("value");
itemDescriptions.add("long or double value of the measure");
itemTypes.add((value instanceof Long) ?SimpleType.LONG :
SimpleType.DOUBLE);
CompositeType xct =
new CompositeType(ct.getTypeName(),
ct.getDescription(),
itemNames.toArray(new String[0]),
itemDescriptions.toArray(new String[0]),
itemTypes.toArray(new OpenType<?>[0]));
CompositeData cd =
new CompositeDataSupport(xct,
new String[] {"units", "value"},
new Object[] {units, value});
assert ct.isValue(cd); // check we've done it right
return cd;
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}
將出現在用於此型別的屬性或操作的 Descriptor 的 openType 欄位中的 CompositeType 將只顯示 units 項,但產生的實際 CompositeData 將既包含 units 又包含 value。
MXBean| 方法摘要 | |
|---|---|
CompositeData |
toCompositeData(CompositeType ct)
返回對應於此物件中的值的 CompositeData。 |
| 方法詳細資訊 |
|---|
CompositeData toCompositeData(CompositeType ct)
返回對應於此物件中的值的 CompositeData。返回的值通常應該是一個 CompositeDataSupport 實例,或通過 writeReplace 方法序列化為一個 CompositeDataSupport 的類別。否則,接收物件的遠端客戶端可能無法重新建構它。
ct - 返回值的期望 CompositeType。如果返回值是 cd,則 cd.getCompositeType().equals(ct) 應該為 true。通常這是因為 cd 是一個通過將 ct 作為其 CompositeType 建構而成的 CompositeDataSupport。
CompositeData。
|
JavaTM 2 Platform Standard Ed. 6 |
|||||||||
| 上一個類別 下一個類別 | 框架 無框架 | |||||||||
| 摘要: 巢狀 | 欄位 | 建構子 | 方法 | 詳細資訊: 欄位 | 建構子 | 方法 | |||||||||
版權所有 2008 Sun Microsystems, Inc. 保留所有權利。請遵守GNU General Public License, version 2 only。