package sequence; import exceptions.BoundaryViolationException; import exceptions.EmptyListException; import exceptions.EmptySequenceException; import exceptions.InvalidPositionException; import iterator.IndexListIterator; import iterator.IterablePositionIndexList; import position.Position; import sequence.utility.ArrayPosition; import java.util.Iterator; /** * Created with xgiovio.macbookair. * User: xgiovio * Date: 31/03/14 * Time: 16:12 */ public class ArraySequence implements Sequence { private Position[] array; private int capacity = 100; private int size = 0; public ArraySequence() { array = new Position[capacity]; } public ArraySequence(int in_size) { array = new Position[in_size]; capacity = in_size; } public int size() { return size; } public boolean isEmpty() { return size() == 0; } public E get(int r) throws IndexOutOfBoundsException { checkIndex(r, size()); return array[r].element(); } public E set(int r, E e) throws IndexOutOfBoundsException { checkIndex(r, size()); E temp = array[r].element(); ((ArrayPosition)array[r]).setElement(e); return temp; } public void add(int r, E e) throws IndexOutOfBoundsException { checkIndex(r, size() + 1); if (size == capacity) { capacity *= 2; Position[] B = new Position[capacity]; for (int i=0; i=r; i--) array[i+1] = array[i]; array[r] = new ArrayPosition(e,r); size++; } public E remove(int r) throws IndexOutOfBoundsException { checkIndex(r, size()); E temp = array[r].element(); for (int i=r; i atIndex(int in_index) throws BoundaryViolationException { try { checkIndex(in_index, size()); return array[in_index]; } catch (IndexOutOfBoundsException err){ throw new BoundaryViolationException(); } } @Override public int indexOf(Position position) throws InvalidPositionException { checkPosition(position); return ((ArrayPosition) position).getIndex(); } @Override public Position first() throws EmptyListException { try{ return array[0]; } catch (IndexOutOfBoundsException err){ throw new EmptyListException("Empty Array"); } } @Override public Position last() throws EmptyListException { try{ return array[size()-1]; } catch (IndexOutOfBoundsException err){ throw new EmptyListException("Empty Array"); } } @Override public Position next(Position p) throws InvalidPositionException, BoundaryViolationException { ArrayPosition t = checkPosition(p); if (t.getIndex()< size() -1){ return array[t.getIndex() + 1]; }else{ throw new BoundaryViolationException(); } } @Override public Position prev(Position p) throws InvalidPositionException, BoundaryViolationException { ArrayPosition t = checkPosition(p); if (t.getIndex()> 0){ return array[t.getIndex() - 1]; }else{ throw new BoundaryViolationException(); } } @Override public void addFirst(E e) { add (0,e); } @Override public void addLast(E e) { add (size(),e); } @Override public Position addAfter(Position p, E e) throws InvalidPositionException { try { ArrayPosition t = checkPosition(p); add(t.getIndex() + 1, e); return array[t.getIndex() + 1]; } catch (IndexOutOfBoundsException err){ throw new InvalidPositionException(); } } @Override public Position addBefore(Position p, E e) throws InvalidPositionException { try { ArrayPosition t = checkPosition(p); add(t.getIndex() - 1,e); return array[t.getIndex() - 1]; } catch (IndexOutOfBoundsException err){ throw new InvalidPositionException(); } } @Override public E remove(Position p) throws InvalidPositionException { try { ArrayPosition t = checkPosition(p); return remove(t.getIndex()); } catch (IndexOutOfBoundsException err){ throw new InvalidPositionException(); } } @Override public E set(Position p, E e) throws InvalidPositionException { try { ArrayPosition t = checkPosition(p); return set (t.getIndex(),e); } catch (IndexOutOfBoundsException err){ throw new InvalidPositionException(); } } @Override public String toString() { String to_return = ""; to_return = to_return + "["; for ( int i = 0; i< size() ; i++){ if ( i== size() -1){ to_return+=(get(i).toString()); }else{ to_return+=(get(i).toString() + ","); } } to_return = to_return + "]"; return to_return; } protected void checkIndex(int r, int n) // throws IndexOutOfBoundsException { // if (r < 0 || r >= n) throw new IndexOutOfBoundsException("Illegal index: " + r); } protected ArrayPosition checkPosition(Position p) throws InvalidPositionException { if (p == null) throw new InvalidPositionException ("Null position passed"); try { ArrayPosition temp = (ArrayPosition) p; if ( temp.getIndex() >= size() || temp.getIndex() < 0 ) 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"); } } @Override ///////////////// implemented used a generic IndexListIterator public Iterator iterator() { E[] temp = (E[])new Object[this.size()]; for (int i = 0 ;i < this.size(); i++){ temp[i] = this.get(i); } return new IndexListIterator(temp); } @Override public Iterable> positions() { Position[] temp = new Position [this.size()]; for (int i = 0 ;i < this.size(); i++){ temp[i] = this.atIndex(i); } return new IterablePositionIndexList(temp); } }