1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
| public class Neighborhood implements Iterable
{
ArrayList<Position> relativePosition;
public Neighborhood(ArrayList<Position> pos)
{
relativePosition = pos.clone();
}
// x is the mark of the Neighborhood.
// @returns The absolute neighborhood with the mark x.
public Neighborhood getAbsolute(Position x)
{
ArrayList<Position> pos = new ArrayList<Position>();
for(Position p : relativePosition)
pos.add(new Position(x.getx() + p.getx(), x.gety() + p.gety()));
return new Neighborhood(pos);
}
public Neighborhood getAbsolute(Position x, Predicate<Position> p)
{
ArrayList<Position> absolutePos = new ArrayList<Position>();
for(Position pos : relativePosition)
if(p.evaluate(pos))
absolutePos.add(new Position(x.getx() + pos.getx(), x.gety() + pos.gety()));
return new Neighborhood(absolutePos);
}
@Override
public Iterator iterator()
{
return new NeighborhoodIterator();
}
private class NeighborhoodIterator implements Iterator
{
private Iterator iter;
public NeighborhoodIterator()
{
iter = relativePosition.iterator();
}
@Override
public boolean hasNext()
{
return iter.hasNext();
}
@Override
public Position next()
{
return (Position) iter.next();
}
@Override
public void remove()
{
iter.remove();
}
}
} |