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

Removed nulls from Retry (#886) #1010

Merged
merged 3 commits into from
Feb 2, 2019
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
5 changes: 5 additions & 0 deletions src/main/java/org/cactoos/func/IoCheckedBiProc.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@
* @param <X> Type of input
* @param <Y> Type of input
* @since 0.22
* @todo #886:30min Avoid usage of null value in exec(first, second),
* which is against our design principles.
* This look like a duplication of functionality from IoCheckedBiFunc.
* Perhaphs, we do not need this class?
* Please take a look on #918 for more details.
*/
public final class IoCheckedBiProc<X, Y> implements BiProc<X, Y> {

Expand Down
35 changes: 0 additions & 35 deletions src/main/java/org/cactoos/func/Retry.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
package org.cactoos.func;

import org.cactoos.Func;
import org.cactoos.Proc;

/**
* Func that will try a few times before throwing an exception.
Expand All @@ -34,11 +33,6 @@
* @param <X> Type of input
* @param <Y> Type of output
* @since 0.8
* @todo #861:30min Avoid usage of null value in ctor(Proc),
* ctor(Proc, int), ctor(Proc, Func(Integer, Boolean)) which is against
* design principles.
* Perhaps in creating RetryProc?
* Please take a look on #551 and #843 for more details.
*/
public final class Retry<X, Y> implements Func<X, Y> {

Expand All @@ -52,35 +46,6 @@ public final class Retry<X, Y> implements Func<X, Y> {
*/
private final Func<Integer, Boolean> exit;

/**
* Ctor.
* @param proc Func original
* @since 0.12
*/
public Retry(final Proc<X> proc) {
this(new FuncOf<>(proc, null));
}

/**
* Ctor.
* @param proc Func original
* @param attempts Maximum number of attempts
* @since 0.12
*/
public Retry(final Proc<X> proc, final int attempts) {
this(new FuncOf<>(proc, null), attempts);
}

/**
* Ctor.
* @param proc Func original
* @param ext Exit condition, returns TRUE if there is no more reason to try
* @since 0.12
*/
public Retry(final Proc<X> proc, final Func<Integer, Boolean> ext) {
this(new FuncOf<>(proc, null), ext);
}

/**
* Ctor.
* @param fnc Func original
Expand Down
62 changes: 17 additions & 45 deletions src/test/java/org/cactoos/func/RetryTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,9 @@
package org.cactoos.func;

import java.security.SecureRandom;
import java.util.concurrent.atomic.AtomicBoolean;
import org.hamcrest.MatcherAssert;
import org.hamcrest.Matchers;
import org.junit.Test;
import org.llorllale.cactoos.matchers.Assertion;
import org.llorllale.cactoos.matchers.FuncApplies;

/**
* Test case for {@link Retry}.
Expand All @@ -40,63 +39,36 @@
public final class RetryTest {

@Test
public void runsFuncMultipleTimes() throws Exception {
MatcherAssert.assertThat(
new Retry<>(
public void runsFuncMultipleTimes() {
new Assertion<>(
"Didn't run multiple times",
() -> new Retry<>(
input -> {
if (new SecureRandom().nextDouble() > 0.3d) {
throw new IllegalArgumentException("May happen");
}
return 0;
},
Integer.MAX_VALUE
).apply(true),
Matchers.equalTo(0)
);
),
new FuncApplies<>(true, 0)
).affirm();
}

@Test
public void runsProcMultipleTimes() throws Exception {
MatcherAssert.assertThat(
new Retry<>(
input -> {
if (new SecureRandom().nextDouble() > 0.3d) {
throw new IllegalArgumentException("May happen");
}
},
Integer.MAX_VALUE
).apply(true),
Matchers.nullValue()
);
}

@Test
public void runsProcDefaultMultipleTimes() throws Exception {
final AtomicBoolean fail = new AtomicBoolean(true);
MatcherAssert.assertThat(
new Retry<>(
input -> {
if (fail.getAndSet(false)) {
throw new IllegalArgumentException("May happen");
}
}
).apply(true),
Matchers.nullValue()
);
}

@Test
public void runsProcConditionMultipleTimes() throws Exception {
MatcherAssert.assertThat(
new Retry<>(
public void runsFuncConditionMultipleTimes() {
new Assertion<>(
"Didn't check condition multiple times",
() -> new Retry<>(
input -> {
if (new SecureRandom().nextDouble() > 0.3d) {
throw new IllegalArgumentException("May happen");
}
return true;
},
count -> count == Integer.MAX_VALUE
).apply(true),
Matchers.nullValue()
);
),
new FuncApplies<>(true, true)
).affirm();
}
}