Interface Traversable<T>

    • Method Summary

      All Methods Instance Methods Abstract Methods Default Methods 
      Modifier and Type Method Description
      default Optional<java.lang.Double> average()
      Calculates the average of this elements, assuming that the element type is Number.
      Traversable<T> filter​(java.util.function.Predicate<T> predicate)
      Filter out an element if it does not match the supplied predicate.
      Optional<T> first​(java.util.function.Predicate<T> predicate)
      Find the first match in the elements using the provided Predicate.
      Iterator<T> iterator()  
      Optional<T> last​(java.util.function.Predicate<T> predicate)
      Find the last match in the Iterator using the provided Predicate.
      <U> Traversable<U> map​(java.util.function.Function<T,​U> mapper)
      Perform a mapping operation on the elements in the stream.
      Traversable<T> orElse​(java.lang.Iterable<? extends T> other)
      Returns either this if it is non empty, otherwise will return the provided other.
      Traversable<T> orElse​(java.util.function.Supplier<? extends java.lang.Iterable<? extends T>> supplier)
      Returns either this if it is non empty, otherwise the provided supplier is evaluated and returned.
      Traversable<T> reject​(java.util.function.Predicate<T> predicate)
      Return a list that removes all elements that match the predicate provided.
      default Optional<java.lang.Double> sum()
      Calculates the sum of this elements.
      • Methods inherited from interface java.lang.Iterable

        forEach, spliterator
    • Method Detail

      • filter

        Traversable<T> filter​(java.util.function.Predicate<T> predicate)
        Description copied from interface: Streamable
        Filter out an element if it does not match the supplied predicate. This operation will iterate over all elements and return a new set containing only the elements where the predicate returns true for.
        Specified by:
        filter in interface Streamable<T>
        Parameters:
        predicate - the predicate to apply to the contents of this
        Returns:
        the filtered value
      • first

        Optional<T> first​(java.util.function.Predicate<T> predicate)
        Find the first match in the elements using the provided Predicate. The returned Optional is null safe and will either contain the element or be an empty Optional.

        Example:

        // the result will be an Optional with the value 2
            int firstMatch = Collection(1, 2, 3, 4)
                  .first(i -> i % 2 == 0);
        Parameters:
        predicate - the predicate to use
        Returns:
        the first match found
        Throws:
        java.lang.NullPointerException - in case that the predicate is null
      • iterator

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

        Optional<T> last​(java.util.function.Predicate<T> predicate)
        Find the last match in the Iterator using the provided Predicate. The returned Optional is null safe and will either contain the element or be an empty Optional.

        Example:

        // the result will be an Optional with the value 4
            int firstMatch = Collection(1, 2, 3, 4)
                  .last(i -> i % 2 == 0);
        Parameters:
        predicate - the predicate to use
        Returns:
        the last match found
        Throws:
        java.lang.NullPointerException - in case that the predicate is null
      • map

        <U> Traversable<U> map​(java.util.function.Function<T,​U> mapper)
        Description copied from interface: Streamable
        Perform a mapping operation on the elements in the stream.

        This operation will loop over all elements in the stream and apply the mapper method. The mapped values will be returned in as a new stream of elements.

        Specified by:
        map in interface Streamable<T>
        Type Parameters:
        U - the type of object expected as a result
        Parameters:
        mapper - the mapping functionality
        Returns:
        the mapped object
      • orElse

        Traversable<T> orElse​(java.lang.Iterable<? extends T> other)
        Returns either this if it is non empty, otherwise will return the provided other.
        Parameters:
        other - the alternative
        Returns:
        this Traversable if non empty, or other
      • orElse

        Traversable<T> orElse​(java.util.function.Supplier<? extends java.lang.Iterable<? extends T>> supplier)
        Returns either this if it is non empty, otherwise the provided supplier is evaluated and returned.
        Parameters:
        supplier - the supplier to generate the other
        Returns:
        this (@code Traversable} if non empty, otherwise other
      • reject

        Traversable<T> reject​(java.util.function.Predicate<T> predicate)
        Return a list that removes all elements that match the predicate provided.
        Parameters:
        predicate - the predicate to use
        Returns:
        the elements that do not match the predicate
        Throws:
        java.lang.NullPointerException - in case predicate is null
      • sum

        default Optional<java.lang.Double> sum()
        Calculates the sum of this elements. Supported component types are Byte, Double, Float, Integer, Long, Short, BigInteger and BigDecimal.

        Examples:

        
         API.List().sum()                          // = Optional()
         API.List(1, 2, 3).sum()                   // = Optional(6.0)
         API.List(1.0, 10e100, 2.0, -10e100).sum() // = Optional(3.0)
         API.List(1.0, Double.NaN).sum()           // = NaN
         API.List("apple", "pear").sum()           // throws an exception
         
        Returns:
        Optional(sum), or Optional() if no elements are present
        Throws:
        java.lang.ClassCastException - if the elements are not numeric
      • average

        default Optional<java.lang.Double> average()
        Calculates the average of this elements, assuming that the element type is Number. Since we do not know if the component type T is of type Number, the average() call might throw at runtime (see examples below).

        Examples

        
         API.List().average()                          // = Optional()
         API.List(1, 2, 3).average()                   // = Optional(2.0)
         API.List(1.0, 10e100, 2.0, -10e100).average() // = Optional(0.75)
         API.List(1.0, Double.NaN).average()           // = NaN
         API.List("apple", "pear").average()           // throws an exception
         
        Returns:
        Optional(average), or {@code Optional()} if no elements are present
        Throws:
        java.lang.ClassCastException - if the elements are not numeric