219 lines
5.7 KiB
Java
219 lines
5.7 KiB
Java
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<E> extends ArrayIndexList<E> implements Sequence<E> {
|
|
|
|
|
|
public ArraySequence (int in){
|
|
super(in);
|
|
}
|
|
|
|
@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<E> atIndex(int in_index) throws BoundaryViolationException {
|
|
try {
|
|
checkIndex(in_index, size());
|
|
return new DNodeIndex<E>(get(in_index),in_index);
|
|
}
|
|
catch (IndexOutOfBoundsException err){
|
|
throw new BoundaryViolationException();
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public int indexOf(Position<E> position) throws InvalidPositionException {
|
|
checkPosition(position);
|
|
return ((DNodeIndex<E>) position).getIndex();
|
|
}
|
|
|
|
@Override
|
|
public Position<E> first() throws EmptyListException {
|
|
try{
|
|
return new DNodeIndex<E>(get(0),0);
|
|
}
|
|
catch (IndexOutOfBoundsException err){
|
|
throw new EmptyListException("Empty Array");
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public Position<E> last() throws EmptyListException {
|
|
try{
|
|
return new DNodeIndex<E>(get(size() - 1),size() - 1);
|
|
}
|
|
catch (IndexOutOfBoundsException err){
|
|
throw new EmptyListException("Empty Array");
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public Position<E> next(Position<E> p) throws InvalidPositionException, BoundaryViolationException {
|
|
DNodeIndex<E> t = checkPosition(p);
|
|
if (t.getIndex()< size() -1){
|
|
return new DNodeIndex<E>(get(t.getIndex() + 1),t.getIndex() + 1);
|
|
}else{
|
|
throw new BoundaryViolationException();
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public Position<E> prev(Position<E> p) throws InvalidPositionException, BoundaryViolationException {
|
|
DNodeIndex<E> t = checkPosition(p);
|
|
if (t.getIndex()> 0){
|
|
return new DNodeIndex<E>(get(t.getIndex() - 1),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 void addAfter(Position<E> p, E e) throws InvalidPositionException {
|
|
try {
|
|
DNodeIndex<E> t = checkPosition(p);
|
|
add(t.getIndex() + 1, e);
|
|
}
|
|
catch (IndexOutOfBoundsException err){
|
|
throw new InvalidPositionException();
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public void addBefore(Position<E> p, E e) throws InvalidPositionException {
|
|
try {
|
|
DNodeIndex<E> t = checkPosition(p);
|
|
add(t.getIndex() - 1,e);
|
|
}
|
|
catch (IndexOutOfBoundsException err){
|
|
throw new InvalidPositionException();
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public E remove(Position<E> p) throws InvalidPositionException {
|
|
try {
|
|
DNodeIndex<E> t = checkPosition(p);
|
|
return remove(t.getIndex());
|
|
}
|
|
catch (IndexOutOfBoundsException err){
|
|
throw new InvalidPositionException();
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public E set(Position<E> p, E e) throws InvalidPositionException {
|
|
try {
|
|
DNodeIndex<E> 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 DNodeIndex<E> checkPosition(Position<E> p) throws InvalidPositionException {
|
|
if (p == null)
|
|
throw new InvalidPositionException
|
|
("Null position passed");
|
|
|
|
try {
|
|
DNodeIndex<E> temp = (DNodeIndex<E>) 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");
|
|
}
|
|
}
|
|
|
|
|
|
|
|
}
|