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

org.infinispan.commons.util.IteratorMapper Maven / Gradle / Ivy

There is a newer version: 15.1.0.Dev04
Show newest version
package org.infinispan.commons.util;

import java.util.Iterator;
import java.util.function.Function;

/**
 * A iterator that maps each value to the output of the Function.  Note that the remove is supported if the iterator
 * originally supported remove. This iterator implements {@link CloseableIterator} and will close the provided iterator
 * if it also implemented CloseableIterator.
 * @author William Burns
 * @since 8.0
 */
public class IteratorMapper implements CloseableIterator {
   private final Iterator iterator;
   private final Function function;

   public IteratorMapper(Iterator iterator, Function function) {
      if (iterator == null || function == null) {
         throw new NullPointerException();
      }
      this.iterator = iterator;
      this.function = function;
   }

   @Override
   public boolean hasNext() {
      return iterator.hasNext();
   }

   @Override
   public S next() {
      E value = iterator.next();
      return function.apply(value);
   }

   @Override
   public void remove() {
      iterator.remove();
   }

   @Override
   public void close() {
      if (iterator instanceof CloseableIterator) {
         ((CloseableIterator) iterator).close();
      }
   }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy