001: /*
002: * JBoss, Home of Professional Open Source.
003: * Copyright 2006, Red Hat Middleware LLC, and individual contributors
004: * as indicated by the @author tags. See the copyright.txt file in the
005: * distribution for a full listing of individual contributors.
006: *
007: * This is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU Lesser General Public License as
009: * published by the Free Software Foundation; either version 2.1 of
010: * the License, or (at your option) any later version.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this software; if not, write to the Free
019: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021: */
022: package javax.management.openmbean;
023:
024: import java.util.ArrayList;
025: import java.util.Collections;
026: import java.util.Iterator;
027: import java.util.List;
028: import java.io.Serializable;
029:
030: /**
031: * The TabularType is an OpenType that describes TabularData.
032: *
033: * @see TabularData
034: *
035: * @author <a href="mailto:Adrian.Brock@HappeningTimes.com">Adrian Brock</a>.
036: *
037: * @version $Revision: 57200 $
038: *
039: */
040: public class TabularType extends OpenType implements Serializable {
041: // Attributes ----------------------------------------------------
042:
043: /**
044: * The open type of the rows
045: */
046: private CompositeType rowType;
047:
048: /**
049: * Index names
050: */
051: private List indexNames;
052:
053: /**
054: * Cached hash code
055: */
056: private transient int cachedHashCode = 0;
057:
058: /**
059: * Cached string representation
060: */
061: private transient String cachedToString = null;
062:
063: // Static --------------------------------------------------------
064:
065: private static final long serialVersionUID = 6554071860220659261L;
066:
067: // Constructors --------------------------------------------------
068:
069: /**
070: * Construct a tabular type. The parameters are checked for validity.<p>
071: *
072: * getClassName() returns javax.management.openbean.TabularData<p>
073: *
074: * @param typeName the name of the tabular type, cannot be null or
075: * empty
076: * @param description the human readable description of the tabular type,
077: * cannot be null or empty
078: * @param rowType the type of the row elements in the tabular data, cannot
079: * be null
080: * @param indexNames the names of the item values that uniquely index each
081: * row element in the tabular data, cannot be null or empty. Each
082: * element must be an item name in the rowType, nul or empty is not
083: * allowed. The order of the item names in this parameter is used
084: * by {@link TabularData#get} and {@link TabularData#remove} the
085: * TabularData to match the array of values to items.
086: * @exception OpenDataException when an element of indexNames is not defined
087: * in rowType.
088: * @exception IllegalArgumentException when a parameter does not match
089: * what is described above.
090: */
091: public TabularType(String typeName, String description,
092: CompositeType rowType, String[] indexNames)
093: throws OpenDataException {
094: super (TabularData.class.getName(), typeName, description);
095: if (rowType == null)
096: throw new IllegalArgumentException("null rowType");
097: if (indexNames == null || indexNames.length == 0)
098: throw new IllegalArgumentException(
099: "null or empty indexNames");
100: this .rowType = rowType;
101: this .indexNames = new ArrayList();
102: for (int i = 0; i < indexNames.length; i++) {
103: if (indexNames[i] == null)
104: throw new IllegalArgumentException("null index name "
105: + i);
106: String indexName = indexNames[i].trim();
107: if (indexName.length() == 0)
108: throw new IllegalArgumentException("empty index name "
109: + i);
110: if (rowType.containsKey(indexName) == false)
111: throw new OpenDataException("no item name " + indexName);
112: this .indexNames.add(indexName);
113: }
114: }
115:
116: // Public --------------------------------------------------------
117:
118: /**
119: * Retrieve the row type
120: *
121: * @return the row type
122: */
123: public CompositeType getRowType() {
124: return rowType;
125: }
126:
127: /**
128: * Retrieve an unmodifiable list of index names in the same order as
129: * passed to the constructor.
130: *
131: * @return the index names
132: */
133: public List getIndexNames() {
134: return Collections.unmodifiableList(indexNames);
135: }
136:
137: // OpenType Overrides --------------------------------------------
138:
139: /**
140: * Determines whether the object is a value of the this tabular type.<p>
141: *
142: * The object must not be null and it must be an instance of
143: * javax.management.openbean.TabularData. The TabularType of the
144: * TabularData have equality with this TabularType.
145: *
146: * @param obj the object to test
147: * @return the true when the above condition is satisfied, false otherwise
148: */
149: public boolean isValue(Object obj) {
150: if (obj == null || !(obj instanceof TabularData))
151: return false;
152: TabularType other = ((TabularData) obj).getTabularType();
153: return equals(other);
154: }
155:
156: /**
157: * Tests for equality with another composite type<p>
158: *
159: * The type names must be equal.<br>
160: * The row types are equal<br>
161: * The index names are the same and in the same order.
162: *
163: * @param obj the other tabular type to test
164: * @return the true when the above condition is satisfied, false otherwise
165: */
166: public boolean equals(Object obj) {
167: if (obj == null || (obj instanceof TabularType) == false)
168: return false;
169: if (this == obj)
170: return true;
171:
172: TabularType other = (TabularType) obj;
173: if (this .getTypeName().equals(other.getTypeName()) == false)
174: return false;
175: if (this .getRowType().equals(other.getRowType()) == false)
176: return false;
177: Iterator this Names = this .getIndexNames().iterator();
178: Iterator otherNames = other.getIndexNames().iterator();
179: while (this Names.hasNext() && otherNames.hasNext()) {
180: String this Name = (String) this Names.next();
181: String otherName = (String) otherNames.next();
182: if (this Name.equals(otherName) == false)
183: return false;
184: }
185: if (this Names.hasNext() || otherNames.hasNext())
186: return false;
187:
188: return true;
189: }
190:
191: public int hashCode() {
192: if (cachedHashCode != 0)
193: return cachedHashCode;
194: cachedHashCode = getTypeName().hashCode();
195: cachedHashCode += getRowType().hashCode();
196: for (Iterator i = indexNames.iterator(); i.hasNext();)
197: cachedHashCode += i.next().hashCode();
198: return cachedHashCode;
199: }
200:
201: public String toString() {
202: if (cachedToString != null)
203: return cachedToString;
204: StringBuffer buffer = new StringBuffer(getClass().getName());
205: buffer.append(": typeName=[");
206: buffer.append(getTypeName());
207: buffer.append("] rowType=[");
208: buffer.append(getRowType());
209: buffer.append("] indexNames=[");
210: Iterator this Names = getIndexNames().iterator();
211: while (this Names.hasNext()) {
212: buffer.append(this Names.next());
213: if (this Names.hasNext())
214: buffer.append(", ");
215: }
216: buffer.append("]");
217: cachedToString = buffer.toString();
218: return cachedToString;
219: }
220: }
|