Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
SharplEr committed May 10, 2018
1 parent c7614db commit 1f687cf
Show file tree
Hide file tree
Showing 16 changed files with 119 additions and 218 deletions.
21 changes: 7 additions & 14 deletions src/main/java/org/cactoos/func/AsyncFunc.java
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,9 @@ public final class AsyncFunc<X, Y> implements Func<X, Future<Y>>, Proc<X> {
/**
* Ctor.
* @param proc The proc
* @param result Result to return
* @since 0.32
*/
public AsyncFunc(final Proc<X> proc, final Y result) {
this(new FuncOf<>(proc, result));
public AsyncFunc(final Proc<X> proc) {
this(new FuncOf<>(proc));
}

/**
Expand All @@ -79,13 +77,10 @@ public AsyncFunc(final Func<X, Y> fnc) {
/**
* Ctor.
* @param proc The proc
* @param result Result to return
* @param fct Factory
* @since 0.32
*/
public AsyncFunc(final Proc<X> proc, final Y result,
final ThreadFactory fct) {
this(new FuncOf<>(proc, result), fct);
public AsyncFunc(final Proc<X> proc, final ThreadFactory fct) {
this(new FuncOf<>(proc), fct);
}

/**
Expand All @@ -101,12 +96,10 @@ public AsyncFunc(final Func<X, Y> fnc, final ThreadFactory fct) {
* Ctor.
* @param proc The proc
* @param exec Executor Service
* @param result Result to return
* @since 0.32
* @since 0.17
*/
public AsyncFunc(final Proc<X> proc, final Y result,
final ExecutorService exec) {
this(new FuncOf<>(proc, result), exec);
public AsyncFunc(final Proc<X> proc, final ExecutorService exec) {
this(new FuncOf<>(proc), exec);
}

/**
Expand Down
16 changes: 14 additions & 2 deletions src/main/java/org/cactoos/func/FuncOf.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@
* @param <X> Type of input
* @param <Y> Type of output
* @since 0.12
* @todo #551:30min Remove ctor FuncOf(Proc) by replacing with FuncOf(Proc, Y)
* FuncOf(Proc) force using of null value which is against design principles.
* It's affect a lot of classes other classes.
* Please take a look on #551 and #843 for more details.
*/
public final class FuncOf<X, Y> implements Func<X, Y> {

Expand All @@ -48,15 +52,15 @@ public final class FuncOf<X, Y> implements Func<X, Y> {
* @param result The result
*/
public FuncOf(final Y result) {
this(input -> result);
this((Func<X, Y>) input -> result);
}

/**
* Ctor.
* @param callable The callable
*/
public FuncOf(final Callable<Y> callable) {
this(input -> callable.call());
this((Func<X, Y>) input -> callable.call());
}

/**
Expand All @@ -69,6 +73,14 @@ public FuncOf(final Runnable runnable, final Y result) {
this(input -> runnable.run(), result);
}

/**
* Ctor.
* @param proc The proc
*/
public FuncOf(final Proc<X> proc) {
this(proc, null);
}

/**
* Ctor.
* @param proc The proc
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/cactoos/func/IoCheckedProc.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public IoCheckedProc(final Proc<X> prc) {

@Override
public void exec(final X input) throws IOException {
new IoCheckedFunc<>(new FuncOf<>(this.proc, null)).apply(input);
new IoCheckedFunc<>(new FuncOf<>(this.proc)).apply(input);
}

}
7 changes: 3 additions & 4 deletions src/main/java/org/cactoos/func/RepeatedFunc.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,11 @@ public final class RepeatedFunc<X, Y> implements Func<X, Y> {
/**
* Ctor.
* @param proc Proc
* @param result Result to return
* @param max How many times
* @since 0.32
* @since 0.12
*/
public RepeatedFunc(final Proc<X> proc, final Y result, final int max) {
this(new FuncOf<>(proc, result), max);
public RepeatedFunc(final Proc<X> proc, final int max) {
this(new FuncOf<>(proc), max);
}

/**
Expand Down
22 changes: 9 additions & 13 deletions src/main/java/org/cactoos/func/RetryFunc.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,34 +50,30 @@ public final class RetryFunc<X, Y> implements Func<X, Y> {
/**
* Ctor.
* @param proc Func original
* @param result Result to return
* @since 0.32
* @since 0.12
*/
public RetryFunc(final Proc<X> proc, final Y result) {
this(new FuncOf<>(proc, result));
public RetryFunc(final Proc<X> proc) {
this(new FuncOf<>(proc));
}

/**
* Ctor.
* @param proc Func original
* @param result Result to return
* @param attempts Maximum number of attempts
* @since 0.32
* @since 0.12
*/
public RetryFunc(final Proc<X> proc, final Y result, final int attempts) {
this(new FuncOf<>(proc, result), attempts);
public RetryFunc(final Proc<X> proc, final int attempts) {
this(new FuncOf<>(proc), attempts);
}

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

/**
Expand Down
21 changes: 21 additions & 0 deletions src/main/java/org/cactoos/func/SyncFunc.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

import java.util.concurrent.Callable;
import org.cactoos.Func;
import org.cactoos.Proc;

/**
* Func that is thread-safe.
Expand All @@ -47,6 +48,16 @@ public final class SyncFunc<X, Y> implements Func<X, Y> {
*/
private final Object lock;

/**
* Ctor.
* @param runnable Func original
* @param result Result to return
* @since 0.32
*/
public SyncFunc(final Runnable runnable, final Y result) {
this(new FuncOf<>(runnable, result));
}

/**
* Ctor.
* @param callable Func original
Expand All @@ -56,6 +67,16 @@ public SyncFunc(final Callable<Y> callable) {
this(new FuncOf<>(callable));
}

/**
* Ctor.
* @param proc Func original
* @param result Result to return
* @since 0.32
*/
public SyncFunc(final Proc<X> proc, final Y result) {
this(new FuncOf<>(proc, result));
}

/**
* Ctor.
* @param fnc Func original
Expand Down
73 changes: 0 additions & 73 deletions src/main/java/org/cactoos/func/SyncProc.java

This file was deleted.

7 changes: 2 additions & 5 deletions src/main/java/org/cactoos/func/TimedFunc.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,10 @@ public final class TimedFunc<X, Y> implements Func<X, Y> {
/**
* Ctor.
* @param proc Proc
* @param result Result to return
* @param milliseconds Milliseconds
* @since 0.32
*/
public TimedFunc(final Proc<X> proc, final Y result,
final long milliseconds) {
this(new FuncOf<>(proc, result), milliseconds);
public TimedFunc(final Proc<X> proc, final long milliseconds) {
this(new FuncOf<>(proc), milliseconds);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/cactoos/func/UncheckedProc.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public UncheckedProc(final Proc<X> prc) {

@Override
public void exec(final X input) {
new UncheckedFunc<>(new FuncOf<>(this.proc, null)).apply(input);
new UncheckedFunc<>(new FuncOf<>(this.proc)).apply(input);
}

}
9 changes: 2 additions & 7 deletions src/test/java/org/cactoos/func/AsyncFuncTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@
* @since 0.10
* @checkstyle JavadocMethodCheck (500 lines)
*/
@SuppressWarnings("PMD.AvoidDuplicateLiterals")
public final class AsyncFuncTest {

@Test
Expand Down Expand Up @@ -70,8 +69,7 @@ public void runsAsProcInBackground() {
input -> {
final CountDownLatch latch = new CountDownLatch(1);
new AsyncFunc<>(
(Proc<Boolean>) ipt -> latch.countDown(),
true
(Proc<Boolean>) ipt -> latch.countDown()
).exec(input);
latch.await();
return true;
Expand All @@ -90,8 +88,7 @@ public void runsInBackgroundWithoutFuture() {
new AsyncFunc<>(
input -> {
latch.countDown();
},
"done!"
}
),
new FuncApplies<>(
true,
Expand Down Expand Up @@ -120,7 +117,6 @@ public void runsInBackgroundWithThreadFactory() {
}
latch.countDown();
},
null,
factory
),
new FuncApplies<>(
Expand Down Expand Up @@ -151,7 +147,6 @@ public void runsInBackgroundWithExecutorService() {
}
latch.countDown();
},
null,
Executors.newSingleThreadExecutor(factory)
),
new FuncApplies<>(
Expand Down
13 changes: 13 additions & 0 deletions src/test/java/org/cactoos/func/FuncOfTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,19 @@ public void convertsProcIntoFunc() throws Exception {
);
}

@Test
public void convertsProcWithNoResultIntoFunc() throws Exception {
final AtomicBoolean done = new AtomicBoolean(false);
MatcherAssert.assertThat(
new FuncOf<String, Boolean>(
input -> {
done.set(true);
}
).apply("hello you"),
Matchers.nullValue()
);
}

@Test
public void convertsValueIntoFunc() throws Exception {
MatcherAssert.assertThat(
Expand Down
Loading

0 comments on commit 1f687cf

Please sign in to comment.