All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.softicar.platform.common.container.filter.FilteringIterator Maven / Gradle / Ivy

Go to download

The SoftiCAR Platform is a lightweight, Java-based library to create interactive business web applications.

There is a newer version: 50.0.0
Show newest version
package com.softicar.platform.common.container.filter;

import java.util.Iterator;
import java.util.NoSuchElementException;
import java.util.function.Predicate;

/**
 * An iterator that filters the elements with respect to a given predicate.
 * 

* If the sequence contains null elements the predicate must also support * null values. * * @param * the element type * @author Oliver Richers */ public class FilteringIterator implements Iterator { private final Iterator iterator; private final Predicate predicate; private boolean haveElement; private T element; public FilteringIterator(Iterator iterator, Predicate predicate) { this.iterator = iterator; this.predicate = predicate; this.haveElement = false; this.element = null; } @Override public boolean hasNext() { while (!haveElement && iterator.hasNext()) { this.element = iterator.next(); if (predicate.test(element)) { this.haveElement = true; } } return haveElement; } @Override public T next() { if (hasNext()) { this.haveElement = false; return element; } else { throw new NoSuchElementException(); } } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy