/**
 * @author Patrick Latifi
 */
public class SConstantPool
{
	private SCpInfo[] constants;

	public SConstantPool(int size) throws SClassFileException
	{
		if (size < 0 || size > 65535)
			throw new SClassFileException(
				"Invalid constant pool count: " + size);

		constants = new SCpInfo[size];
	}

	public SCpInfo getCpInfo(int index) throws SClassFileException
	{	
		checkArrayBounds(index);
		return constants[index];
	}

	public int count()
	{
		return constants.length;
	}

	public void setCpInfo(int index, SCpInfo info)
		throws SClassFileException
	{
		checkArrayBounds(index);
		constants[index] = info;
	}

	public String toString()
	{
		String s = new String();

		s += "\n***Constant Pool***\n";
		s += "\n Constant pool count = " + constants.length + "\n";
		for (int i = 0; i < constants.length; i++)
			s += constants[i] + "\n";

		return s;
	}

	private void checkArrayBounds(int index) throws SClassFileException
	{
		if (index == 0)
			throw new SClassFileException(
				"Invalid access for the constant pool: " +
				"trying to reference this.");

		if (index < 0 || index >= constants.length)
			throw new SClassFileException(
				"Out of bound access for the constant pool");
	}
}
