Skip to content

Commit

Permalink
#1650 RunnableOf with Callable
Browse files Browse the repository at this point in the history
  • Loading branch information
yegor256 committed Aug 21, 2022
1 parent 93b2301 commit 8c53edb
Show file tree
Hide file tree
Showing 4 changed files with 142 additions and 21 deletions.
17 changes: 0 additions & 17 deletions 2019-12-05.md

This file was deleted.

99 changes: 99 additions & 0 deletions src/main/java/org/cactoos/proc/CallableOf.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
/*
* 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.proc;

import java.util.concurrent.Callable;
import org.cactoos.Proc;
import org.cactoos.Scalar;
import org.cactoos.scalar.Unchecked;

/**
* Func as {@link Callable}.
*
* <p>There is no thread-safety guarantee.
*
* @param <T> Type of return
* @since 0.53
*/
public final class CallableOf<T> implements Callable<T> {

/**
* The callable.
*/
private final Callable<T> callable;

/**
* Ctor.
* @param proc Encapsulated proc
* @param ipt Input
* @param <X> Type of input
* @since 0.32
*/
public <X> CallableOf(final Proc<? super X> proc, final X ipt) {
this(
() -> new UncheckedProc<>(proc).exec(ipt)
);
}

/**
* Ctor.
* @param scalar Encapsulated scalar
* @since 0.11
*/
public CallableOf(final Scalar<?> scalar) {
this(
() -> {
new Unchecked<>(scalar).value();
}
);
}

/**
* Ctor.
* @param runnable The callable
* @since 0.53
*/
public CallableOf(final Runnable runnable) {
this(
() -> {
runnable.run();
return null;
}
);
}

/**
* Ctor.
* @param clbl The callable original
* @since 0.53
*/
public CallableOf(final Callable<T> clbl) {
this.callable = clbl;
}

@Override
public T call() throws Exception {
return this.callable.call();
}
}
25 changes: 21 additions & 4 deletions src/main/java/org/cactoos/proc/RunnableOf.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,13 @@
*/
package org.cactoos.proc;

import java.util.concurrent.Callable;
import org.cactoos.Proc;
import org.cactoos.Scalar;
import org.cactoos.scalar.Unchecked;

/**
* Func as Runnable.
* Func as {@link Runnable}.
*
* <p>There is no thread-safety guarantee.
*
Expand All @@ -45,9 +46,7 @@ public final class RunnableOf extends RunnableEnvelope {
*/
public <X> RunnableOf(final Proc<? super X> proc, final X ipt) {
this(
() -> {
new UncheckedProc<>(proc).exec(ipt);
}
() -> new UncheckedProc<>(proc).exec(ipt)
);
}

Expand All @@ -64,6 +63,24 @@ public RunnableOf(final Scalar<?> scalar) {
);
}

/**
* Ctor.
* @param callable The callable
* @since 0.53
*/
public RunnableOf(final Callable<?> callable) {
this(
() -> {
try {
callable.call();
// @checkstyle IllegalCatchCheck (1 line)
} catch (final Exception ex) {
throw new IllegalStateException(ex);
}
}
);
}

/**
* Ctor.
* @param runnable Encapsulated runnable
Expand Down
22 changes: 22 additions & 0 deletions src/test/java/org/cactoos/proc/RunnableOfTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
*/
package org.cactoos.proc;

import java.util.concurrent.Callable;
import java.util.concurrent.atomic.AtomicReference;
import org.cactoos.scalar.ScalarOf;
import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -100,4 +101,25 @@ void convertsLambdaIntoRunnable() {
).affirm();
}

@Test
void convertsCallableIntoRunnable() {
final AtomicReference<Object> done = new AtomicReference<>();
final Object obj = new Object();
new Assertion<>(
"Must execute Runnable with Callable",
new RunnableOf(
(Callable<Void>) () -> {
done.set(obj);
return null;
}
),
new Satisfies<>(
runnable -> {
runnable.run();
return done.get().equals(obj);
}
)
).affirm();
}

}

0 comments on commit 8c53edb

Please sign in to comment.