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

Use Fallback instead of Func #1615

Merged
merged 5 commits into from
Jul 22, 2021
Merged
Show file tree
Hide file tree
Changes from all 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
18 changes: 18 additions & 0 deletions src/main/java/org/cactoos/Fallback.java
Original file line number Diff line number Diff line change
Expand Up @@ -104,4 +104,22 @@ public int support(final Throwable exception) {
).intValue();
}
}

/**
* No fallback. Throw unchecked exception.
*
* @param <X> Type of result.
* @since 1.0
*/
final class None<X> implements Fallback<X> {
@Override
public int support(final Throwable exception) {
return 0;
}

@Override
public X apply(final Throwable input) throws Exception {
throw new IllegalStateException(input);
}
}
}
47 changes: 18 additions & 29 deletions src/main/java/org/cactoos/bytes/UncheckedBytes.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,40 +24,34 @@
package org.cactoos.bytes;

import org.cactoos.Bytes;
import org.cactoos.Func;
import org.cactoos.func.UncheckedFunc;
import org.cactoos.Fallback;
import org.cactoos.scalar.ScalarWithFallback;
import org.cactoos.scalar.Unchecked;

/**
* Bytes that doesn't throw checked {@link Exception}.
*
* <p>There is no thread-safety guarantee.
*
* @since 0.3
* @todo #1615:30m Extract fallback logic for Bytes
* to a separate class in accordance
* to other XXXWithFallback classes.
* Leave only basic exception handling in this class.
*/
public final class UncheckedBytes implements Bytes {

/**
* Original bytes.
* Scalar with fallback.
*/
private final Bytes bytes;

/**
* Fallback.
*/
private final Func<? super Exception, byte[]> fallback;
private final Unchecked<byte[]> scalar;

/**
* Ctor.
* @param bts Encapsulated bytes
*/
@SuppressWarnings("PMD.AvoidThrowingRawExceptionTypes")
public UncheckedBytes(final Bytes bts) {
this(
bts,
error -> {
throw new RuntimeException(error);
}
);
this(bts, new Fallback.None<>());
}

/**
Expand All @@ -66,23 +60,18 @@ public UncheckedBytes(final Bytes bts) {
* @param fbk Fallback
* @since 0.5
*/
public UncheckedBytes(final Bytes bts,
final Func<? super Exception, byte[]> fbk) {
this.bytes = bts;
this.fallback = fbk;
public UncheckedBytes(
final Bytes bts,
final Fallback<byte[]> fbk
) {
this.scalar = new Unchecked<byte[]>(
new ScalarWithFallback<byte[]>(bts::asBytes, fbk)
);
}

@Override
@SuppressWarnings("PMD.AvoidCatchingGenericException")
public byte[] asBytes() {
byte[] data;
try {
data = this.bytes.asBytes();
// @checkstyle IllegalCatchCheck (1 line)
} catch (final Exception ex) {
data = new UncheckedFunc<>(this.fallback).apply(ex);
}
return data;
return this.scalar.value();
}

}
28 changes: 24 additions & 4 deletions src/test/java/org/cactoos/bytes/UncheckedBytesTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
package org.cactoos.bytes;

import java.io.IOException;
import org.cactoos.Fallback;
import org.cactoos.Text;
import org.cactoos.text.TextOf;
import org.hamcrest.core.IsEqual;
Expand All @@ -36,11 +37,12 @@
*
* @since 0.3
* @checkstyle JavadocMethodCheck (500 lines)
* @checkstyle ClassDataAbstractionCouplingCheck (500 lines)
*/
public final class UncheckedBytesTest {
final class UncheckedBytesTest {

@Test
public void rethrowsCheckedToUncheckedException() {
void rethrowsCheckedToUncheckedException() {
new Assertion<>(
"Must rethrow checked to unchecked exception",
() -> new UncheckedBytes(
Expand All @@ -53,14 +55,32 @@ public void rethrowsCheckedToUncheckedException() {
}

@Test
public void worksNormallyWhenNoExceptionIsThrown() throws Exception {
void worksNormallyWhenNoExceptionIsThrown() throws Exception {
final Text source = new TextOf("hello, cactoos!");
new Assertion<>(
"Must works normally when no exception is thrown",
"Must work normally when no exception is thrown",
new UncheckedBytes(
new BytesOf(source)
).asBytes(),
new IsEqual<>(new BytesOf(source).asBytes())
).affirm();
}

@Test
void worksWithFallback() {
final byte[] empty = {};
new Assertion<>(
"Must work with fallback",
new UncheckedBytes(
() -> {
throw new IOException("OK");
},
new Fallback.From<>(
IOException.class,
ex -> empty
)
).asBytes(),
new IsEqual<>(empty)
).affirm();
}
}