Class FilteredIterable<T>

  • Type Parameters:
    T - the Iterable generic type
    All Implemented Interfaces:
    java.lang.Iterable<T>

    public class FilteredIterable<T>
    extends java.lang.Object
    implements java.lang.Iterable<T>
    Adds a fluent filtering API to any Iterable.
    Version:
    $Revision$ $Date$
    • Field Summary

      Fields 
      Modifier and Type Field Description
      private static FilteredIterable EMPTY
      A default FilteredIterable static instance that iterates over an empty collection.
      private java.lang.Iterable<? extends T> iterable
      The Iterable has to be filtered.
      private UnaryAnd<T> predicate
      The predicate used to test input Iterable elements.
    • Constructor Summary

      Constructors 
      Modifier Constructor Description
      private FilteredIterable​(java.lang.Iterable<? extends T> iterable)
      Create a new FilteredIterable.
    • Field Detail

      • EMPTY

        private static final FilteredIterable EMPTY
        A default FilteredIterable static instance that iterates over an empty collection.
      • iterable

        private final java.lang.Iterable<? extends T> iterable
        The Iterable has to be filtered.
      • predicate

        private UnaryAnd<T> predicate
        The predicate used to test input Iterable elements.
    • Constructor Detail

      • FilteredIterable

        private FilteredIterable​(java.lang.Iterable<? extends T> iterable)
        Create a new FilteredIterable.
        Parameters:
        iterable - wrapped
    • Method Detail

      • iterator

        public java.util.Iterator<T> iterator()
        Specified by:
        iterator in interface java.lang.Iterable<T>
      • toString

        public java.lang.String toString()
        Overrides:
        toString in class java.lang.Object
      • retain

        public FilteredIterable<T> retain​(UnaryPredicate<? super T> predicate)
        Retain only elements matching predicate.
        Parameters:
        predicate - filter, non-null
        Returns:
        this, fluently
      • retain

        public <U> FilteredIterable<U> retain​(java.lang.Class<U> type)
        Retain elements of a given type with type-safety.
        Type Parameters:
        U - the input Class generic type.
        Parameters:
        type - filter, non-null
        Returns:
        new FilteredIterable instance that delegates to this
      • retain

        public FilteredIterable<T> retain​(java.lang.Class<?>... ofType)
        Retain elements of any of specified types.
        Parameters:
        ofType - filter, non-null
        Returns:
        this, fluently
      • of

        public static <T> FilteredIterable<T> of​(java.lang.Iterable<T> iterable)
        Get a FilteredIterable of iterable. If wrapped is null, result will also be null. A FilteredIterable argument will be passed back directly; any other argument will be wrapped in a new FilteredIterable object.
        Type Parameters:
        T - the input iterable generic type
        Parameters:
        iterable - wrapped
        Returns:
        FilteredIterable
      • empty

        public static <T> FilteredIterable<T> empty()
        Get an empty FilteredIterable.
        Type Parameters:
        T - the expected Iterable generic type.
        Returns:
        FilteredIterable