Add static import convenience methods to Done and NotUsed (#25174)

* Add Done.done() convenience for static import
* Add NotUsed.notUsed() convenience for static import
This commit is contained in:
Tim Moore 2018-05-31 21:42:20 +09:30 committed by Christopher Batey
parent c8f4a17025
commit 5c401584c3
7 changed files with 25 additions and 6 deletions

View file

@ -13,6 +13,7 @@ import org.scalatest.junit.JUnitSuite;
import java.util.concurrent.CompletableFuture; import java.util.concurrent.CompletableFuture;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import static akka.Done.done;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
/** /**
@ -32,7 +33,7 @@ public class ActorTestKitTest extends JUnitSuite {
public void testKitShouldSpawnActor() throws Exception { public void testKitShouldSpawnActor() throws Exception {
final CompletableFuture<Done> started = new CompletableFuture<>(); final CompletableFuture<Done> started = new CompletableFuture<>();
testKit.spawn(Behaviors.setup((ctx) -> { testKit.spawn(Behaviors.setup((ctx) -> {
started.complete(Done.getInstance()); started.complete(done());
return Behaviors.same(); return Behaviors.same();
})); }));
started.get(3, TimeUnit.SECONDS); started.get(3, TimeUnit.SECONDS);

View file

@ -17,6 +17,7 @@ import org.junit.Test;
import akka.actor.typed.*; import akka.actor.typed.*;
import static akka.Done.done;
import static akka.actor.typed.javadsl.Behaviors.*; import static akka.actor.typed.javadsl.Behaviors.*;
public class WatchTest extends JUnitSuite { public class WatchTest extends JUnitSuite {
@ -53,7 +54,7 @@ public class WatchTest extends JUnitSuite {
(ctx, msg) -> unhandled(), (ctx, msg) -> unhandled(),
(ctx, sig) -> { (ctx, sig) -> {
if (sig instanceof Terminated) { if (sig instanceof Terminated) {
replyWhenTerminated.tell(Done.getInstance()); replyWhenTerminated.tell(done());
} }
return same(); return same();
} }
@ -64,7 +65,7 @@ public class WatchTest extends JUnitSuite {
return receive( return receive(
(ctx, msg) -> { (ctx, msg) -> {
if (msg instanceof CustomTerminationMessage) { if (msg instanceof CustomTerminationMessage) {
replyWhenReceived.tell(Done.getInstance()); replyWhenReceived.tell(done());
return same(); return same();
} else { } else {
return unhandled(); return unhandled();

View file

@ -19,4 +19,11 @@ case object Done extends Done {
* Java API: the singleton instance * Java API: the singleton instance
*/ */
def getInstance(): Done = this def getInstance(): Done = this
/**
* Java API: the singleton instance
*
* This is equivalent to [[Done#getInstance()]], but can be used with static import.
*/
def done(): Done = this
} }

View file

@ -17,4 +17,11 @@ case object NotUsed extends NotUsed {
* Java API: the singleton instance * Java API: the singleton instance
*/ */
def getInstance(): NotUsed = this def getInstance(): NotUsed = this
/**
* Java API: the singleton instance
*
* This is equivalent to [[NotUsed#getInstance()]], but can be used with static import.
*/
def notUsed(): NotUsed = this
} }

View file

@ -34,6 +34,7 @@ import java.util.concurrent.TimeoutException;
import java.util.stream.Stream; import java.util.stream.Stream;
import java.time.Duration; import java.time.Duration;
import static akka.Done.done;
import static akka.stream.testkit.StreamTestKit.PublisherProbeSubscription; import static akka.stream.testkit.StreamTestKit.PublisherProbeSubscription;
import static org.junit.Assert.*; import static org.junit.Assert.*;
@ -595,7 +596,7 @@ public class FlowTest extends StreamTest {
.watchTermination(Keep.right()) .watchTermination(Keep.right())
.to(Sink.ignore()).run(materializer); .to(Sink.ignore()).run(materializer);
assertEquals(Done.getInstance(), future.toCompletableFuture().get(3, TimeUnit.SECONDS)); assertEquals(done(), future.toCompletableFuture().get(3, TimeUnit.SECONDS));
} }
@Test @Test

View file

@ -17,6 +17,7 @@ import java.util.concurrent.CompletionStage;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import akka.testkit.AkkaJUnitActorSystemResource; import akka.testkit.AkkaJUnitActorSystemResource;
import static akka.Done.done;
import static org.junit.Assert.*; import static org.junit.Assert.*;
public class KillSwitchTest extends StreamTest { public class KillSwitchTest extends StreamTest {
@ -55,7 +56,7 @@ public class KillSwitchTest extends StreamTest {
upstream.expectCancellation(); upstream.expectCancellation();
downstream.expectComplete(); downstream.expectComplete();
assertEquals(completionStage.toCompletableFuture().get(3, TimeUnit.SECONDS), Done.getInstance()); assertEquals(completionStage.toCompletableFuture().get(3, TimeUnit.SECONDS), done());
} }
@Test @Test

View file

@ -33,6 +33,7 @@ import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException; import java.util.concurrent.TimeoutException;
import java.util.stream.Stream; import java.util.stream.Stream;
import static akka.NotUsed.notUsed;
import static akka.stream.testkit.StreamTestKit.PublisherProbeSubscription; import static akka.stream.testkit.StreamTestKit.PublisherProbeSubscription;
import static akka.stream.testkit.TestPublisher.ManualProbe; import static akka.stream.testkit.TestPublisher.ManualProbe;
import static org.junit.Assert.*; import static org.junit.Assert.*;
@ -451,7 +452,7 @@ public class SourceTest extends StreamTest {
@SuppressWarnings("unused") @SuppressWarnings("unused")
public void mustCompileMethodsWithJavaDuration() { public void mustCompileMethodsWithJavaDuration() {
Source<NotUsed, Cancellable> tickSource = Source.tick(Duration.ofSeconds(1), Source<NotUsed, Cancellable> tickSource = Source.tick(Duration.ofSeconds(1),
Duration.ofMillis(500), NotUsed.getInstance()); Duration.ofMillis(500), notUsed());
} }
@Test @Test