/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 * This source file is part of SableVM.                            *
 *                                                                 *
 * See the file "LICENSE" for the copyright information and for    *
 * the terms and conditions for copying, distribution and          *
 * modification of this source file.                               *
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

/**
 * @author Patrick Latifi
 */
public class SClasses
{
	private static final int MIN_SIZE = 0;
	private static final int MAX_SIZE = 65535;

	private SClassTableEntry[] classes;

	public SClasses(int size) throws SClassFileException
	{
		if (size < MIN_SIZE || size > MAX_SIZE)
			throw new SClassFileException(
					"Invalid classes count: " + size);

		classes = new SClassTableEntry[size];
	}

	public SClassTableEntry getClass(int index)
		throws SClassFileException
	{	
		checkArrayBounds(index);
		return classes[index];
	}

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

	public void setClass(int index, SClassTableEntry entry)
		throws SClassFileException
	{
		checkArrayBounds(index);
		classes[index] = entry;
	}

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

		s += "\n*** Classes ***\n";
		for (int i = 0; i < classes.length; i++)
			s += classes[i] + "\t";

		return s;
	}

	private void checkArrayBounds(int index) throws SClassFileException
	{
		if (index < 0 || index >= classes.length)
			throw new SClassFileException(
				"Out of bound access for the classes table");
	}
}
