/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 * 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 SExceptions
{
    private static final int MIN_SIZE = 0;
    private static final int MAX_SIZE = 65535;

    private SExceptionTableEntry[] exceptions;

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

	exceptions = new SExceptionTableEntry[size];
    }

    public SExceptionTableEntry getException(int index)
	throws SClassFileException
    {	
	checkArrayBounds(index);
	return exceptions[index];
    }

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

    public void setException(int index, SExceptionTableEntry entry)
	throws SClassFileException
    {
	checkArrayBounds(index);
	exceptions[index] = entry;
    }
    
    public String toString()
    {
	    String s = new String();

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

	    return s;
    }

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