|
| 1 | +package org.hamcrest; |
| 2 | + |
| 3 | +import org.hamcrest.collection.IsCollectionContaining; |
| 4 | + |
| 5 | +/** |
| 6 | + * @author 2015 http://www.hamcrest.com |
| 7 | + */ |
| 8 | +public class MatchingCollections { |
| 9 | + /** |
| 10 | + * Creates a matcher for {@link Iterable}s that only matches when a single pass over the |
| 11 | + * examined {@link Iterable} yields at least one item that is matched by the specified |
| 12 | + * <code>itemMatcher</code>. Whilst matching, the traversal of the examined {@link Iterable} |
| 13 | + * will stop as soon as a matching item is found. |
| 14 | + * For example: |
| 15 | + * <pre>assertThat(Arrays.asList("foo", "bar"), hasItem(startsWith("ba")))</pre> |
| 16 | + * |
| 17 | + * @param itemMatcher |
| 18 | + * the matcher to apply to items provided by the examined {@link Iterable} |
| 19 | + */ |
| 20 | + public static <T> Matcher<Iterable<? super T>> hasItem(Matcher<? super T> itemMatcher) { |
| 21 | + return IsCollectionContaining.hasItem(itemMatcher); |
| 22 | + } |
| 23 | + |
| 24 | + /** |
| 25 | + * Creates a matcher for {@link Iterable}s that only matches when a single pass over the |
| 26 | + * examined {@link Iterable} yields at least one item that is equal to the specified |
| 27 | + * <code>item</code>. Whilst matching, the traversal of the examined {@link Iterable} |
| 28 | + * will stop as soon as a matching item is found. |
| 29 | + * For example: |
| 30 | + * <pre>assertThat(Arrays.asList("foo", "bar"), hasItem("bar"))</pre> |
| 31 | + * |
| 32 | + * @param item |
| 33 | + * the item to compare against the items provided by the examined {@link Iterable} |
| 34 | + */ |
| 35 | + public static <T> Matcher<Iterable<? super T>> hasItem(T item) { |
| 36 | + return IsCollectionContaining.hasItem(item); |
| 37 | + } |
| 38 | + |
| 39 | + /** |
| 40 | + * Creates a matcher for {@link Iterable}s that matches when consecutive passes over the |
| 41 | + * examined {@link Iterable} yield at least one item that is matched by the corresponding |
| 42 | + * matcher from the specified <code>itemMatchers</code>. Whilst matching, each traversal of |
| 43 | + * the examined {@link Iterable} will stop as soon as a matching item is found. |
| 44 | + * For example: |
| 45 | + * <pre>assertThat(Arrays.asList("foo", "bar", "baz"), hasItems(endsWith("z"), endsWith("o")))</pre> |
| 46 | + * |
| 47 | + * @param itemMatchers |
| 48 | + * the matchers to apply to items provided by the examined {@link Iterable} |
| 49 | + */ |
| 50 | + @SafeVarargs |
| 51 | + public static <T> Matcher<Iterable<T>> hasItems(Matcher<? super T>... itemMatchers) { |
| 52 | + return IsCollectionContaining.hasItems(itemMatchers); |
| 53 | + } |
| 54 | + |
| 55 | + /** |
| 56 | + * Creates a matcher for {@link Iterable}s that matches when consecutive passes over the |
| 57 | + * examined {@link Iterable} yield at least one item that is equal to the corresponding |
| 58 | + * item from the specified <code>items</code>. Whilst matching, each traversal of the |
| 59 | + * examined {@link Iterable} will stop as soon as a matching item is found. |
| 60 | + * For example: |
| 61 | + * <pre>assertThat(Arrays.asList("foo", "bar", "baz"), hasItems("baz", "foo"))</pre> |
| 62 | + * |
| 63 | + * @param items |
| 64 | + * the items to compare against the items provided by the examined {@link Iterable} |
| 65 | + */ |
| 66 | + @SafeVarargs |
| 67 | + public static <T> Matcher<Iterable<T>> hasItems(T... items) { |
| 68 | + return IsCollectionContaining.hasItems(items); |
| 69 | + } |
| 70 | +} |
0 commit comments