Skip to content
Snippets Groups Projects
Commit e31c6985 authored by Mathea Berg Vindsetmo's avatar Mathea Berg Vindsetmo
Browse files

of5_2 lf

parent fbecd4aa
No related branches found
No related tags found
No related merge requests found
package of5_2.lf;
import java.util.Iterator;
import java.util.function.Predicate;
public class FilterAnimalsIterator implements Iterator<Animal> {
private int index = 0;
private Farm farm;
private Predicate<Animal> animalPredicate;
public FilterAnimalsIterator(Farm farm, Predicate<Animal> animalPredicate){
this.animalPredicate = animalPredicate;
this.farm = farm;
}
@Override
public boolean hasNext() {
while(index<farm.numberOfAnimals()){
if(animalPredicate.test(farm.getAnimal(index))){
return true;
}
else{
index++;
}
}
return false;
}
@Override
public Animal next() {
if(!hasNext()){
throw new IllegalArgumentException("No more animals in the farm");
}
return farm.getAnimal(index++);
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment