Class StreamMatchersExtensions


  • @ThreadSafe
    public class StreamMatchersExtensions
    extends StreamMatchers
    Drop-in replacement for Java-8-matchers' StreamMatchers.

    Can be statically imported along Hamcrest's Matchers.

    Author:
    aherscu
    • Constructor Detail

      • StreamMatchersExtensions

        public StreamMatchersExtensions()
    • Method Detail

      • adapt

        public static <T> org.hamcrest.Matcher<Stream<T>> adapt​(Supplier<Collection<T>> supplier,
                                                                org.hamcrest.Matcher<Iterable<T>> matcher)
        Adapts a Stream to be matched with an Iterable matcher. Use with combinable matchers.
        Type Parameters:
        T - type of objects produced by the Stream
        Parameters:
        supplier - the specific collection; e.g. ArrayList::new
        matcher - against which to compare objects from the Stream
        Returns:
        adapted Iterable matcher
      • adaptedStream

        public static <T,​R> org.hamcrest.Matcher<Stream<T>> adaptedStream​(Function<T,​R> converter,
                                                                                org.hamcrest.Matcher<Stream<R>> matcher)
        Adapts a matcher of stream of objects by specified converter function.
        Type Parameters:
        T - type to be adapted
        R - type after adaption
        Parameters:
        converter - the converter function
        matcher - the matcher
        Returns:
        adapted matcher
      • anyStream

        public static <T> org.hamcrest.Matcher<Stream<T>> anyStream​(String... message)
        Type Parameters:
        T - dummy matcher that matches anything
        Parameters:
        message - optional, something to be added to description
        Returns:
        something that matches any stream
      • counts

        public static <T> org.hamcrest.Matcher<Stream<T>> counts​(long expectedNumberOfItems)
      • counts

        public static <T> org.hamcrest.Matcher<Stream<T>> counts​(org.hamcrest.Matcher<Long> expectedNumberOfItems)
        Type Parameters:
        T - type of stream item
        Parameters:
        expectedNumberOfItems - the expected number of items
        Returns:
        the counting matcher
      • emptyStream

        public static <T> org.hamcrest.Matcher<Stream<T>> emptyStream()
        Fixes method signature from StreamMatchers.empty().
        Type Parameters:
        T - type of objects
        Returns:
        an empty stream matcher
      • hasItemsMatching

        public static <T> org.hamcrest.Matcher<Stream<T>> hasItemsMatching​(List<org.hamcrest.Matcher<T>> matchers)
        A matcher for Stream of objects, must produce objects that match the given Matchers in given order. Gaps between matched objects are acceptable.
        Type Parameters:
        T - type of objects produced by the Stream
        Parameters:
        matchers - against which to compare objects from the Stream
        Returns:
        matcher for stream
      • hasItemsMatching

        @SafeVarargs
        public static <T> org.hamcrest.Matcher<Stream<T>> hasItemsMatching​(org.hamcrest.Matcher<T>... values)
        A matcher for Stream of objects, must produce objects that match the given Matchers in given order. Gaps between matched objects are acceptable.
        Type Parameters:
        T - type of objects produced by the Stream
        Parameters:
        values - against which to compare objects from the Stream
        Returns:
        matcher for stream
      • hasItemsMatchingInAnyOrder

        public static <T> org.hamcrest.Matcher<Stream<T>> hasItemsMatchingInAnyOrder​(Set<org.hamcrest.Matcher<T>> matchers)
        A matcher for Stream of objects, must produce objects that match the given Matchers. Gaps between matched objects are acceptable.
        Type Parameters:
        T - type of objects produced by the Stream
        Parameters:
        matchers - against which to compare objects from the Stream
        Returns:
        matcher for stream
      • hasItemsMatchingInAnyOrder

        @SafeVarargs
        public static <T> org.hamcrest.Matcher<Stream<T>> hasItemsMatchingInAnyOrder​(org.hamcrest.Matcher<T>... values)
        A matcher for Stream of objects, must produce objects that match the given Matchers. Gaps between matched objects are acceptable.
        Type Parameters:
        T - type of objects produced by the Stream
        Parameters:
        values - against which to compare objects from the Stream
        Returns:
        matcher for stream
      • hasSpecificItems

        public static <T> org.hamcrest.Matcher<Stream<T>> hasSpecificItems​(List<T> values)
        Wraps each of specified values into CoreMatchers.is(org.hamcrest.Matcher<T>).
        Type Parameters:
        T - type of objects in stream
        Parameters:
        values - expected objects
        Returns:
        matcher for stream
        See Also:
        hasItemsMatching(List)
      • hasSpecificItems

        @SafeVarargs
        public static <T> org.hamcrest.Matcher<Stream<T>> hasSpecificItems​(T... values)
        Wraps each of specified values into CoreMatchers.is(org.hamcrest.Matcher<T>).
        Type Parameters:
        T - type of objects in stream
        Parameters:
        values - expected objects
        Returns:
        matcher for stream
        See Also:
        hasItemsMatching(List)
      • hasSpecificItemsInAnyOrder

        @SafeVarargs
        public static <T> org.hamcrest.Matcher<Stream<T>> hasSpecificItemsInAnyOrder​(T... values)
        Wraps each of specified values into CoreMatchers.is(org.hamcrest.Matcher<T>).
        Type Parameters:
        T - type of objects in stream
        Parameters:
        values - expected objects
        Returns:
        matcher for stream
        See Also:
        hasItemsMatchingInAnyOrder(Set)
      • hasSpecificItemsInAnyOrder

        public static <T> org.hamcrest.Matcher<Stream<T>> hasSpecificItemsInAnyOrder​(Set<T> values)
        Wraps each of specified values into CoreMatchers.is(org.hamcrest.Matcher<T>).
        Type Parameters:
        T - type of objects in stream
        Parameters:
        values - expected objects
        Returns:
        matcher for stream
        See Also:
        hasItemsMatchingInAnyOrder(Set)
      • orderedStream

        public static <T> org.hamcrest.Matcher<Stream<T>> orderedStream​(com.google.common.collect.Ordering<T> ordering)
        Matches an ordered Stream by specified ordering.
        Parameters:
        ordering - the expected ordering
        Returns:
        the ordering matcher
      • scanningAtMost

        public static <T> org.hamcrest.Matcher<Stream<T>> scanningAtMost​(int limit,
                                                                         org.hamcrest.Matcher<Stream<T>> matcher)
        Limits amount of scanned objects produced by a stream.
        Type Parameters:
        T - type of objects
        Parameters:
        limit - how many objects to scan at most
        matcher - delegate matcher
        Returns:
        a stream limiting matcher