package sequence; import exceptions.BoundaryViolationException; import exceptions.EmptyListException; import exceptions.EmptySequenceException; import exceptions.InvalidPositionException; import position.NodePositionList; import position.Position; /** * Created with MONSTER. * User: xgiovio * Date: 30/03/2014 * Time: 14:35 */ public class NodeSequence extends NodePositionList implements Sequence { @Override public E getFirst() throws EmptySequenceException { try { Position t = first(); return t.element(); } catch (EmptyListException e){ throw new EmptySequenceException(); } } @Override public E getLast() throws EmptySequenceException { try { Position t = last(); return t.element(); } catch (EmptyListException e){ throw new EmptySequenceException(); } } @Override public E removeFirst() throws EmptySequenceException { try { Position t = first(); E ele = remove(t); return ele; } catch (EmptyListException e){ throw new EmptySequenceException(); } } @Override public E removeLast() throws EmptySequenceException { try { Position t = last(); E ele = remove(t); return ele; } catch (EmptyListException e){ throw new EmptySequenceException(); } } @Override // si potrebbe anche andare indietro a partire dall'ultimo public Position atIndex(int index) throws BoundaryViolationException { try { checkIndex(index, size()); Position t = first(); for (int i = 0; i < index; i++) { t = next(t); } return t; } catch (IndexOutOfBoundsException e){ throw new BoundaryViolationException(); } } @Override public int indexOf(Position position) throws InvalidPositionException { checkPosition(position); Position t = first(); int i ; for (i = 0 ;t != position;i++){ t= next(t); } return i; } @Override public E remove(int i) throws IndexOutOfBoundsException { checkIndex(i,size()); E ele = remove(atIndex(i)); return ele; } @Override public void add(int i, E e) throws IndexOutOfBoundsException { try { checkIndex(i,size()+1); if (i == 0){ addFirst(e); } else { if ( i== size()){ addLast(e); }else { Position t = atIndex(i); addBefore(t, e); } } } catch (BoundaryViolationException err){ throw new IndexOutOfBoundsException (); } } @Override public E set(int i, E e) throws IndexOutOfBoundsException { try { Position t = atIndex(i); E ele = set(t,e); return ele; } catch (BoundaryViolationException err){ throw new IndexOutOfBoundsException (); } } @Override public E get(int i) throws IndexOutOfBoundsException { try { Position t = atIndex(i); return t.element(); } catch (BoundaryViolationException err){ throw new IndexOutOfBoundsException (); } } protected void checkIndex(int r, int n) throws IndexOutOfBoundsException { if (r < 0 || r >= n) throw new IndexOutOfBoundsException("Illegal index: " + r); } @Override public String toString() { String to_return = ""; to_return = to_return + "["; if (size() > 0) { int i; Position temp; for (temp = first(), i = 0;true;) { if ( i == size() - 1) { to_return += (temp.element().toString()); } else { to_return += (temp.element().toString() + ","); } if (i == size() - 1){ break; }else{ i++; temp = next(temp); } } } to_return = to_return + "]"; return to_return; } }