I am trying to write a fixed sized queue for generic type elements.The user can call the constructor, providing a size and an internal array is created for that size.(see below)
class FixedSizeQ<E>{
private E[] q;
private int N;
public FixedSizeQ(int max) {
super();
q = (E[]) new Object[max];
N = 0;
}
..
}
I thought of defining an isEmpty() and isFull() methods and an insert() method for the above.If the client tries to add an element to the already full array,I want to throw an exception.Going through the javadocs,I thought IllegalStateException
would be the correct exception to throw.
public boolean isFull(){
return N == q.length;
}
public void insert(Item item){
if(isFull()){
throw new IllegalStateException("queue full");
}
...
}
I want to know if my understanding is correct..Someone suggested that IllegalArgumentException
is more appropriate.Can someone advise?