package javax.management.openmbean;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.io.Serializable;
public class TabularType
extends OpenType
implements Serializable
{
private CompositeType rowType;
private List indexNames;
private transient int cachedHashCode = 0;
private transient String cachedToString = null;
private static final long serialVersionUID = 6554071860220659261L;
public TabularType(String typeName, String description,
CompositeType rowType, String[] indexNames)
throws OpenDataException
{
super(TabularData.class.getName(), typeName, description);
if (rowType == null)
throw new IllegalArgumentException("null rowType");
if (indexNames == null || indexNames.length == 0)
throw new IllegalArgumentException("null or empty indexNames");
this.rowType = rowType;
this.indexNames = new ArrayList();
for (int i = 0; i < indexNames.length; i++)
{
if (indexNames[i] == null)
throw new IllegalArgumentException("null index name " + i);
String indexName = indexNames[i].trim();
if (indexName.length() == 0)
throw new IllegalArgumentException("empty index name " + i);
if (rowType.containsKey(indexName) == false)
throw new OpenDataException("no item name " + indexName);
this.indexNames.add(indexName);
}
}
public CompositeType getRowType()
{
return rowType;
}
public List getIndexNames()
{
return Collections.unmodifiableList(indexNames);
}
public boolean isValue(Object obj)
{
if (obj == null || !(obj instanceof TabularData))
return false;
TabularType other = ((TabularData) obj).getTabularType();
return equals(other);
}
public boolean equals(Object obj)
{
if (obj == null || (obj instanceof TabularType) == false)
return false;
if (this == obj)
return true;
TabularType other = (TabularType) obj;
if (this.getTypeName().equals(other.getTypeName()) == false)
return false;
if (this.getRowType().equals(other.getRowType()) == false)
return false;
Iterator thisNames = this.getIndexNames().iterator();
Iterator otherNames = other.getIndexNames().iterator();
while (thisNames.hasNext() && otherNames.hasNext())
{
String thisName = (String) thisNames.next();
String otherName = (String) otherNames.next();
if (thisName.equals(otherName) == false)
return false;
}
if (thisNames.hasNext() || otherNames.hasNext())
return false;
return true;
}
public int hashCode()
{
if (cachedHashCode != 0)
return cachedHashCode;
cachedHashCode = getTypeName().hashCode();
cachedHashCode += getRowType().hashCode();
for (Iterator i = indexNames.iterator(); i.hasNext();)
cachedHashCode += i.next().hashCode();
return cachedHashCode;
}
public String toString()
{
if (cachedToString != null)
return cachedToString;
StringBuffer buffer = new StringBuffer(getClass().getName());
buffer.append(": typeName=[");
buffer.append(getTypeName());
buffer.append("] rowType=[");
buffer.append(getRowType());
buffer.append("] indexNames=[");
Iterator thisNames = getIndexNames().iterator();
while(thisNames.hasNext())
{
buffer.append(thisNames.next());
if (thisNames.hasNext())
buffer.append(", ");
}
buffer.append("]");
cachedToString = buffer.toString();
return cachedToString;
}
}