package javax.management.openmbean;
import java.io.Serializable;
public class ArrayType
extends OpenType
implements Serializable
{
private int dimension = 0;
private OpenType elementType = null;
private transient int cachedHashCode = 0;
private transient String cachedToString = null;
private static final long serialVersionUID = 720504429830309770L;
public ArrayType(int dimension, OpenType elementType)
throws OpenDataException
{
super(genName(dimension, elementType),
genName(dimension, elementType),
genDesc(dimension, elementType));
this.dimension = dimension;
this.elementType = elementType;
}
public int getDimension()
{
return dimension;
}
public OpenType getElementOpenType()
{
return elementType;
}
public boolean isValue(Object obj)
{
if (obj == null)
return false;
Class clazz = obj.getClass();
if (clazz.isArray() == false)
return false;
if (elementType instanceof SimpleType)
return (getClassName().equals(clazz.getName()));
if (elementType instanceof TabularType ||
elementType instanceof CompositeType)
{
Class thisClass = null;
try
{
thisClass = Thread.currentThread().getContextClassLoader().loadClass(getClassName());
}
catch (ClassNotFoundException e)
{
return false;
}
if (thisClass.isAssignableFrom(clazz) == false)
return false;
return recursiveCheck((Object[]) obj, dimension);
}
return false;
}
public boolean equals(Object obj)
{
if (this == obj)
return true;
if (obj == null || !(obj instanceof ArrayType))
return false;
ArrayType other = (ArrayType) obj;
return (this.getDimension() == other.getDimension() &&
this.getElementOpenType().equals(other.getElementOpenType()));
}
public int hashCode()
{
if (cachedHashCode != 0)
return cachedHashCode;
cachedHashCode = getDimension() + getElementOpenType().hashCode();
return cachedHashCode;
}
public String toString()
{
if (cachedToString != null)
return cachedToString;
StringBuffer buffer = new StringBuffer(ArrayType.class.getName());
buffer.append("\n");
buffer.append(getTypeName());
buffer.append("\n");
buffer.append(new Integer(dimension));
buffer.append("-dimensional array of\n");
buffer.append(elementType);
cachedToString = buffer.toString();
return cachedToString;
}
private static String genName(int dimension, OpenType elementType)
throws OpenDataException
{
if (dimension < 1)
throw new IllegalArgumentException("negative dimension");
if (elementType == null)
throw new IllegalArgumentException("null element type");
if (elementType instanceof ArrayType)
throw new OpenDataException("array type cannot be an" +
" element of an array type");
StringBuffer buffer = new StringBuffer();
for (int i=0; i < dimension; i++)
buffer.append('[');
buffer.append('L');
buffer.append(elementType.getClassName());
buffer.append(';');
return buffer.toString();
}
private static String genDesc(int dimension, OpenType elementType)
{
StringBuffer buffer = new StringBuffer();
buffer.append(new Integer(dimension));
buffer.append("-dimension array of ");
buffer.append(elementType.getClassName());
return buffer.toString();
}
private boolean recursiveCheck(Object[] elements, int dimension)
{
if (dimension == 1)
{
for (int i = 0; i < elements.length; i++)
if (elements[i] != null && elementType.isValue(elements[i]) == false)
return false;
}
else
{
for (int i = 0; i < elements.length; i++)
if (recursiveCheck((Object[]) elements[i], dimension-1) == false)
return false;
}
return true;
}
}