Skip to content

Commit

Permalink
(#240) Exploit generic variance to improve user experience
Browse files Browse the repository at this point in the history
  • Loading branch information
victornoel committed May 2, 2021
1 parent 181db92 commit 37cac65
Show file tree
Hide file tree
Showing 15 changed files with 33 additions and 33 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@
<dependency>
<groupId>org.cactoos</groupId>
<artifactId>cactoos</artifactId>
<version>0.49</version>
<version>0.50</version>
</dependency>
<dependency>
<groupId>org.hamcrest</groupId>
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/org/llorllale/cactoos/matchers/Assertion.java
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ public final class Assertion<T> {
/**
* Matcher.
*/
private final Matcher<T> matcher;
private final Matcher<? super T> matcher;

/**
* Ctor.
Expand All @@ -91,7 +91,7 @@ public final class Assertion<T> {
* @param matcher Tester.
*/
public Assertion(
final String msg, final T test, final Matcher<T> matcher
final String msg, final T test, final Matcher<? super T> matcher
) {
this.msg = msg;
this.test = test;
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/llorllale/cactoos/matchers/HasEntry.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
* @param <V> Type of value.
* @since 1.0.0
*/
public final class HasEntry<K, V> extends MatcherEnvelope<Map<K, V>> {
public final class HasEntry<K, V> extends MatcherEnvelope<Map<? extends K, ? extends V>> {
/**
* Ctor.
* @param key The expected key.
Expand Down
7 changes: 3 additions & 4 deletions src/main/java/org/llorllale/cactoos/matchers/HasLines.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@
* separator.
*
* @since 1.0.0
* @checkstyle ProtectedMethodInFinalClassCheck (200 lines)
*/
public final class HasLines extends MatcherEnvelope<String> {

Expand Down Expand Up @@ -76,7 +75,7 @@ public HasLines(final Iterable<String> lns) {
* @param lns The expected lines to be present.
*/
public HasLines(
final BiFunc<Collection<String>, Collection<String>, Boolean> fnc,
final BiFunc<? super Collection<String>, ? super Collection<String>, Boolean> fnc,
final Scalar<String> sep,
final Collection<String> lns
) {
Expand All @@ -96,9 +95,9 @@ public HasLines(
* @param split The function to split the text which came for testing.
*/
private HasLines(
final BiFunc<Collection<String>, Collection<String>, Boolean> match,
final BiFunc<? super Collection<String>, ? super Collection<String>, Boolean> match,
final Collection<String> expected,
final Func<String, Collection<String>> split
final Func<? super String, ? extends Collection<String>> split
) {
super(
new MatcherOf<>(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ public HasProperty(final String key) {
*
* @param entr Entry matcher.
*/
private HasProperty(final Matcher<Map.Entry<String, String>> entr) {
private HasProperty(final Matcher<? super Map.Entry<String, String>> entr) {
super(
new MatcherOf<>(
properties -> new HasValuesMatching<>(
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/org/llorllale/cactoos/matchers/HasValue.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
* @param <T> Type of result
* @since 0.2
*/
public final class HasValue<T> extends MatcherEnvelope<Scalar<T>> {
public final class HasValue<T> extends MatcherEnvelope<Scalar<? extends T>> {

/**
* Ctor.
Expand All @@ -50,7 +50,7 @@ public HasValue(final T value) {
* Ctor.
* @param mtr Matcher of the value
*/
public HasValue(final Matcher<T> mtr) {
public HasValue(final Matcher<? super T> mtr) {
super(
new MatcherOf<>(
scalar -> mtr.matches(scalar.value()),
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/org/llorllale/cactoos/matchers/HasValues.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
* @param <X> Type of item.
* @since 1.0.0
*/
public final class HasValues<X> extends MatcherEnvelope<Iterable<X>> {
public final class HasValues<X> extends MatcherEnvelope<Iterable<? extends X>> {
/**
* Ctor.
* @param expected The expected values within unit test.
Expand All @@ -58,7 +58,7 @@ public HasValues(final X... expected) {
* Ctor.
* @param expected The expected values within unit test.
*/
public HasValues(final Iterable<X> expected) {
public HasValues(final Iterable<? extends X> expected) {
super(
new MatcherOf<>(
actual -> new ListOf<>(actual).containsAll(new ListOf<>(expected)),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,13 @@
* @param <X> Type of item.
* @since 1.0.0
*/
public final class HasValuesMatching<X> extends MatcherEnvelope<Iterable<X>> {
public final class HasValuesMatching<X> extends MatcherEnvelope<Iterable<? extends X>> {
/**
* Ctor.
* @param fnc The function to match at least one element within the
* {@link Iterable}.
*/
public HasValuesMatching(final Func<X, Boolean> fnc) {
public HasValuesMatching(final Func<? super X, Boolean> fnc) {
super(
new MatcherOf<>(
actual -> new Or(fnc, actual).value(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
* @since 0.2
*/
@SuppressWarnings("PMD.AvoidDuplicateLiterals")
public final class IsApplicable<X, Y> extends MatcherEnvelope<Func<X, Y>> {
public final class IsApplicable<X, Y> extends MatcherEnvelope<Func<? super X, ? extends Y>> {

/**
* Ctor.
Expand All @@ -55,7 +55,7 @@ public IsApplicable(final X inpt, final Y result) {
* @param input Input for the function
* @param mtr Matcher of the text
*/
public IsApplicable(final X input, final Matcher<Y> mtr) {
public IsApplicable(final X input, final Matcher<? super Y> mtr) {
super(
new MatcherOf<>(
func -> mtr.matches(
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/org/llorllale/cactoos/matchers/IsEntry.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
* @param <V> Type of value
* @since 1.0.0
*/
public final class IsEntry<K, V> extends MatcherEnvelope<Map.Entry<K, V>> {
public final class IsEntry<K, V> extends MatcherEnvelope<Map.Entry<? extends K, ? extends V>> {

/**
* Ctor.
Expand All @@ -58,7 +58,7 @@ public IsEntry(final K key, final V value) {
* @param key Matcher for key.
* @param value Matcher for value.
*/
public IsEntry(final Matcher<K> key, final Matcher<V> value) {
public IsEntry(final Matcher<? super K> key, final Matcher<? super V> value) {
super(
new MatcherOf<>(
entry -> new And(
Expand Down
7 changes: 3 additions & 4 deletions src/main/java/org/llorllale/cactoos/matchers/Matches.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,10 @@
* </pre>
*
* @param <X> Type of item.
* @param <M> Type of tested matcher.
* @since 1.0.0
* @checkstyle ProtectedMethodInFinalClassCheck (200 lines)
*/
public final class Matches<X, M extends Matcher<X>> extends TypeSafeMatcher<M> {
public final class Matches<X> extends TypeSafeMatcher<Matcher<? super X>> {

/**
* The testing arguments for the target matcher.
Expand All @@ -74,12 +73,12 @@ public void describeTo(final Description desc) {
}

@Override
protected boolean matchesSafely(final M matcher) {
protected boolean matchesSafely(final Matcher<? super X> matcher) {
return matcher.matches(this.args);
}

@Override
protected void describeMismatchSafely(final M matcher, final Description dsc) {
protected void describeMismatchSafely(final Matcher<? super X> matcher, final Description dsc) {
matcher.describeTo(dsc);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -67,14 +67,14 @@ public final class MatchesBefore<T> extends TypeSafeDiagnosingMatcher<T> {
/**
* The matcher.
*/
private final Matcher<T> matcher;
private final Matcher<? super T> matcher;

/**
* Ctor.
* @param mllsc Timeout.
* @param mtchr Matcher.
*/
public MatchesBefore(final long mllsc, final Matcher<T> mtchr) {
public MatchesBefore(final long mllsc, final Matcher<? super T> mtchr) {
super();
this.millisec = mllsc;
this.matcher = mtchr;
Expand Down
10 changes: 6 additions & 4 deletions src/main/java/org/llorllale/cactoos/matchers/Mismatches.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,11 @@
* }</pre>
*
* @param <X> Type of item.
* @param <M> Type of tested matcher.
* @since 1.0.0
* @checkstyle ProtectedMethodInFinalClassCheck (200 lines)
*/
public final class Mismatches<X, M extends Matcher<X>> extends
TypeSafeDiagnosingMatcher<M> {
public final class Mismatches<X> extends
TypeSafeDiagnosingMatcher<Matcher<? super X>> {

/**
* Multiline start.
Expand Down Expand Up @@ -140,7 +139,10 @@ public void describeTo(final Description desc) {
}

@Override
protected boolean matchesSafely(final M matcher, final Description dsc) {
protected boolean matchesSafely(
final Matcher<? super X> matcher,
final Description dsc
) {
boolean mismatch;
try {
new Assertion<>("", this.args, matcher).affirm();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
* @since 0.24
* @checkstyle JavadocMethodCheck (500 lines)
*/
public final class RunsInThreads<T> extends TypeSafeDiagnosingMatcher<Func<T, Boolean>> {
public final class RunsInThreads<T> extends TypeSafeDiagnosingMatcher<Func<? super T, Boolean>> {

/**
* Input.
Expand Down Expand Up @@ -88,7 +88,7 @@ public RunsInThreads(final T object, final int threads) {

@Override
public boolean matchesSafely(
final Func<T, Boolean> func,
final Func<? super T, Boolean> func,
final Description desc
) {
final ExecutorService service = Executors.newFixedThreadPool(
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/org/llorllale/cactoos/matchers/Throws.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
* @checkstyle ProtectedMethodInFinalClassCheck (200 lines)
*/
@SuppressWarnings("PMD.AvoidDuplicateLiterals")
public final class Throws<T> extends TypeSafeDiagnosingMatcher<Scalar<T>> {
public final class Throws<T> extends TypeSafeDiagnosingMatcher<Scalar<? extends T>> {

/**
* The expected exception message.
Expand Down Expand Up @@ -109,7 +109,7 @@ public void describeTo(final Description dsc) {
{ "PMD.AvoidCatchingGenericException", "PMD.AvoidCatchingThrowable" }
)
protected boolean matchesSafely(
final Scalar<T> obj,
final Scalar<? extends T> obj,
final Description dsc
) {
// @checkstyle IllegalCatchCheck (20 lines)
Expand Down

0 comments on commit 37cac65

Please sign in to comment.