Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(#1533) Exploit generic variance in package bytes #1557

Merged
merged 6 commits into from
Mar 14, 2021
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/main/java/org/cactoos/bytes/CheckedBytes.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,14 @@ public final class CheckedBytes<E extends Exception> implements Bytes {
/**
* Function that wraps exception of {@link #origin} to the required type.
*/
private final Func<Exception, E> func;
private final Func<? super Exception, ? extends E> func;

/**
* Ctor.
* @param orig Origin bytes.
* @param fnc Function that wraps exceptions.
*/
public CheckedBytes(final Bytes orig, final Func<Exception, E> fnc) {
public CheckedBytes(final Bytes orig, final Func<? super Exception, ? extends E> fnc) {
this.origin = orig;
this.func = fnc;
}
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/org/cactoos/bytes/UncheckedBytes.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public final class UncheckedBytes implements Bytes {
/**
* Fallback.
*/
private final Func<Exception, byte[]> fallback;
private final Func<? super Exception, byte[]> fallback;

/**
* Ctor.
Expand All @@ -67,7 +67,7 @@ public UncheckedBytes(final Bytes bts) {
* @since 0.5
*/
public UncheckedBytes(final Bytes bts,
final Func<Exception, byte[]> fbk) {
final Func<? super Exception, byte[]> fbk) {
this.bytes = bts;
this.fallback = fbk;
}
Expand Down
3 changes: 3 additions & 0 deletions src/main/java/org/cactoos/collection/package-info.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,8 @@
* Collections, tests.
*
* @since 0.14
* @todo #1533:30min Continue to exploit generic variance for package
* org.cactoos.collection to ensure typing works as best as
* possible as it is explained in #1533 issue.
baudoliver7 marked this conversation as resolved.
Show resolved Hide resolved
*/
package org.cactoos.collection;
8 changes: 4 additions & 4 deletions src/main/java/org/cactoos/scalar/Checked.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,20 +44,20 @@ public final class Checked<T, E extends Exception> implements Scalar<T> {
/**
* Function that wraps exception.
*/
private final Func<Exception, E> func;
private final Func<? super Exception, ? extends E> func;

/**
* Original scalar.
*/
private final Scalar<T> origin;
private final Scalar<? extends T> origin;

/**
* Ctor.
* @param scalar Encapsulated scalar
* @param fnc Func that wraps exception
*/
public Checked(final Scalar<T> scalar,
final Func<Exception, E> fnc) {
public Checked(final Scalar<? extends T> scalar,
final Func<? super Exception, ? extends E> fnc) {
baudoliver7 marked this conversation as resolved.
Show resolved Hide resolved
this.func = fnc;
this.origin = scalar;
}
Expand Down
27 changes: 27 additions & 0 deletions src/test/java/org/cactoos/scalar/CheckedTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,12 @@

import java.io.FileNotFoundException;
import java.io.IOException;
import java.nio.channels.AcceptPendingException;
import org.hamcrest.MatcherAssert;
import org.hamcrest.Matchers;
import org.hamcrest.core.IsEqual;
import org.junit.Test;
import org.llorllale.cactoos.matchers.Assertion;

/**
* Test case for {@link Checked}.
Expand All @@ -46,6 +49,30 @@ public void runtimeExceptionGoesOut() throws Exception {
).value();
}

@Test(expected = IllegalStateException.class)
baudoliver7 marked this conversation as resolved.
Show resolved Hide resolved
public void usesGenericVarianceOnExceptionTypes() throws Exception {
new Checked<String, IllegalStateException>(
() -> {
throw new IllegalStateException();
},
(Throwable ex) -> {
return new AcceptPendingException();
}
).value();
}

@SuppressWarnings("PMD.AvoidDuplicateLiterals")
public void usesContravarianceOnResultType() throws Exception {
new Assertion<>(
"Must use contravariance on result",
new Checked<CharSequence, IOException>(
() -> new String("contravariance"),
IOException::new
).value(),
new IsEqual<>("contravariance")
);
}

@Test(expected = IOException.class)
public void throwsIoException() throws Exception {
new Checked<>(
Expand Down