Class LazyIterableAdapter<T>

All Implemented Interfaces:
Iterable<T>, InternalIterable<T>, LazyIterable<T>, RichIterable<T>

public class LazyIterableAdapter<T> extends AbstractLazyIterable<T>
A LazyIterableAdapter wraps any iterable with the LazyIterable interface.
  • Constructor Details

    • LazyIterableAdapter

      public LazyIterableAdapter(Iterable<T> newAdapted)
  • Method Details

    • each

      public void each(Procedure<? super T> procedure)
      Description copied from interface: RichIterable
      The procedure is executed for each element in the iterable.

      Example using a Java 8 lambda expression:

       people.each(person -> LOGGER.info(person.getName()));
       

      Example using an anonymous inner class:

       people.each(new Procedure<Person>()
       {
           public void value(Person person)
           {
               LOGGER.info(person.getName());
           }
       });
       
      This method is a variant of InternalIterable.forEach(Procedure) that has a signature conflict with Iterable.forEach(java.util.function.Consumer).
      See Also:
    • forEachWithIndex

      public void forEachWithIndex(ObjectIntProcedure<? super T> objectIntProcedure)
      Description copied from interface: InternalIterable
      Iterates over the iterable passing each element and the current relative int index to the specified instance of ObjectIntProcedure.

      Example using a Java 8 lambda:

       people.forEachWithIndex((Person person, int index) -> LOGGER.info("Index: " + index + " person: " + person.getName()));
       

      Example using an anonymous inner class:

       people.forEachWithIndex(new ObjectIntProcedure<Person>()
       {
           public void value(Person person, int index)
           {
               LOGGER.info("Index: " + index + " person: " + person.getName());
           }
       });
       
      Specified by:
      forEachWithIndex in interface InternalIterable<T>
      Overrides:
      forEachWithIndex in class AbstractRichIterable<T>
    • forEachWith

      public <P> void forEachWith(Procedure2<? super T,? super P> procedure, P parameter)
      Description copied from interface: InternalIterable
      The procedure2 is evaluated for each element in the iterable with the specified parameter provided as the second argument.

      Example using a Java 8 lambda:

       people.forEachWith((Person person, Person other) ->
           {
               if (person.isRelatedTo(other))
               {
                    LOGGER.info(person.getName());
               }
           }, fred);
       

      Example using an anonymous inner class:

       people.forEachWith(new Procedure2<Person, Person>()
       {
           public void value(Person person, Person other)
           {
               if (person.isRelatedTo(other))
               {
                    LOGGER.info(person.getName());
               }
           }
       }, fred);
       
      Specified by:
      forEachWith in interface InternalIterable<T>
      Overrides:
      forEachWith in class AbstractRichIterable<T>
    • iterator

      public Iterator<T> iterator()
    • into

      public <R extends Collection<T>> R into(R target)
      Description copied from interface: RichIterable
      Adds all the elements in this iterable to the specific target Collection.
      Specified by:
      into in interface LazyIterable<T>
      Specified by:
      into in interface RichIterable<T>
      Overrides:
      into in class AbstractLazyIterable<T>
    • select

      public LazyIterable<T> select(Predicate<? super T> predicate)
      Description copied from interface: LazyIterable
      Creates a deferred iterable for selecting elements from the current iterable.
      Specified by:
      select in interface LazyIterable<T>
      Specified by:
      select in interface RichIterable<T>
      Overrides:
      select in class AbstractLazyIterable<T>
    • reject

      public LazyIterable<T> reject(Predicate<? super T> predicate)
      Description copied from interface: LazyIterable
      Creates a deferred iterable for rejecting elements from the current iterable.
      Specified by:
      reject in interface LazyIterable<T>
      Specified by:
      reject in interface RichIterable<T>
      Overrides:
      reject in class AbstractLazyIterable<T>
      Parameters:
      predicate - a Predicate to use as the reject criteria
      Returns:
      a RichIterable that contains elements that cause Predicate.accept(Object) method to evaluate to false
    • collect

      public <V> LazyIterable<V> collect(Function<? super T,? extends V> function)
      Description copied from interface: LazyIterable
      Creates a deferred iterable for collecting elements from the current iterable.
      Specified by:
      collect in interface LazyIterable<T>
      Specified by:
      collect in interface RichIterable<T>
      Overrides:
      collect in class AbstractLazyIterable<T>
    • flatCollect

      public <V> LazyIterable<V> flatCollect(Function<? super T,? extends Iterable<V>> function)
      Description copied from interface: LazyIterable
      Creates a deferred flattening iterable for the current iterable.
      Specified by:
      flatCollect in interface LazyIterable<T>
      Specified by:
      flatCollect in interface RichIterable<T>
      Overrides:
      flatCollect in class AbstractLazyIterable<T>
      Parameters:
      function - The Function to apply
      Returns:
      a new flattened collection produced by applying the given function
    • collectIf

      public <V> LazyIterable<V> collectIf(Predicate<? super T> predicate, Function<? super T,? extends V> function)
      Description copied from interface: LazyIterable
      Creates a deferred iterable for selecting and collecting elements from the current iterable.
      Specified by:
      collectIf in interface LazyIterable<T>
      Specified by:
      collectIf in interface RichIterable<T>
      Overrides:
      collectIf in class AbstractLazyIterable<T>
    • take

      public LazyIterable<T> take(int count)
      Description copied from interface: LazyIterable
      Creates a deferred take iterable for the current iterable using the specified count as the limit.
      Specified by:
      take in interface LazyIterable<T>
      Overrides:
      take in class AbstractLazyIterable<T>
    • drop

      public LazyIterable<T> drop(int count)
      Description copied from interface: LazyIterable
      Creates a deferred drop iterable for the current iterable using the specified count as the limit.
      Specified by:
      drop in interface LazyIterable<T>
      Overrides:
      drop in class AbstractLazyIterable<T>
    • takeWhile

      public LazyIterable<T> takeWhile(Predicate<? super T> predicate)
      Specified by:
      takeWhile in interface LazyIterable<T>
      Overrides:
      takeWhile in class AbstractLazyIterable<T>
      See Also:
    • dropWhile

      public LazyIterable<T> dropWhile(Predicate<? super T> predicate)
      Specified by:
      dropWhile in interface LazyIterable<T>
      Overrides:
      dropWhile in class AbstractLazyIterable<T>
      See Also:
    • distinct

      public LazyIterable<T> distinct()
      Description copied from interface: LazyIterable
      Creates a deferred distinct iterable to get distinct elements from the current iterable.
      Specified by:
      distinct in interface LazyIterable<T>
      Overrides:
      distinct in class AbstractLazyIterable<T>
    • toArray

      public Object[] toArray()
      Description copied from interface: RichIterable
      Converts this iterable to an array.
      Specified by:
      toArray in interface RichIterable<T>
      Overrides:
      toArray in class AbstractRichIterable<T>
      See Also:
    • size

      public int size()
      Description copied from interface: RichIterable
      Returns the number of items in this iterable.
      Specified by:
      size in interface RichIterable<T>
      Overrides:
      size in class AbstractLazyIterable<T>
    • isEmpty

      public boolean isEmpty()
      Description copied from interface: RichIterable
      Returns true if this iterable has zero items.
      Specified by:
      isEmpty in interface RichIterable<T>
      Overrides:
      isEmpty in class AbstractLazyIterable<T>
    • anySatisfy

      public boolean anySatisfy(Predicate<? super T> predicate)
      Description copied from interface: RichIterable
      Returns true if the predicate evaluates to true for any element of the iterable. Returns false if the iterable is empty, or if no element returned true when evaluating the predicate.
      Specified by:
      anySatisfy in interface RichIterable<T>
      Overrides:
      anySatisfy in class AbstractRichIterable<T>
    • allSatisfy

      public boolean allSatisfy(Predicate<? super T> predicate)
      Description copied from interface: RichIterable
      Returns true if the predicate evaluates to true for every element of the iterable or if the iterable is empty. Otherwise, returns false.
      Specified by:
      allSatisfy in interface RichIterable<T>
      Overrides:
      allSatisfy in class AbstractRichIterable<T>
    • noneSatisfy

      public boolean noneSatisfy(Predicate<? super T> predicate)
      Description copied from interface: RichIterable
      Returns true if the predicate evaluates to false for every element of the iterable or if the iterable is empty. Otherwise, returns false.
      Specified by:
      noneSatisfy in interface RichIterable<T>
      Overrides:
      noneSatisfy in class AbstractRichIterable<T>
    • anySatisfyWith

      public <P> boolean anySatisfyWith(Predicate2<? super T,? super P> predicate, P parameter)
      Description copied from interface: RichIterable
      Returns true if the predicate evaluates to true for any element of the collection, or return false. Returns false if the collection is empty.
      Specified by:
      anySatisfyWith in interface RichIterable<T>
      Overrides:
      anySatisfyWith in class AbstractRichIterable<T>
    • allSatisfyWith

      public <P> boolean allSatisfyWith(Predicate2<? super T,? super P> predicate, P parameter)
      Description copied from interface: RichIterable
      Returns true if the predicate evaluates to true for every element of the collection, or returns false.
      Specified by:
      allSatisfyWith in interface RichIterable<T>
      Overrides:
      allSatisfyWith in class AbstractRichIterable<T>
    • noneSatisfyWith

      public <P> boolean noneSatisfyWith(Predicate2<? super T,? super P> predicate, P parameter)
      Description copied from interface: RichIterable
      Returns true if the predicate evaluates to false for every element of the collection, or return false. Returns true if the collection is empty.
      Specified by:
      noneSatisfyWith in interface RichIterable<T>
      Overrides:
      noneSatisfyWith in class AbstractRichIterable<T>
    • getFirst

      public T getFirst()
      Description copied from interface: RichIterable
      Returns the first element of an iterable. In the case of a List it is the element at the first index. In the case of any other Collection, it is the first element that would be returned during an iteration. If the iterable is empty, null is returned. If null is a valid element of the container, then a developer would need to check to see if the iterable is empty to validate that a null result was not due to the container being empty.

      The order of Sets are not guaranteed (except for TreeSets and other Ordered Set implementations), so if you use this method, the first element could be any element from the Set.

      Specified by:
      getFirst in interface LazyIterable<T>
      Specified by:
      getFirst in interface RichIterable<T>
      Overrides:
      getFirst in class AbstractLazyIterable<T>
    • getLast

      public T getLast()
      Description copied from interface: RichIterable
      Returns the last element of an iterable. In the case of a List it is the element at the last index. In the case of any other Collection, it is the last element that would be returned during an iteration. If the iterable is empty, null is returned. If null is a valid element of the container, then a developer would need to check to see if the iterable is empty to validate that a null result was not due to the container being empty.

      The order of Sets are not guaranteed (except for TreeSets and other Ordered Set implementations), so if you use this method, the last element could be any element from the Set.

      Specified by:
      getLast in interface RichIterable<T>
      Overrides:
      getLast in class AbstractLazyIterable<T>
    • detect

      public T detect(Predicate<? super T> predicate)
      Description copied from interface: RichIterable
      Returns the first element of the iterable for which the predicate evaluates to true or null in the case where no element returns true. This method is commonly called find.

      Example using a Java 8 lambda expression:

       Person person =
           people.detect(person -> person.getFirstName().equals("John") && person.getLastName().equals("Smith"));
       

      Example using an anonymous inner class:

       Person person =
           people.detect(new Predicate<Person>()
           {
               public boolean accept(Person person)
               {
                   return person.getFirstName().equals("John") && person.getLastName().equals("Smith");
               }
           });
       
      Specified by:
      detect in interface RichIterable<T>
      Overrides:
      detect in class AbstractRichIterable<T>
    • detectWith

      public <P> T detectWith(Predicate2<? super T,? super P> predicate, P parameter)
      Description copied from interface: RichIterable
      Returns the first element that evaluates to true for the specified predicate2 and parameter, or null if none evaluate to true.

      Example using a Java 8 lambda expression:

       Person person =
           people.detectWith((person, fullName) -> person.getFullName().equals(fullName), "John Smith");
       

      Example using an anonymous inner class:

       Person person =
           people.detectWith(new Predicate2<Person, String>()
           {
               public boolean accept(Person person, String fullName)
               {
                   return person.getFullName().equals(fullName);
               }
           }, "John Smith");
       
      Specified by:
      detectWith in interface RichIterable<T>
      Overrides:
      detectWith in class AbstractRichIterable<T>
    • detectOptional

      public Optional<T> detectOptional(Predicate<? super T> predicate)
      Description copied from interface: RichIterable
      Returns the first element of the iterable for which the predicate evaluates to true as an Optional. This method is commonly called find.

      Example using a Java 8 lambda expression:

       Person person =
           people.detectOptional(person -> person.getFirstName().equals("John") && person.getLastName().equals("Smith"));
       

      Specified by:
      detectOptional in interface RichIterable<T>
      Overrides:
      detectOptional in class AbstractRichIterable<T>
    • detectWithOptional

      public <P> Optional<T> detectWithOptional(Predicate2<? super T,? super P> predicate, P parameter)
      Description copied from interface: RichIterable
      Returns the first element that evaluates to true for the specified predicate2 and parameter as an Optional.

      Example using a Java 8 lambda expression:

       Optional<Person> person =
           people.detectWithOptional((person, fullName) -> person.getFullName().equals(fullName), "John Smith");
       

      Specified by:
      detectWithOptional in interface RichIterable<T>
      Overrides:
      detectWithOptional in class AbstractRichIterable<T>