modificato arraysequence in fake e aggiunti una nuova versione. fidato alcuni metodi che restituivano void invece di position. sono necessari ulteriori test
This commit is contained in:
@@ -1,27 +1,82 @@
|
||||
package sequence;
|
||||
|
||||
import arraylist.ArrayIndexList;
|
||||
import exceptions.BoundaryViolationException;
|
||||
import exceptions.EmptyListException;
|
||||
import exceptions.EmptySequenceException;
|
||||
import exceptions.InvalidPositionException;
|
||||
import position.Position;
|
||||
import sequence.utility.DNodeIndex;
|
||||
import sequence.utility.ArrayPosition;
|
||||
|
||||
/**
|
||||
* Created with MONSTER.
|
||||
* Created with xgiovio.macbookair.
|
||||
* User: xgiovio
|
||||
* Date: 30/03/2014
|
||||
* Time: 17:19
|
||||
* Date: 31/03/14
|
||||
* Time: 16:12
|
||||
*/
|
||||
public class ArraySequence<E> extends ArrayIndexList<E> implements Sequence<E> {
|
||||
public class ArraySequence<E> implements Sequence<E> {
|
||||
|
||||
private Position<E>[] array;
|
||||
private int capacity = 100;
|
||||
private int size = 0;
|
||||
|
||||
public ArraySequence (int in){
|
||||
super(in);
|
||||
public ArraySequence() {
|
||||
array = (Position<E>[]) new Object[capacity];
|
||||
}
|
||||
|
||||
@Override
|
||||
public ArraySequence(int in_size) {
|
||||
array = (Position<E>[]) new Object[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<E>)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<E>[] B =(Position<E>[]) new Object[capacity];
|
||||
for (int i=0; i<size; i++)
|
||||
B[i] = array[i];
|
||||
array = B;
|
||||
}
|
||||
for (int i=size-1; i>=r; i--)
|
||||
array[i+1] = array[i];
|
||||
((ArrayPosition<E>)array[r]).setElement(e);
|
||||
size++;
|
||||
}
|
||||
|
||||
|
||||
public E remove(int r) throws IndexOutOfBoundsException {
|
||||
checkIndex(r, size());
|
||||
E temp = array[r].element();
|
||||
for (int i=r; i<size-1; i++)
|
||||
array[i] = array[i+1];
|
||||
size--;
|
||||
return temp;
|
||||
}
|
||||
|
||||
|
||||
public E getFirst() throws EmptySequenceException {
|
||||
try {
|
||||
E ele = get(0);
|
||||
@@ -32,6 +87,7 @@ public class ArraySequence<E> extends ArrayIndexList<E> implements Sequence<E> {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public E getLast() throws EmptySequenceException {
|
||||
try {
|
||||
@@ -65,11 +121,12 @@ public class ArraySequence<E> extends ArrayIndexList<E> implements Sequence<E> {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public Position<E> atIndex(int in_index) throws BoundaryViolationException {
|
||||
try {
|
||||
checkIndex(in_index, size());
|
||||
return new DNodeIndex<E>(get(in_index),in_index);
|
||||
return array[in_index];
|
||||
}
|
||||
catch (IndexOutOfBoundsException err){
|
||||
throw new BoundaryViolationException();
|
||||
@@ -79,13 +136,13 @@ public class ArraySequence<E> extends ArrayIndexList<E> implements Sequence<E> {
|
||||
@Override
|
||||
public int indexOf(Position<E> position) throws InvalidPositionException {
|
||||
checkPosition(position);
|
||||
return ((DNodeIndex<E>) position).getIndex();
|
||||
return ((ArrayPosition<E>) position).getIndex();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Position<E> first() throws EmptyListException {
|
||||
try{
|
||||
return new DNodeIndex<E>(get(0),0);
|
||||
return array[0];
|
||||
}
|
||||
catch (IndexOutOfBoundsException err){
|
||||
throw new EmptyListException("Empty Array");
|
||||
@@ -95,18 +152,19 @@ public class ArraySequence<E> extends ArrayIndexList<E> implements Sequence<E> {
|
||||
@Override
|
||||
public Position<E> last() throws EmptyListException {
|
||||
try{
|
||||
return new DNodeIndex<E>(get(size() - 1),size() - 1);
|
||||
return array[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);
|
||||
ArrayPosition<E> t = checkPosition(p);
|
||||
if (t.getIndex()< size() -1){
|
||||
return new DNodeIndex<E>(get(t.getIndex() + 1),t.getIndex() + 1);
|
||||
return array[t.getIndex() + 1];
|
||||
}else{
|
||||
throw new BoundaryViolationException();
|
||||
}
|
||||
@@ -114,9 +172,9 @@ public class ArraySequence<E> extends ArrayIndexList<E> implements Sequence<E> {
|
||||
|
||||
@Override
|
||||
public Position<E> prev(Position<E> p) throws InvalidPositionException, BoundaryViolationException {
|
||||
DNodeIndex<E> t = checkPosition(p);
|
||||
ArrayPosition<E> t = checkPosition(p);
|
||||
if (t.getIndex()> 0){
|
||||
return new DNodeIndex<E>(get(t.getIndex() - 1),t.getIndex() - 1);
|
||||
return array[t.getIndex() - 1];
|
||||
}else{
|
||||
throw new BoundaryViolationException();
|
||||
}
|
||||
@@ -124,19 +182,21 @@ public class ArraySequence<E> extends ArrayIndexList<E> implements Sequence<E> {
|
||||
|
||||
@Override
|
||||
public void addFirst(E e) {
|
||||
add (0,e);
|
||||
add (0,e);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addLast(E e) {
|
||||
add (size(),e);
|
||||
add (size(),e);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void addAfter(Position<E> p, E e) throws InvalidPositionException {
|
||||
public Position<E> addAfter(Position<E> p, E e) throws InvalidPositionException {
|
||||
try {
|
||||
DNodeIndex<E> t = checkPosition(p);
|
||||
ArrayPosition<E> t = checkPosition(p);
|
||||
add(t.getIndex() + 1, e);
|
||||
return array[t.getIndex() + 1];
|
||||
}
|
||||
catch (IndexOutOfBoundsException err){
|
||||
throw new InvalidPositionException();
|
||||
@@ -144,21 +204,23 @@ public class ArraySequence<E> extends ArrayIndexList<E> implements Sequence<E> {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addBefore(Position<E> p, E e) throws InvalidPositionException {
|
||||
public Position<E> addBefore(Position<E> p, E e) throws InvalidPositionException {
|
||||
try {
|
||||
DNodeIndex<E> t = checkPosition(p);
|
||||
ArrayPosition<E> t = checkPosition(p);
|
||||
add(t.getIndex() - 1,e);
|
||||
return array[t.getIndex() - 1];
|
||||
}
|
||||
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());
|
||||
ArrayPosition<E> t = checkPosition(p);
|
||||
return remove(t.getIndex());
|
||||
}
|
||||
catch (IndexOutOfBoundsException err){
|
||||
throw new InvalidPositionException();
|
||||
@@ -168,7 +230,7 @@ public class ArraySequence<E> extends ArrayIndexList<E> implements Sequence<E> {
|
||||
@Override
|
||||
public E set(Position<E> p, E e) throws InvalidPositionException {
|
||||
try {
|
||||
DNodeIndex<E> t = checkPosition(p);
|
||||
ArrayPosition<E> t = checkPosition(p);
|
||||
return set (t.getIndex(),e);
|
||||
}
|
||||
catch (IndexOutOfBoundsException err){
|
||||
@@ -178,8 +240,10 @@ public class ArraySequence<E> extends ArrayIndexList<E> implements Sequence<E> {
|
||||
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
|
||||
String to_return = "";
|
||||
to_return = to_return + "[";
|
||||
|
||||
@@ -196,13 +260,19 @@ public class ArraySequence<E> extends ArrayIndexList<E> implements Sequence<E> {
|
||||
return to_return;
|
||||
}
|
||||
|
||||
protected DNodeIndex<E> checkPosition(Position<E> p) throws InvalidPositionException {
|
||||
protected void checkIndex(int r, int n) //
|
||||
throws IndexOutOfBoundsException { //
|
||||
if (r < 0 || r >= n)
|
||||
throw new IndexOutOfBoundsException("Illegal index: " + r);
|
||||
}
|
||||
|
||||
protected ArrayPosition<E> checkPosition(Position<E> p) throws InvalidPositionException {
|
||||
if (p == null)
|
||||
throw new InvalidPositionException
|
||||
("Null position passed");
|
||||
|
||||
try {
|
||||
DNodeIndex<E> temp = (DNodeIndex<E>) p;
|
||||
ArrayPosition<E> temp = (ArrayPosition<E>) p;
|
||||
if ( temp.getIndex() >= size() || temp.getIndex() < 0 )
|
||||
throw new InvalidPositionException
|
||||
("Position does not belong to a valid value");
|
||||
@@ -213,6 +283,4 @@ public class ArraySequence<E> extends ArrayIndexList<E> implements Sequence<E> {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user