package sequence; import arraylist.ArrayIndexList; import exceptions.BoundaryViolationException; import exceptions.EmptyListException; import exceptions.EmptySequenceException; import exceptions.InvalidPositionException; import position.Position; import sequence.utility.DNodeIndex; /** * Created with MONSTER. * User: xgiovio * Date: 30/03/2014 * Time: 17:19 */ public class ArraySequence extends ArrayIndexList implements Sequence { @Override public E getFirst() throws EmptySequenceException { try { E ele = get(0); return ele; } catch (IndexOutOfBoundsException err){ throw new EmptySequenceException(); } } @Override public E getLast() throws EmptySequenceException { try { E ele = get(size()-1); return ele; } catch (IndexOutOfBoundsException err){ throw new EmptySequenceException(); } } @Override public E removeFirst() throws EmptySequenceException { try { E ele = remove(0); return ele; } catch (IndexOutOfBoundsException err){ throw new EmptySequenceException(); } } @Override public E removeLast() throws EmptySequenceException { try { E ele = remove(size() - 1); return ele; } catch (IndexOutOfBoundsException err){ throw new EmptySequenceException(); } } @Override public Position atIndex(int in_index) throws BoundaryViolationException { try { checkIndex(in_index, size()); ///////////// } catch (IndexOutOfBoundsException err){ throw new BoundaryViolationException(); } } @Override public int indexOf(Position position) throws InvalidPositionException { return 0; } @Override public Position first() throws EmptyListException { return null; } @Override public Position last() throws EmptyListException { return null; } @Override public Position next(Position p) throws InvalidPositionException, BoundaryViolationException { return null; } @Override public Position prev(Position p) throws InvalidPositionException, BoundaryViolationException { return null; } @Override public void addFirst(E e) { } @Override public void addLast(E e) { } @Override public void addAfter(Position p, E e) throws InvalidPositionException { } @Override public void addBefore(Position p, E e) throws InvalidPositionException { } @Override public E remove(Position p) throws InvalidPositionException { return null; } @Override public E set(Position p, E e) throws InvalidPositionException { return null; } @Override public String toString() { return super.toString(); } protected DNodeIndex checkPosition(Position p) throws InvalidPositionException { if (p == null) throw new InvalidPositionException ("Null position passed"); try { DNodeIndex temp = (DNodeIndex) p; if ((temp.getPrev() == null) || (temp.getNext() == null) || (temp.getIndex() == -1)) throw new InvalidPositionException ("Position does not belong to a valid value"); return temp; } catch (ClassCastException e) { throw new InvalidPositionException ("Position is of wrong type for this array"); } } }