Untitled
unknown
java
3 years ago
1.6 kB
4
Indexable
package com.comp301.a05driver; import java.util.Iterator; import java.util.NoSuchElementException; public class ProximityIterator implements Iterator<Driver> { private final Iterator<Driver> driverPool; private final Position clientPosition; private final int proximityRange; private Driver nextDriver; public ProximityIterator(Iterable<Driver> driverPool, Position clientPosition, int proximityRange) throws IllegalArgumentException { if (driverPool == null) { throw new IllegalArgumentException("Driver pool is null"); } if (clientPosition == null) { throw new IllegalArgumentException("Client position is null"); } this.driverPool = driverPool.iterator(); this.clientPosition = clientPosition; this.proximityRange = proximityRange; nextDriver = null; } private void loadNextDriver() { if (nextDriver == null) { while (driverPool.hasNext()) { Driver currentDriver = driverPool.next(); if (clientPosition.getManhattanDistanceTo(currentDriver.getVehicle().getPosition()) <= proximityRange) { nextDriver = currentDriver; break; } } } } @Override public boolean hasNext() { loadNextDriver(); return (nextDriver != null); } @Override public Driver next() throws NoSuchElementException { if (hasNext()) { Driver driver = nextDriver; nextDriver = null; return driver; } else { throw new NoSuchElementException(); } } }
Editor is loading...