Skip to content

Commit

Permalink
Add MoreFutures.unmodifiableFuture
Browse files Browse the repository at this point in the history
  • Loading branch information
dain authored and johngmyers committed May 23, 2015
1 parent 2a362f6 commit 0b0c6ac
Show file tree
Hide file tree
Showing 2 changed files with 160 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,22 @@ public final class MoreFutures
{
private MoreFutures() { }

public static <V> CompletableFuture<V> unmodifiableFuture(CompletableFuture<V> future)
{
requireNonNull(future, "future is null");

UnmodifiableCompletableFuture<V> unmodifiableFuture = new UnmodifiableCompletableFuture<>();
future.whenComplete((value, exception) -> {
if (exception != null) {
unmodifiableFuture.internalCompleteExceptionally(exception);
}
else {
unmodifiableFuture.internalComplete(value);
}
});
return unmodifiableFuture;
}

public static <V> CompletableFuture<V> failedFuture(Throwable throwable)
{
requireNonNull(throwable, "throwable is null");
Expand Down Expand Up @@ -163,4 +179,49 @@ public void onFailure(Throwable t)
});
return future;
}

private static class UnmodifiableCompletableFuture<V>
extends CompletableFuture<V>
{
void internalComplete(V value)
{
super.complete(value);
}

void internalCompleteExceptionally(Throwable ex)
{
super.completeExceptionally(ex);
}

@Override
public boolean cancel(boolean mayInterruptIfRunning)
{
// ignore cancellation
return false;
}

@Override
public boolean complete(V value)
{
throw new UnsupportedOperationException();
}

@Override
public boolean completeExceptionally(Throwable ex)
{
throw new UnsupportedOperationException();
}

@Override
public void obtrudeValue(V value)
{
throw new UnsupportedOperationException();
}

@Override
public void obtrudeException(Throwable ex)
{
throw new UnsupportedOperationException();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import static com.proofpoint.concurrent.MoreFutures.toCompletableFuture;
import static com.proofpoint.concurrent.MoreFutures.toListenableFuture;
import static com.proofpoint.concurrent.MoreFutures.tryGetFutureValue;
import static com.proofpoint.concurrent.MoreFutures.unmodifiableFuture;
import static com.proofpoint.testing.Assertions.assertInstanceOf;
import static java.util.concurrent.CompletableFuture.completedFuture;
import static java.util.concurrent.TimeUnit.MILLISECONDS;
Expand All @@ -28,6 +29,104 @@

public class TestMoreFutures
{
@Test
public void testModifyUnmodifiableFuture()
throws Exception
{
CompletableFuture<String> future = new CompletableFuture<>();
CompletableFuture<String> unmodifiableFuture = unmodifiableFuture(future);

// completion results in an UnsupportedOperationException
assertFailure(() -> unmodifiableFuture.complete("fail"), UnsupportedOperationException.class::isInstance);
assertFalse(future.isDone());
assertFalse(unmodifiableFuture.isDone());

assertFailure(() -> unmodifiableFuture.completeExceptionally(new IOException()), UnsupportedOperationException.class::isInstance);
assertFalse(future.isDone());
assertFalse(unmodifiableFuture.isDone());

assertFailure(() -> unmodifiableFuture.obtrudeValue("fail"), UnsupportedOperationException.class::isInstance);
assertFalse(future.isDone());
assertFalse(unmodifiableFuture.isDone());

assertFailure(() -> unmodifiableFuture.obtrudeException(new IOException()), UnsupportedOperationException.class::isInstance);
assertFalse(future.isDone());
assertFalse(unmodifiableFuture.isDone());

// cancel is ignored
assertFalse(unmodifiableFuture.cancel(false));
assertFalse(future.isDone());
assertFalse(unmodifiableFuture.isDone());

assertFalse(unmodifiableFuture.cancel(true));
assertFalse(future.isDone());
assertFalse(unmodifiableFuture.isDone());
}

@Test
public void testCompleteUnmodifiableFuture()
throws Exception
{
CompletableFuture<String> future = new CompletableFuture<>();
CompletableFuture<String> unmodifiableFuture = unmodifiableFuture(future);

assertTrue(future.complete("done"));
assertEquals(future.getNow(null), "done");
assertTrue(unmodifiableFuture.isDone());
assertEquals(unmodifiableFuture.getNow(null), "done");
}

@Test
public void testCompleteExceptionallyUnmodifiableFuture()
throws Exception
{
CompletableFuture<String> future = new CompletableFuture<>();
CompletableFuture<String> unmodifiableFuture = unmodifiableFuture(future);

assertTrue(future.completeExceptionally(new SQLException("foo")));
assertFailure(() -> getFutureValue(future, SQLException.class), (e) -> {
assertInstanceOf(e, SQLException.class);
assertEquals(e.getMessage(), "foo");
});

assertTrue(unmodifiableFuture.isDone());
assertFailure(() -> getFutureValue(unmodifiableFuture, SQLException.class), (e) -> {
assertInstanceOf(e, SQLException.class);
assertEquals(e.getMessage(), "foo");
});
}

@Test
public void testAlreadyCompleteUnmodifiableFuture()
throws Exception
{
CompletableFuture<String> future = completedFuture("done");
CompletableFuture<String> unmodifiableFuture = unmodifiableFuture(future);

assertEquals(future.getNow(null), "done");
assertTrue(unmodifiableFuture.isDone());
assertEquals(unmodifiableFuture.getNow(null), "done");
}

@Test
public void testAlreadyCompleteExceptionallyUnmodifiableFuture()
throws Exception
{
CompletableFuture<String> future = failedFuture(new SQLException("foo"));
CompletableFuture<String> unmodifiableFuture = unmodifiableFuture(future);

assertFailure(() -> getFutureValue(future, SQLException.class), (e) -> {
assertInstanceOf(e, SQLException.class);
assertEquals(e.getMessage(), "foo");
});

assertTrue(unmodifiableFuture.isDone());
assertFailure(() -> getFutureValue(unmodifiableFuture, SQLException.class), (e) -> {
assertInstanceOf(e, SQLException.class);
assertEquals(e.getMessage(), "foo");
});
}

@Test
public void testFailedFuture()
throws Exception
Expand Down

0 comments on commit 0b0c6ac

Please sign in to comment.