1 /*
2 BSD License
3
4 Copyright (c) 2000-2006, www.hamcrest.org
5 All rights reserved.
6
7 Redistribution and use in source and binary forms, with or without
8 modification, are permitted provided that the following conditions are met:
9
10 Redistributions of source code must retain the above copyright notice, this list of
11 conditions and the following disclaimer. Redistributions in binary form must reproduce
12 the above copyright notice, this list of conditions and the following disclaimer in
13 the documentation and/or other materials provided with the distribution.
14
15 Neither the name of Hamcrest nor the names of its contributors may be used to endorse
16 or promote products derived from this software without specific prior written
17 permission.
18
19 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
20 EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21 OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
22 SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
23 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
24 TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
25 BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY
27 WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
28 DAMAGE.
29 */
30 package dev.aherscu.qa.testing.utils.assertions.impl.matcher;
31
32 import static org.hamcrest.core.IsEqual.equalTo;
33
34 import java.util.Collection;
35
36 import org.hamcrest.Description;
37 import org.hamcrest.Matcher;
38
39 /**
40 * Matches if collection size satisfies a nested matcher.
41 */
42 public class IsCollectionWithSize<E>
43 extends CollectionMatcher<Collection<? extends E>> {
44 private final Matcher<? super Integer> sizeMatcher;
45
46 public IsCollectionWithSize(Matcher<? super Integer> sizeMatcher) {
47 this.sizeMatcher = sizeMatcher;
48 }
49
50 @Override
51 public boolean matchesSafely(Collection<? extends E> item) {
52 return sizeMatcher.matches(item.size());
53 }
54
55 @Override
56 public void describeTo(Description description) {
57 description.appendText("a collection with size ")
58 .appendDescriptionOf(sizeMatcher);
59 }
60
61 /**
62 * Does collection size satisfy a given matcher?
63 */
64 public static <E> Matcher<? super Collection<? extends E>> hasSize(
65 Matcher<? super Integer> size) {
66 return new IsCollectionWithSize<E>(size);
67 }
68
69 /**
70 * This is a shortcut to the frequently used hasSize(equalTo(x)).
71 *
72 * For example, assertThat(hasSize(equal_to(x))) vs. assertThat(hasSize(x))
73 */
74 public static <E> Matcher<? super Collection<? extends E>> hasSize(
75 int size) {
76 Matcher<? super Integer> matcher = equalTo(size);
77 return IsCollectionWithSize.hasSize(matcher);
78 }
79 }