|
| 1 | +package org.hamcrest.collection; |
| 2 | + |
| 3 | +import static org.hamcrest.core.IsEqual.equalTo; |
| 4 | + |
| 5 | +import java.util.ArrayList; |
| 6 | +import java.util.Arrays; |
| 7 | +import java.util.Collection; |
| 8 | +import java.util.List; |
| 9 | + |
| 10 | +import org.hamcrest.Description; |
| 11 | +import org.hamcrest.Factory; |
| 12 | +import org.hamcrest.Matcher; |
| 13 | +import org.hamcrest.TypeSafeMatcher; |
| 14 | + |
| 15 | +public class IsArrayContainingInAnyOrder<E> extends TypeSafeMatcher<E[]> { |
| 16 | + private final IsIterableContainingInAnyOrder<E> iterableMatcher; |
| 17 | + private final Collection<Matcher<? super E>> matchers; |
| 18 | + |
| 19 | + public IsArrayContainingInAnyOrder(Collection<Matcher<? super E>> matchers) { |
| 20 | + this.iterableMatcher = new IsIterableContainingInAnyOrder<E>(matchers); |
| 21 | + this.matchers = matchers; |
| 22 | + } |
| 23 | + |
| 24 | + @Override |
| 25 | + public boolean matchesSafely(E[] item) { |
| 26 | + return iterableMatcher.matches(Arrays.asList(item)); |
| 27 | + } |
| 28 | + |
| 29 | + public void describeTo(Description description) { |
| 30 | + description.appendText("is array containing in any order: ") |
| 31 | + .appendList("[ ", ", ", " ]", matchers); |
| 32 | + } |
| 33 | + |
| 34 | + @Factory |
| 35 | + public static <E> Matcher<E[]> arrayContainingInAnyOrder(Matcher<? super E>... matchers) { |
| 36 | + return arrayContainingInAnyOrder(Arrays.asList(matchers)); |
| 37 | + } |
| 38 | + |
| 39 | + |
| 40 | + @Factory |
| 41 | + public static <E> Matcher<E[]> arrayContainingInAnyOrder(Collection<Matcher<? super E>> matchers) { |
| 42 | + return new IsArrayContainingInAnyOrder<E>(matchers); |
| 43 | + } |
| 44 | + |
| 45 | + @Factory |
| 46 | + public static <E> Matcher<E[]> arrayContainingInAnyOrder(E... items) { |
| 47 | + List<Matcher<? super E>> matchers = new ArrayList<Matcher<? super E>>(); |
| 48 | + for (E item : items) { |
| 49 | + matchers.add(equalTo(item)); |
| 50 | + } |
| 51 | + return new IsArrayContainingInAnyOrder<E>(matchers); |
| 52 | + } |
| 53 | + |
| 54 | + @Factory |
| 55 | + public static <E> Matcher<E[]> arrayContainingInAnyOrder(E first, E second) { |
| 56 | + List<Matcher<? super E>> matchers = new ArrayList<Matcher<? super E>>(); |
| 57 | + matchers.add(equalTo(first)); |
| 58 | + matchers.add(equalTo(second)); |
| 59 | + return new IsArrayContainingInAnyOrder<E>(matchers); |
| 60 | + } |
| 61 | + |
| 62 | + @Factory |
| 63 | + public static <E> Matcher<E[]> arrayContainingInAnyOrder(E first, E second, E third) { |
| 64 | + List<Matcher<? super E>> matchers = new ArrayList<Matcher<? super E>>(); |
| 65 | + matchers.add(equalTo(first)); |
| 66 | + matchers.add(equalTo(second)); |
| 67 | + matchers.add(equalTo(third)); |
| 68 | + return new IsArrayContainingInAnyOrder<E>(matchers); |
| 69 | + } |
| 70 | + |
| 71 | + @Factory |
| 72 | + public static <E> Matcher<E[]> arrayContainingInAnyOrder(E first, E second, E third, E forth) { |
| 73 | + List<Matcher<? super E>> matchers = new ArrayList<Matcher<? super E>>(); |
| 74 | + matchers.add(equalTo(first)); |
| 75 | + matchers.add(equalTo(second)); |
| 76 | + matchers.add(equalTo(third)); |
| 77 | + matchers.add(equalTo(forth)); |
| 78 | + return new IsArrayContainingInAnyOrder<E>(matchers); |
| 79 | + } |
| 80 | + |
| 81 | + @Factory |
| 82 | + public static <E> Matcher<E[]> arrayContainingInAnyOrder(E first, E second, E third, E forth, E fifth) { |
| 83 | + List<Matcher<? super E>> matchers = new ArrayList<Matcher<? super E>>(); |
| 84 | + matchers.add(equalTo(first)); |
| 85 | + matchers.add(equalTo(second)); |
| 86 | + matchers.add(equalTo(third)); |
| 87 | + matchers.add(equalTo(forth)); |
| 88 | + matchers.add(equalTo(fifth)); |
| 89 | + return new IsArrayContainingInAnyOrder<E>(matchers); |
| 90 | + } |
| 91 | + |
| 92 | + @Factory |
| 93 | + public static <E> Matcher<E[]> arrayContainingInAnyOrder(E first, E second, E third, E forth, E fifth, E sixth) { |
| 94 | + List<Matcher<? super E>> matchers = new ArrayList<Matcher<? super E>>(); |
| 95 | + matchers.add(equalTo(first)); |
| 96 | + matchers.add(equalTo(second)); |
| 97 | + matchers.add(equalTo(third)); |
| 98 | + matchers.add(equalTo(forth)); |
| 99 | + matchers.add(equalTo(fifth)); |
| 100 | + matchers.add(equalTo(sixth)); |
| 101 | + return new IsArrayContainingInAnyOrder<E>(matchers); |
| 102 | + } |
| 103 | + |
| 104 | + @Factory |
| 105 | + public static <E> Matcher<E[]> arrayContainingInAnyOrder(Matcher<E> first, Matcher<? super E> second) { |
| 106 | + List<Matcher<? super E>> matchers = new ArrayList<Matcher<? super E>>(); |
| 107 | + matchers.add(first); |
| 108 | + matchers.add(second); |
| 109 | + return arrayContainingInAnyOrder(matchers); |
| 110 | + } |
| 111 | + |
| 112 | + @Factory |
| 113 | + public static <E> Matcher<E[]> arrayContainingInAnyOrder(Matcher<E> first, Matcher<? super E> second, Matcher<? super E> third) { |
| 114 | + List<Matcher<? super E>> matchers = new ArrayList<Matcher<? super E>>(); |
| 115 | + matchers.add(first); |
| 116 | + matchers.add(second); |
| 117 | + matchers.add(third); |
| 118 | + return arrayContainingInAnyOrder(matchers); |
| 119 | + } |
| 120 | + |
| 121 | + @Factory |
| 122 | + public static <E> Matcher<E[]> arrayContainingInAnyOrder(Matcher<E> first, Matcher<? super E> second, Matcher<? super E> third, Matcher<? super E> forth) { |
| 123 | + List<Matcher<? super E>> matchers = new ArrayList<Matcher<? super E>>(); |
| 124 | + matchers.add(first); |
| 125 | + matchers.add(second); |
| 126 | + matchers.add(third); |
| 127 | + matchers.add(forth); |
| 128 | + return arrayContainingInAnyOrder(matchers); |
| 129 | + } |
| 130 | + |
| 131 | + @Factory |
| 132 | + public static <E> Matcher<E[]> arrayContainingInAnyOrder(Matcher<E> first, Matcher<? super E> second, Matcher<? super E> third, Matcher<? super E> forth, Matcher<? super E> fifth) { |
| 133 | + List<Matcher<? super E>> matchers = new ArrayList<Matcher<? super E>>(); |
| 134 | + matchers.add(first); |
| 135 | + matchers.add(second); |
| 136 | + matchers.add(third); |
| 137 | + matchers.add(forth); |
| 138 | + matchers.add(fifth); |
| 139 | + return arrayContainingInAnyOrder(matchers); |
| 140 | + } |
| 141 | + |
| 142 | + @Factory |
| 143 | + public static <E> Matcher<E[]> arrayContainingInAnyOrder(Matcher<E> first, Matcher<? super E> second, Matcher<? super E> third, Matcher<? super E> forth, Matcher<? super E> fifth, Matcher<? super E> sixth) { |
| 144 | + List<Matcher<? super E>> matchers = new ArrayList<Matcher<? super E>>(); |
| 145 | + matchers.add(first); |
| 146 | + matchers.add(second); |
| 147 | + matchers.add(third); |
| 148 | + matchers.add(forth); |
| 149 | + matchers.add(fifth); |
| 150 | + matchers.add(sixth); |
| 151 | + return arrayContainingInAnyOrder(matchers); |
| 152 | + } |
| 153 | +} |
0 commit comments