Skip to content

Commit

Permalink
Merge pull request #28 from code77se/wrap-void-method
Browse files Browse the repository at this point in the history
Wrap void method
  • Loading branch information
code77se authored Dec 12, 2016
2 parents 8442ad9 + 73e66ec commit a88d105
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 2 deletions.
4 changes: 2 additions & 2 deletions jq/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ android {
defaultConfig {
minSdkVersion 3
targetSdkVersion 23
versionCode 15
versionName "1.1.4"
versionCode 16
versionName "1.1.5"
}
buildTypes {
}
Expand Down
31 changes: 31 additions & 0 deletions jq/src/main/java/se/code77/jq/Value.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,17 @@
* @param <V> Type of the value being wrapped
*/
public final class Value<V> implements Future<V> {
/**
* A wrapper for calling void methods and returning Value.VOID.
*/
public interface VoidCallable {
/**
* This callback should call the void method.
* @throws Exception Any exception thrown by the callback.
*/
void call() throws Exception;
}

/**
* Return value for Void callbacks
*/
Expand All @@ -36,6 +47,26 @@ public static <V> Value<V> wrap(V value) {
return new Value<V>(value);
}

/**
* Convenience for calling a void method and returning a Future&lt;Void&gt;.
* Examples:
*
* <pre>
* return Value.wrap(this::doSomething);
* return Value.wrap(() -&gt; doSomethingWithArgs(42));
* </pre>
*
* @param callable The callback making the call to a void method
* @return Value.VOID
* @throws Exception if the invoked void method threw an exception
*/
public static Value<Void> wrap(VoidCallable callable) throws Exception {
callable.call();

return Value.VOID;
}

/**
* Not applicable, since the value is already available upon construction
*
Expand Down
48 changes: 48 additions & 0 deletions jq/src/test/java/se/code77/jq/ValueTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import se.code77.jq.Promise.UnhandledRejectionException;
import se.code77.jq.util.AsyncTests;
import se.code77.jq.util.BlockingDataHolder;
import se.code77.jq.util.DataCallback;
import se.code77.jq.util.DataFulfilledCallback;
import se.code77.jq.util.DataRejectedCallback;
import se.code77.jq.util.SlowTask;
Expand Down Expand Up @@ -73,4 +74,51 @@ public void value_isDone() {
assertEquals(false, value.cancel(true));
assertEquals(true, value.isDone());
}

private <T> void setFlag(BlockingDataHolder<T> holder, T data) {
if (holder != null) {
holder.set(data);
} else {
throw new IllegalArgumentException();
}
}

@Test
public void value_wrapVoid() {
try {
final BlockingDataHolder<String> holder = new BlockingDataHolder<>();

Value<Void> v = Value.wrap(new Value.VoidCallable() {
@Override
public void call() throws Exception {
setFlag(holder, TEST_VALUE1);
}
});

assertEquals(Value.VOID, v);
assertData(holder, 100, TEST_VALUE1);
} catch (Exception e) {
assertTrue(false);
}
}

@Test
public void value_wrapVoidThrows() {
final BlockingDataHolder<Exception> holder = new BlockingDataHolder<>();

try {
Value<Void> v = Value.wrap(new Value.VoidCallable() {
@Override
public void call() throws Exception {
setFlag(null, null);
}
});

assertTrue(false);
} catch (Exception e) {
holder.set(e);
}

assertData(holder, 100);
}
}

0 comments on commit a88d105

Please sign in to comment.