Implementato NodeSequence e aggiunto alcuni costruttori alle eccezioni
This commit is contained in:
175
sequence/NodeSequence.java
Normal file
175
sequence/NodeSequence.java
Normal file
@@ -0,0 +1,175 @@
|
||||
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 <E> extends NodePositionList<E> implements Sequence<E> {
|
||||
|
||||
@Override
|
||||
public E getFirst() throws EmptySequenceException {
|
||||
try {
|
||||
Position<E> t = first();
|
||||
return t.element();
|
||||
}
|
||||
catch (EmptyListException e){
|
||||
throw new EmptySequenceException();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public E getLast() throws EmptySequenceException {
|
||||
try {
|
||||
Position<E> t = last();
|
||||
return t.element();
|
||||
}
|
||||
catch (EmptyListException e){
|
||||
throw new EmptySequenceException();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public E removeFirst() throws EmptySequenceException {
|
||||
try {
|
||||
Position<E> t = first();
|
||||
E ele = remove(t);
|
||||
return ele;
|
||||
}
|
||||
catch (EmptyListException e){
|
||||
throw new EmptySequenceException();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public E removeLast() throws EmptySequenceException {
|
||||
try {
|
||||
Position<E> 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<E> atIndex(int index) throws BoundaryViolationException {
|
||||
try {
|
||||
checkIndex(index, size());
|
||||
Position<E> 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<E> position) throws InvalidPositionException {
|
||||
checkPosition(position);
|
||||
Position<E> 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<E> t = atIndex(i);
|
||||
addBefore(t, e);
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (BoundaryViolationException err){
|
||||
throw new IndexOutOfBoundsException ();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public E set(int i, E e) throws IndexOutOfBoundsException {
|
||||
try {
|
||||
Position<E> 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<E> 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<E> 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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user