Migliroato positions e agiunto come metodo alla interfaccia position. Fixato alcuni bug. Aggiunto test della prova intercorso del 10/4/14. Bisogna implementare tree
This commit is contained in:
94
iterator/IterablePosition.java
Normal file
94
iterator/IterablePosition.java
Normal file
@@ -0,0 +1,94 @@
|
||||
package iterator;
|
||||
|
||||
import exceptions.BoundaryViolationException;
|
||||
import javafx.geometry.Pos;
|
||||
import position.Position;
|
||||
import position.PositionList;
|
||||
|
||||
import java.util.Iterator;
|
||||
import java.util.NoSuchElementException;
|
||||
|
||||
/**
|
||||
* Created with MONSTER.
|
||||
* User: xgiovio
|
||||
* Date: 06/04/2014
|
||||
* Time: 21:06
|
||||
*/
|
||||
|
||||
//iterable of positions implementation with cursor for PositionList data structures.
|
||||
|
||||
public class IterablePosition<E> implements Iterable<Position<E>> {
|
||||
|
||||
PositionList<E> new_structure = null;
|
||||
|
||||
public IterablePosition(PositionList<E> structure){
|
||||
|
||||
new_structure = structure;
|
||||
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public Iterator<Position<E>> iterator() {
|
||||
return new getpositionsiterator(new_structure);
|
||||
}
|
||||
|
||||
|
||||
class getpositionsiterator implements Iterator<Position<E>> {
|
||||
|
||||
PositionList<E> new_structure = null;
|
||||
Position<E> pos = null;
|
||||
|
||||
public getpositionsiterator (PositionList<E> in){
|
||||
new_structure = in;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasNext() {
|
||||
if (pos == null){
|
||||
if (new_structure. size() <= 0){
|
||||
return false;
|
||||
} else {
|
||||
return true;
|
||||
}
|
||||
|
||||
} else {
|
||||
|
||||
try {
|
||||
new_structure.next(pos);
|
||||
return true;
|
||||
}
|
||||
catch (BoundaryViolationException err){
|
||||
return false;
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Position<E> next() throws NoSuchElementException {
|
||||
if (hasNext()){
|
||||
if (pos == null){
|
||||
pos = new_structure.first();
|
||||
}else {
|
||||
pos = new_structure.next(pos);
|
||||
}
|
||||
return pos;
|
||||
} else{
|
||||
throw new NoSuchElementException();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void remove() {
|
||||
throw new UnsupportedOperationException ();
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
73
iterator/IterablePositionIndexList.java
Normal file
73
iterator/IterablePositionIndexList.java
Normal file
@@ -0,0 +1,73 @@
|
||||
package iterator;
|
||||
|
||||
import arraylist.ArrayIndexList;
|
||||
import position.Position;
|
||||
|
||||
import java.util.Iterator;
|
||||
import java.util.NoSuchElementException;
|
||||
|
||||
/**
|
||||
* Created with MONSTER.
|
||||
* User: xgiovio
|
||||
* Date: 06/04/2014
|
||||
* Time: 21:13
|
||||
*/
|
||||
|
||||
// generic iterable list creation using an arraylist. the class calling this metod should pass an array of Position<E>
|
||||
|
||||
public class IterablePositionIndexList<E> implements Iterable<Position<E>> {
|
||||
|
||||
Position<E>[] new_structure = null;
|
||||
|
||||
public IterablePositionIndexList (Position<E>[] in){
|
||||
new_structure = in;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Iterator<Position<E>> iterator() {
|
||||
return new getpositionsiterator(new_structure);
|
||||
}
|
||||
|
||||
|
||||
class getpositionsiterator implements Iterator<Position<E>>{
|
||||
|
||||
public getpositionsiterator(Position<E>[] in_elements){
|
||||
for (int i = 0;i< in_elements.length;i++){
|
||||
data.add(i,in_elements[i]);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasNext() {
|
||||
|
||||
if (pos <= data.size() -2){
|
||||
return true;
|
||||
}else {
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public Position<E> next() throws NoSuchElementException {
|
||||
if (hasNext()){
|
||||
pos++;
|
||||
return data.get(pos);
|
||||
}else {
|
||||
throw new NoSuchElementException();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void remove() {
|
||||
throw new UnsupportedOperationException ();
|
||||
}
|
||||
|
||||
private ArrayIndexList<Position<E>> data = new ArrayIndexList<Position<E>>();
|
||||
private int pos = -1;
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user