package javax.management.openmbean;
import java.util.Collections;
import java.util.Iterator;
import java.util.Set;
import java.util.TreeMap;
import java.io.Serializable;
public class CompositeType
extends OpenType
implements Serializable
{
private TreeMap nameToDescription;
private TreeMap nameToType;
private transient int cachedHashCode = 0;
private transient String cachedToString = null;
private static final long serialVersionUID = -5366242454346948798L;
public CompositeType(String typeName, String description,
String[] itemNames, String[] itemDescriptions,
OpenType[] itemTypes)
throws OpenDataException
{
super(CompositeData.class.getName(), typeName, description);
if (itemNames == null || itemNames.length == 0)
throw new IllegalArgumentException("null or empty itemNames");
if (itemDescriptions == null || itemDescriptions.length == 0)
throw new IllegalArgumentException("null or empty itemDescriptions");
if (itemTypes == null || itemTypes.length == 0)
throw new IllegalArgumentException("null or empty itemTypes");
if (itemNames.length != itemDescriptions.length)
throw new IllegalArgumentException("wrong number of itemDescriptions");
if (itemNames.length != itemTypes.length)
throw new IllegalArgumentException("wrong number of itemTypes");
nameToDescription = new TreeMap();
nameToType = new TreeMap();
for (int i = 0; i < itemNames.length; i++)
{
if (itemNames[i] == null)
throw new IllegalArgumentException("null item name " + i);
String itemName = itemNames[i].trim();
if (itemName.length() == 0)
throw new IllegalArgumentException("empty item name " + i);
if (nameToDescription.containsKey(itemName))
throw new OpenDataException("duplicate item name " + itemName);
if (itemDescriptions[i] == null)
throw new IllegalArgumentException("null item description " + i);
String itemDescription = itemDescriptions[i].trim();
if (itemDescription.length() == 0)
throw new IllegalArgumentException("empty item description " + i);
if (itemTypes[i] == null)
throw new IllegalArgumentException("null item type " + i);
nameToDescription.put(itemName, itemDescription);
nameToType.put(itemName, itemTypes[i]);
}
}
public boolean containsKey(String itemName)
{
if (itemName == null)
return false;
return nameToDescription.containsKey(itemName);
}
public String getDescription(String itemName)
{
if (itemName == null)
return null;
return (String) nameToDescription.get(itemName);
}
public OpenType getType(String itemName)
{
if (itemName == null)
return null;
return (OpenType) nameToType.get(itemName);
}
public Set keySet()
{
return Collections.unmodifiableSet(nameToDescription.keySet());
}
public boolean isValue(Object obj)
{
if (obj == null || !(obj instanceof CompositeData))
return false;
return equals(((CompositeData) obj).getCompositeType());
}
public boolean equals(Object obj)
{
if (obj == null || (obj instanceof CompositeType) == false)
return false;
if (this == obj)
return true;
CompositeType other = (CompositeType) obj;
if (this.getTypeName().equals(other.getTypeName()) == false)
return false;
Iterator thisNames = this.keySet().iterator();
Iterator otherNames = other.keySet().iterator();
while(thisNames.hasNext() && otherNames.hasNext())
{
String thisName = (String) thisNames.next();
String otherName = (String) otherNames.next();
if (thisName.equals(otherName) == false)
return false;
if (this.getType(thisName).equals(other.getType(otherName)) == false)
return false;
}
if (thisNames.hasNext() || otherNames.hasNext())
return false;
return true;
}
public int hashCode()
{
if (cachedHashCode != 0)
return cachedHashCode;
cachedHashCode = getTypeName().hashCode();
for (Iterator i = nameToType.values().iterator(); i.hasNext();)
cachedHashCode += i.next().hashCode();
for (Iterator i = nameToDescription.keySet().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("\n");
Iterator thisNames = keySet().iterator();
while(thisNames.hasNext())
{
String thisName = (String) thisNames.next();
buffer.append("name=");
buffer.append(thisName);
buffer.append(" type=");
buffer.append(getType(thisName));
if (thisNames.hasNext())
buffer.append("\n");
}
cachedToString = buffer.toString();
return cachedToString;
}
}