001: /**
002: * Copyright (C) The MX4J Contributors.
003: * All rights reserved.
004: *
005: * This software is distributed under the terms of the MX4J License version 1.0.
006: * See the terms of the MX4J License in the documentation provided with this software.
007: */package javax.management.openmbean;
008:
009: import java.io.Serializable;
010: import java.util.ArrayList;
011: import java.util.Collections;
012: import java.util.List;
013:
014: /**
015: * @version $Revision: 1.17 $
016: */
017: public class TabularType extends OpenType implements Serializable {
018: private static final long serialVersionUID = 6554071860220659261L;
019:
020: private CompositeType rowType = null;
021: private List indexNames = null;
022:
023: private transient int m_hashcode = 0;
024: private transient String m_classDescription = null;
025:
026: public TabularType(String typeName, String description,
027: CompositeType rowType, String[] indexNames)
028: throws OpenDataException {
029: super (TabularData.class.getName(), typeName, description);
030: if (typeName.trim().length() == 0)
031: throw new IllegalArgumentException(
032: "TabularType name can't be empty");
033: if (description.trim().length() == 0)
034: throw new IllegalArgumentException(
035: "TabularType description can't be empty");
036: validate(rowType, indexNames);
037: this .rowType = rowType;
038: ArrayList temp = new ArrayList();
039: for (int i = 0; i < indexNames.length; ++i)
040: temp.add(indexNames[i]);
041: this .indexNames = Collections.unmodifiableList(temp);
042: }
043:
044: /**
045: * Checks if all of the values of indexNames match items defined in rowType, that the item is not null or zero length
046: * we then create an unmodifiable list from the "valid" string array.
047: * If any validity checks fail an OpenDataException is thrown
048: */
049: private void validate(CompositeType rowType, String[] indexNames)
050: throws OpenDataException {
051: if (rowType == null)
052: throw new IllegalArgumentException(
053: "The CompositeType passed in cannot be null");
054: if (indexNames == null || indexNames.length == 0)
055: throw new IllegalArgumentException(
056: "The String[] indexNames cannot be null or empty");
057:
058: for (int i = 0; i < indexNames.length; i++) {
059: String item = indexNames[i];
060: if (item == null || item.length() == 0) {
061: throw new IllegalArgumentException(
062: "An Item in the indexNames[] cannot be null or of zero length");
063: }
064: if (!(rowType.containsKey(item))) {
065: throw new OpenDataException("Element value: "
066: + indexNames[i] + " at index: " + i
067: + " is not a valid item name for RowType");
068: }
069: }
070: }
071:
072: public CompositeType getRowType() {
073: return rowType;
074: }
075:
076: public List getIndexNames() {
077: return indexNames;
078: }
079:
080: public boolean isValue(Object object) {
081: if (!(object instanceof TabularData))
082: return false;
083: TabularData tabularData = (TabularData) object;
084: return equals(tabularData.getTabularType());
085: }
086:
087: public boolean equals(Object object) {
088: if (object == this )
089: return true;
090: if (!(object instanceof TabularType))
091: return false;
092: TabularType tabularType = (TabularType) object;
093: return getRowType().equals(tabularType.getRowType())
094: && getIndexNames().equals(tabularType.getIndexNames())
095: && getTypeName().equals(tabularType.getTypeName());
096: }
097:
098: public int hashCode() {
099: if (m_hashcode == 0) {
100: int result = getTypeName().hashCode();
101: result += getRowType().hashCode();
102: List names = getIndexNames();
103: for (int i = 0; i < names.size(); ++i) {
104: Object name = names.get(i);
105: result += name.hashCode();
106: }
107: m_hashcode = result;
108: }
109: return m_hashcode;
110: }
111:
112: public String toString() {
113: if (m_classDescription == null) {
114: StringBuffer classString = new StringBuffer(
115: "TabularType name: ").append(getTypeName());
116: classString.append(" rowType: ").append(getRowType());
117: classString.append("indexNames: ").append(getIndexNames());
118: m_classDescription = classString.toString();
119: }
120: return m_classDescription;
121: }
122: }
|