Skip to content

Commit

Permalink
#1644 IoCheckedBytes
Browse files Browse the repository at this point in the history
  • Loading branch information
yegor256 committed Jul 15, 2022
1 parent dc0b8a9 commit 1a3841b
Show file tree
Hide file tree
Showing 32 changed files with 274 additions and 108 deletions.
83 changes: 83 additions & 0 deletions src/main/java/org/cactoos/bytes/IoCheckedBytes.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2017-2022 Yegor Bugayenko
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package org.cactoos.bytes;

import java.io.IOException;
import org.cactoos.Bytes;
import org.cactoos.Fallback;
import org.cactoos.scalar.IoChecked;

/**
* Bytes that doesn't throw checked {@link Exception},
* but only throws {@link java.io.IOException}.
*
* <p>There is no thread-safety guarantee.
*
* @since 0.52
*/
public final class IoCheckedBytes implements Bytes {

/**
* Scalar with fallback.
*/
private final IoChecked<byte[]> scalar;

/**
* Ctor.
* @param bts Encapsulated bytes
*/
public IoCheckedBytes(final Bytes bts) {
this(bts, new Fallback.None<>());
}

/**
* Ctor.
* @param bts Encapsulated bytes
* @param fbk Fallback
* @since 0.5
*/
@SuppressWarnings({"unchecked", "PMD.AvoidRethrowingException", "PMD.AvoidCatchingThrowable"})
public IoCheckedBytes(final Bytes bts, final Fallback<byte[]> fbk) {
this.scalar = new IoChecked<>(
() -> {
byte[] ret;
try {
ret = bts.asBytes();
} catch (final IOException ex) {
throw ex;
// @checkstyle IllegalCatchCheck (1 line)
} catch (final Throwable ex) {
ret = fbk.apply(ex);
}
return ret;
}
);
}

@Override
public byte[] asBytes() throws IOException {
return this.scalar.value();
}

}
10 changes: 4 additions & 6 deletions src/main/java/org/cactoos/bytes/UncheckedBytes.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,10 @@ public UncheckedBytes(final Bytes bts) {
* @param fbk Fallback
* @since 0.5
*/
public UncheckedBytes(
final Bytes bts,
final Fallback<byte[]> fbk
) {
this.scalar = new Unchecked<byte[]>(
new ScalarWithFallback<byte[]>(bts::asBytes, fbk)
@SuppressWarnings("unchecked")
public UncheckedBytes(final Bytes bts, final Fallback<byte[]> fbk) {
this.scalar = new Unchecked<>(
new ScalarWithFallback<>(bts::asBytes, fbk)
);
}

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/cactoos/collection/NoNulls.java
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public boolean isEmpty() {

@Override
public Iterator<X> iterator() {
return new org.cactoos.iterator.NoNulls<X>(this.col.iterator());
return new org.cactoos.iterator.NoNulls<>(this.col.iterator());
}

@Override
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/cactoos/iterable/Cycled.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public final class Cycled<T> extends IterableEnvelope<T> {
*/
@SafeVarargs
public Cycled(final T... itr) {
this(new IterableOf<T>(itr));
this(new IterableOf<>(itr));
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/cactoos/iterable/IterableOf.java
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ public boolean equals(final Object other) {
() -> Iterable.class.isAssignableFrom(other.getClass()),
() -> {
final Iterable<X> compared = (Iterable<X>) other;
return new ScalarWithFallback<Boolean>(
return new ScalarWithFallback<>(
new And(
(X value) -> true,
new Matched<>(
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/cactoos/iterable/Sticky.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public Sticky(final X... src) {
*/
public Sticky(final Iterable<? extends X> iterable) {
super(
new IterableOf<X>(
new IterableOf<>(
new Mapped<>(
Iterable::iterator,
new org.cactoos.scalar.Sticky<>(
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/cactoos/iterator/RangeOf.java
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@
public RangeOf(final T min, final T max,
final Func<? super T, ? extends T> incrementor) {
this.inc = new UncheckedFunc<>(incrementor);
this.value = new AtomicReference<T>(min);
this.value = new AtomicReference<>(min);
this.max = max;
}

Expand Down
8 changes: 4 additions & 4 deletions src/main/java/org/cactoos/number/MaxOf.java
Original file line number Diff line number Diff line change
Expand Up @@ -66,19 +66,19 @@ public MaxOf(final Number... src) {
public MaxOf(final Iterable<? extends Number> src) {
super(
new NumberOfScalars(
new Reduced<Long>(
new Reduced<>(
Math::max,
new Mapped<>((Number n) -> n::longValue, src)
),
new Reduced<Integer>(
new Reduced<>(
Math::max,
new Mapped<>((Number n) -> n::intValue, src)
),
new Reduced<Float>(
new Reduced<>(
Math::max,
new Mapped<>((Number n) -> n::floatValue, src)
),
new Reduced<Double>(
new Reduced<>(
Math::max,
new Mapped<>((Number n) -> n::doubleValue, src)
)
Expand Down
8 changes: 4 additions & 4 deletions src/main/java/org/cactoos/number/MinOf.java
Original file line number Diff line number Diff line change
Expand Up @@ -66,19 +66,19 @@ public MinOf(final Number... src) {
public MinOf(final Iterable<? extends Number> src) {
super(
new NumberOfScalars(
new Reduced<Long>(
new Reduced<>(
Math::min,
new Mapped<>((Number n) -> n::longValue, src)
),
new Reduced<Integer>(
new Reduced<>(
Math::min,
new Mapped<>((Number n) -> n::intValue, src)
),
new Reduced<Float>(
new Reduced<>(
Math::min,
new Mapped<>((Number n) -> n::floatValue, src)
),
new Reduced<Double>(
new Reduced<>(
Math::min,
new Mapped<>((Number n) -> n::doubleValue, src)
)
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/cactoos/scalar/ItemAt.java
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ public ItemAt(
final Func<? super Iterable<? extends T>, ? extends T> fallback,
final Iterable<? extends T> iterable
) {
this.saved = new Sticky<T>(
this.saved = new Sticky<>(
() -> {
final T ret;
if (position < 0) {
Expand Down
86 changes: 86 additions & 0 deletions src/test/java/org/cactoos/bytes/IoCheckedBytesTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2017-2022 Yegor Bugayenko
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
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;
import org.junit.jupiter.api.Test;
import org.llorllale.cactoos.matchers.Assertion;
import org.llorllale.cactoos.matchers.Throws;

/**
* Test case for {@link IoCheckedBytes}.
*
* @since 0.52
* @checkstyle JavadocMethodCheck (500 lines)
* @checkstyle ClassDataAbstractionCouplingCheck (500 lines)
*/
final class IoCheckedBytesTest {

@Test
void rethrowsCheckedToUncheckedException() {
new Assertion<>(
"Must rethrow checked to io-checked exception",
() -> new IoCheckedBytes(
() -> {
throw new IOException("intended");
}
).asBytes(),
new Throws<>(IOException.class)
).affirm();
}

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

@Test
void worksWithFallback() throws IOException {
final byte[] empty = {};
new Assertion<>(
"Must work with fallback",
new IoCheckedBytes(
() -> {
throw new IllegalArgumentException("OK");
},
new Fallback.From<>(
IllegalArgumentException.class,
ex -> empty
)
).asBytes(),
new IsEqual<>(empty)
).affirm();
}
}
6 changes: 3 additions & 3 deletions src/test/java/org/cactoos/func/FuncWithFallbackTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public void usesMainFunc() {
final String expected = "It's success";
new Assertion<>(
"Can't use the main function if no exception",
new FuncWithFallback<Integer, String>(
new FuncWithFallback<>(
input -> expected,
new Fallback.From<>(
Exception.class,
Expand All @@ -63,7 +63,7 @@ public void usesFallback() {
final String expected = "Never mind";
new Assertion<>(
"Can't use the callback in case of exception",
new FuncWithFallback<Integer, String>(
new FuncWithFallback<>(
input -> {
throw new IOException("Failure");
},
Expand All @@ -78,7 +78,7 @@ public void usesFallbackOfInterruptedException() {
final String expected = "Fallback from InterruptedException";
new Assertion<>(
"Can't use a fallback from Interrupted in case of exception",
new FuncWithFallback<Integer, String>(
new FuncWithFallback<>(
input -> {
throw new InterruptedException(
"Failure with InterruptedException"
Expand Down
3 changes: 1 addition & 2 deletions src/test/java/org/cactoos/io/LoggingInputTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
import java.io.InputStream;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.cactoos.Text;
import org.cactoos.scalar.LengthOf;
import org.cactoos.text.TextOf;
import org.hamcrest.core.AllOf;
Expand Down Expand Up @@ -116,7 +115,7 @@ void logReadFromLargeTextFile() throws Exception {
"Must log 74536 bytes read from text file",
new TextOf(logger.toString()),
new AllOf<>(
new IsNot<Text>(
new IsNot<>(
new HasString("Read 16384 byte(s) from text file")
),
new HasString("Read 74536 byte(s) from text file in"),
Expand Down
2 changes: 1 addition & 1 deletion src/test/java/org/cactoos/io/LoggingOutputTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ public void logWriteToLargeTextFile() throws Exception {
"Must log write and close operations to text file",
logger.toString(),
new AllOf<>(
new IsNot<String>(
new IsNot<>(
new StringContains(
"Written 16384 byte(s) to text file"
)
Expand Down
4 changes: 2 additions & 2 deletions src/test/java/org/cactoos/io/TeeInputStreamTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,8 @@ void copiesContentByteByByte() throws Exception {
)
).asString(),
new AllOf<>(
new IsEqual<String>(content),
new IsEqual<String>(
new IsEqual<>(content),
new IsEqual<>(
new String(baos.toByteArray(), StandardCharsets.UTF_8)
)
)
Expand Down
6 changes: 3 additions & 3 deletions src/test/java/org/cactoos/io/TeeOutputStreamTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,11 @@ void copiesContentByteByByte() throws Exception {
)
).asString(),
new AllOf<>(
new IsEqual<String>(content),
new IsEqual<String>(
new IsEqual<>(content),
new IsEqual<>(
new String(baos.toByteArray(), StandardCharsets.UTF_8)
),
new IsEqual<String>(
new IsEqual<>(
new String(copy.toByteArray(), StandardCharsets.UTF_8)
)
)
Expand Down
4 changes: 2 additions & 2 deletions src/test/java/org/cactoos/iterable/FilteredTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -156,9 +156,9 @@ void filtersWithFuncToScalar() {
void filterWithNumberFilter() {
new Assertion<>(
"Must be filtered with super type filter",
new Filtered<Double>(
new Filtered<>(
(Number d) -> d.doubleValue() > 0,
new IterableOf<Double>(1d, -2d, 3d)
new IterableOf<>(1d, -2d, 3d)
),
new HasValues<>(1d, 3d)
).affirm();
Expand Down
Loading

0 comments on commit 1a3841b

Please sign in to comment.