This is an automated email from the ASF dual-hosted git repository.
fanningpj pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/pekko.git
The following commit(s) were added to refs/heads/main by this push:
new 18d7ff815a bind instanceof to named variables (#2088)
18d7ff815a is described below
commit 18d7ff815a72cd0d714fd7fefa93e4617ebb31fc
Author: PJ Fanning <[email protected]>
AuthorDate: Mon Aug 25 13:44:38 2025 +0100
bind instanceof to named variables (#2088)
* bind instanceof to named variables
* Update StashJavaAPITestActors.java
* Update InteractionPatternsTest.java
---
.../typed/javadsl/AsyncTestingExampleTest.java | 7 +++---
.../apache/pekko/actor/StashJavaAPITestActors.java | 9 ++++----
.../org/apache/pekko/typed/AggregatorTest.java | 6 ++---
.../pekko/typed/InteractionPatternsTest.java | 26 +++++++++-------------
.../org/apache/pekko/typed/StashDocSample.java | 4 ++--
.../pekko/actor/typed/javadsl/ActorCompile.java | 7 +++---
.../pekko/actor/typed/javadsl/AdapterTest.java | 8 +++----
.../pekko/japi/pf/FSMStateFunctionBuilder.java | 9 +++-----
.../fsm/japi/pf/FSMStateFunctionBuilder.java | 9 +++-----
.../persistence/fsm/AbstractPersistentFSMTest.java | 4 ++--
.../remote/artery/compress/CountMinSketch.java | 22 +++++++++---------
.../pekko/stream/typed/ActorSourceExample.java | 6 ++---
.../typed/ActorSourceWithBackpressureExample.java | 2 +-
.../typed/javadsl/ActorSourceSinkCompileTest.java | 2 +-
14 files changed, 54 insertions(+), 67 deletions(-)
diff --git
a/actor-testkit-typed/src/test/java/jdocs/org/apache/pekko/actor/testkit/typed/javadsl/AsyncTestingExampleTest.java
b/actor-testkit-typed/src/test/java/jdocs/org/apache/pekko/actor/testkit/typed/javadsl/AsyncTestingExampleTest.java
index 252439d85a..71258bdb9f 100644
---
a/actor-testkit-typed/src/test/java/jdocs/org/apache/pekko/actor/testkit/typed/javadsl/AsyncTestingExampleTest.java
+++
b/actor-testkit-typed/src/test/java/jdocs/org/apache/pekko/actor/testkit/typed/javadsl/AsyncTestingExampleTest.java
@@ -66,9 +66,10 @@ public class AsyncTestingExampleTest
@Override
public boolean equals(Object o) {
if (this == o) return true;
- if (!(o instanceof Pong)) return false;
- Pong pong = (Pong) o;
- return message.equals(pong.message);
+ if (o instanceof Pong pong) {
+ return message.equals(pong.message);
+ }
+ return false;
}
@Override
diff --git
a/actor-tests/src/test/java/org/apache/pekko/actor/StashJavaAPITestActors.java
b/actor-tests/src/test/java/org/apache/pekko/actor/StashJavaAPITestActors.java
index 7468cb2ab0..a7e804ea66 100644
---
a/actor-tests/src/test/java/org/apache/pekko/actor/StashJavaAPITestActors.java
+++
b/actor-tests/src/test/java/org/apache/pekko/actor/StashJavaAPITestActors.java
@@ -23,9 +23,9 @@ public class StashJavaAPITestActors {
*/
private static int testReceive(
Object msg, int count, ActorRef sender, ActorRef self, UnrestrictedStash
stash) {
- if (msg instanceof String) {
+ if (msg instanceof String s) {
if (count < 0) {
- sender.tell(((String) msg).length(), self);
+ sender.tell(s.length(), self);
} else if (count == 2) {
stash.unstashAll();
return -1;
@@ -33,9 +33,8 @@ public class StashJavaAPITestActors {
stash.stash();
return count + 1;
}
- } else if (msg instanceof Integer) {
- int value = (Integer) msg;
- assertEquals(5, value);
+ } else if (msg instanceof Integer value) {
+ assertEquals(5, value.intValue());
}
return count;
}
diff --git
a/actor-typed-tests/src/test/java/jdocs/org/apache/pekko/typed/AggregatorTest.java
b/actor-typed-tests/src/test/java/jdocs/org/apache/pekko/typed/AggregatorTest.java
index d977c0cb71..979f4603b2 100644
---
a/actor-typed-tests/src/test/java/jdocs/org/apache/pekko/typed/AggregatorTest.java
+++
b/actor-typed-tests/src/test/java/jdocs/org/apache/pekko/typed/AggregatorTest.java
@@ -191,11 +191,9 @@ public class AggregatorTest extends JUnitSuite {
r -> {
// The hotels have different protocols with different
replies,
// convert them to `HotelCustomer.Quote` that this actor
understands.
- if (r instanceof Hotel1.Quote) {
- Hotel1.Quote q = (Hotel1.Quote) r;
+ if (r instanceof Hotel1.Quote q) {
return new Quote(q.hotel, q.price);
- } else if (r instanceof Hotel2.Price) {
- Hotel2.Price p = (Hotel2.Price) r;
+ } else if (r instanceof Hotel2.Price p) {
return new Quote(p.hotel, p.price);
} else {
throw new IllegalArgumentException("Unknown reply " +
r);
diff --git
a/actor-typed-tests/src/test/java/jdocs/org/apache/pekko/typed/InteractionPatternsTest.java
b/actor-typed-tests/src/test/java/jdocs/org/apache/pekko/typed/InteractionPatternsTest.java
index b05bdfa77a..eb4c63d80a 100644
---
a/actor-typed-tests/src/test/java/jdocs/org/apache/pekko/typed/InteractionPatternsTest.java
+++
b/actor-typed-tests/src/test/java/jdocs/org/apache/pekko/typed/InteractionPatternsTest.java
@@ -220,14 +220,11 @@ public class InteractionPatternsTest extends JUnitSuite {
private Behavior<Command>
onWrappedBackendResponse(WrappedBackendResponse wrapped) {
Backend.Response response = wrapped.response;
- if (response instanceof Backend.JobStarted) {
- Backend.JobStarted rsp = (Backend.JobStarted) response;
+ if (response instanceof Backend.JobStarted rsp) {
getContext().getLog().info("Started {}", rsp.taskId);
- } else if (response instanceof Backend.JobProgress) {
- Backend.JobProgress rsp = (Backend.JobProgress) response;
+ } else if (response instanceof Backend.JobProgress rsp) {
getContext().getLog().info("Progress {}", rsp.taskId);
- } else if (response instanceof Backend.JobCompleted) {
- Backend.JobCompleted rsp = (Backend.JobCompleted) response;
+ } else if (response instanceof Backend.JobCompleted rsp) {
getContext().getLog().info("Completed {}", rsp.taskId);
inProgress.get(rsp.taskId).tell(rsp.result);
inProgress.remove(rsp.taskId);
@@ -712,11 +709,10 @@ public class InteractionPatternsTest extends JUnitSuite {
result.whenComplete(
(reply, failure) -> {
- if (reply instanceof CookieFabric.Cookies)
- System.out.println("Yay, " + ((CookieFabric.Cookies)
reply).count + " cookies!");
- else if (reply instanceof CookieFabric.InvalidRequest)
- System.out.println(
- "No cookies for me. " + ((CookieFabric.InvalidRequest)
reply).reason);
+ if (reply instanceof CookieFabric.Cookies cookiesReply)
+ System.out.println("Yay, " + cookiesReply.count + " cookies!");
+ else if (reply instanceof CookieFabric.InvalidRequest
invalidRequest)
+ System.out.println("No cookies for me. " +
invalidRequest.reason);
else System.out.println("Boo! didn't get cookies in time. " +
failure);
});
}
@@ -736,12 +732,12 @@ public class InteractionPatternsTest extends JUnitSuite {
CompletionStage<CookieFabric.Cookies> cookies =
result.thenCompose(
(CookieFabric.Reply reply) -> {
- if (reply instanceof CookieFabric.Cookies) {
- return
CompletableFuture.completedFuture((CookieFabric.Cookies) reply);
- } else if (reply instanceof CookieFabric.InvalidRequest) {
+ if (reply instanceof CookieFabric.Cookies cookiesReply) {
+ return CompletableFuture.completedFuture(cookiesReply);
+ } else if (reply instanceof CookieFabric.InvalidRequest
invalidRequest) {
CompletableFuture<CookieFabric.Cookies> failed = new
CompletableFuture<>();
failed.completeExceptionally(
- new
IllegalArgumentException(((CookieFabric.InvalidRequest) reply).reason));
+ new IllegalArgumentException(invalidRequest.reason));
return failed;
} else {
throw new IllegalStateException("Unexpected reply: " +
reply.getClass());
diff --git
a/actor-typed-tests/src/test/java/jdocs/org/apache/pekko/typed/StashDocSample.java
b/actor-typed-tests/src/test/java/jdocs/org/apache/pekko/typed/StashDocSample.java
index e8b763e221..79b67431dd 100644
---
a/actor-typed-tests/src/test/java/jdocs/org/apache/pekko/typed/StashDocSample.java
+++
b/actor-typed-tests/src/test/java/jdocs/org/apache/pekko/typed/StashDocSample.java
@@ -169,8 +169,8 @@ interface StashDocSample {
private static RuntimeException asRuntimeException(Throwable t) {
// can't throw Throwable in lambdas
- if (t instanceof RuntimeException) {
- return (RuntimeException) t;
+ if (t instanceof RuntimeException runtimeException) {
+ return runtimeException;
} else {
return new RuntimeException(t);
}
diff --git
a/actor-typed-tests/src/test/java/org/apache/pekko/actor/typed/javadsl/ActorCompile.java
b/actor-typed-tests/src/test/java/org/apache/pekko/actor/typed/javadsl/ActorCompile.java
index 24d3de15f6..940ddeff87 100644
---
a/actor-typed-tests/src/test/java/org/apache/pekko/actor/typed/javadsl/ActorCompile.java
+++
b/actor-typed-tests/src/test/java/org/apache/pekko/actor/typed/javadsl/ActorCompile.java
@@ -117,12 +117,11 @@ public class ActorCompile {
{
Behaviors.<MyMsg>receive(
(context, message) -> {
- if (message instanceof MyMsgA) {
+ if (message instanceof MyMsgA msgA) {
return Behaviors.receive(
(ctx2, msg2) -> {
- if (msg2 instanceof MyMsgB) {
- ((MyMsgA) message).replyTo.tell(((MyMsgB) msg2).greeting);
-
+ if (msg2 instanceof MyMsgB msgB) {
+ msgA.replyTo.tell(msgB.greeting);
ActorRef<String> adapter =
ctx2.messageAdapter(String.class, s -> new
MyMsgB(s.toUpperCase()));
}
diff --git
a/actor-typed-tests/src/test/java/org/apache/pekko/actor/typed/javadsl/AdapterTest.java
b/actor-typed-tests/src/test/java/org/apache/pekko/actor/typed/javadsl/AdapterTest.java
index 177df721ce..cbba452362 100644
---
a/actor-typed-tests/src/test/java/org/apache/pekko/actor/typed/javadsl/AdapterTest.java
+++
b/actor-typed-tests/src/test/java/org/apache/pekko/actor/typed/javadsl/AdapterTest.java
@@ -236,14 +236,14 @@ public class AdapterTest extends JUnitSuite {
static Behavior<Typed2Msg> typed2() {
return Behaviors.receive(
(context, message) -> {
- if (message instanceof Ping) {
- ActorRef<String> replyTo = ((Ping) message).replyTo;
+ if (message instanceof Ping ping) {
+ ActorRef<String> replyTo = ping.replyTo;
replyTo.tell("pong");
return same();
} else if (message instanceof StopIt) {
return stopped();
- } else if (message instanceof ThrowIt) {
- throw (ThrowIt) message;
+ } else if (message instanceof ThrowIt throwIt) {
+ throw throwIt;
} else {
return unhandled();
}
diff --git
a/actor/src/main/java/org/apache/pekko/japi/pf/FSMStateFunctionBuilder.java
b/actor/src/main/java/org/apache/pekko/japi/pf/FSMStateFunctionBuilder.java
index c6aa1cafa2..13cfe319c7 100644
--- a/actor/src/main/java/org/apache/pekko/japi/pf/FSMStateFunctionBuilder.java
+++ b/actor/src/main/java/org/apache/pekko/japi/pf/FSMStateFunctionBuilder.java
@@ -57,16 +57,14 @@ public class FSMStateFunctionBuilder<S, D> {
public boolean test(FSM.Event e) {
boolean res = true;
if (eventOrType != null) {
- if (eventOrType instanceof Class) {
- Class eventType = (Class) eventOrType;
+ if (eventOrType instanceof Class eventType) {
res = eventType.isInstance(e.event());
} else {
res = eventOrType.equals(e.event());
}
}
if (res && dataOrType != null) {
- if (dataOrType instanceof Class) {
- Class dataType = (Class) dataOrType;
+ if (dataOrType instanceof Class dataType) {
res = dataType.isInstance(e.stateData());
} else {
res = dataOrType.equals(e.stateData());
@@ -191,8 +189,7 @@ public class FSMStateFunctionBuilder<S, D> {
boolean emMatch = false;
Object event = e.event();
for (Object em : eventMatches) {
- if (em instanceof Class) {
- Class emc = (Class) em;
+ if (em instanceof Class emc) {
emMatch = emc.isInstance(event);
} else {
emMatch = event.equals(em);
diff --git
a/persistence/src/main/java/org/apache/pekko/persistence/fsm/japi/pf/FSMStateFunctionBuilder.java
b/persistence/src/main/java/org/apache/pekko/persistence/fsm/japi/pf/FSMStateFunctionBuilder.java
index adae87ca4b..908e25f9b6 100644
---
a/persistence/src/main/java/org/apache/pekko/persistence/fsm/japi/pf/FSMStateFunctionBuilder.java
+++
b/persistence/src/main/java/org/apache/pekko/persistence/fsm/japi/pf/FSMStateFunctionBuilder.java
@@ -67,16 +67,14 @@ public class FSMStateFunctionBuilder<S, D, E> {
public boolean
test(org.apache.pekko.persistence.fsm.PersistentFSM.Event e) {
boolean res = true;
if (eventOrType != null) {
- if (eventOrType instanceof Class) {
- Class eventType = (Class) eventOrType;
+ if (eventOrType instanceof Class eventType) {
res = eventType.isInstance(e.event());
} else {
res = eventOrType.equals(e.event());
}
}
if (res && dataOrType != null) {
- if (dataOrType instanceof Class) {
- Class dataType = (Class) dataOrType;
+ if (dataOrType instanceof Class dataType) {
res = dataType.isInstance(e.stateData());
} else {
res = dataOrType.equals(e.stateData());
@@ -208,8 +206,7 @@ public class FSMStateFunctionBuilder<S, D, E> {
boolean emMatch = false;
Object event = e.event();
for (Object em : eventMatches) {
- if (em instanceof Class) {
- Class emc = (Class) em;
+ if (em instanceof Class emc) {
emMatch = emc.isInstance(event);
} else {
emMatch = event.equals(em);
diff --git
a/persistence/src/test/java/org/apache/pekko/persistence/fsm/AbstractPersistentFSMTest.java
b/persistence/src/test/java/org/apache/pekko/persistence/fsm/AbstractPersistentFSMTest.java
index b0240b3649..2eefbff95d 100644
---
a/persistence/src/test/java/org/apache/pekko/persistence/fsm/AbstractPersistentFSMTest.java
+++
b/persistence/src/test/java/org/apache/pekko/persistence/fsm/AbstractPersistentFSMTest.java
@@ -301,8 +301,8 @@ public class AbstractPersistentFSMTest {
// #customer-apply-event
@Override
public ShoppingCart applyEvent(DomainEvent event, ShoppingCart
currentData) {
- if (event instanceof ItemAdded) {
- currentData.addItem(((ItemAdded) event).getItem());
+ if (event instanceof ItemAdded itemAdded) {
+ currentData.addItem(itemAdded.getItem());
return currentData;
} else if (event instanceof OrderExecuted) {
return currentData;
diff --git
a/remote/src/main/java/org/apache/pekko/remote/artery/compress/CountMinSketch.java
b/remote/src/main/java/org/apache/pekko/remote/artery/compress/CountMinSketch.java
index 0f5b23e21b..997ab9f468 100644
---
a/remote/src/main/java/org/apache/pekko/remote/artery/compress/CountMinSketch.java
+++
b/remote/src/main/java/org/apache/pekko/remote/artery/compress/CountMinSketch.java
@@ -163,23 +163,23 @@ public class CountMinSketch {
// this is not cryptographic one, anything which is stable and random
is good enough
return o.hashCode();
}
- if (o instanceof String) {
- return hash(((String) o).getBytes());
+ if (o instanceof String s) {
+ return hash(s.getBytes());
}
- if (o instanceof Long) {
- return hashLong((Long) o, 0);
+ if (o instanceof Long l) {
+ return hashLong(l, 0);
}
- if (o instanceof Integer) {
- return hashLong((Integer) o, 0);
+ if (o instanceof Integer i) {
+ return hashLong(i, 0);
}
- if (o instanceof Double) {
- return hashLong(Double.doubleToRawLongBits((Double) o), 0);
+ if (o instanceof Double d) {
+ return hashLong(Double.doubleToRawLongBits(d), 0);
}
- if (o instanceof Float) {
+ if (o instanceof Float f) {
return hashLong(Float.floatToRawIntBits((Float) o), 0);
}
- if (o instanceof byte[]) {
- return bytesHash((byte[]) o, 0);
+ if (o instanceof byte[] array) {
+ return bytesHash(array, 0);
}
return hash(o.toString());
}
diff --git
a/stream-typed/src/test/java/docs/org/apache/pekko/stream/typed/ActorSourceExample.java
b/stream-typed/src/test/java/docs/org/apache/pekko/stream/typed/ActorSourceExample.java
index 0990f10e4f..a006560c7c 100644
---
a/stream-typed/src/test/java/docs/org/apache/pekko/stream/typed/ActorSourceExample.java
+++
b/stream-typed/src/test/java/docs/org/apache/pekko/stream/typed/ActorSourceExample.java
@@ -57,7 +57,7 @@ public class ActorSourceExample {
final Source<Protocol, ActorRef<Protocol>> source =
ActorSource.actorRef(
(m) -> m instanceof Complete,
- (m) -> (m instanceof Fail) ? Optional.of(((Fail) m).ex) :
Optional.empty(),
+ (m) -> (m instanceof Fail fail) ? Optional.of(fail.ex) :
Optional.empty(),
8,
OverflowStrategy.fail());
@@ -66,8 +66,8 @@ public class ActorSourceExample {
.collect(
new JavaPartialFunction<Protocol, String>() {
public String apply(Protocol p, boolean isCheck) {
- if (p instanceof Message) {
- return ((Message) p).msg;
+ if (p instanceof Message message) {
+ return message.msg;
} else {
throw noMatch();
}
diff --git
a/stream-typed/src/test/java/docs/org/apache/pekko/stream/typed/ActorSourceWithBackpressureExample.java
b/stream-typed/src/test/java/docs/org/apache/pekko/stream/typed/ActorSourceWithBackpressureExample.java
index 488183079e..3a2ea9cf99 100644
---
a/stream-typed/src/test/java/docs/org/apache/pekko/stream/typed/ActorSourceWithBackpressureExample.java
+++
b/stream-typed/src/test/java/docs/org/apache/pekko/stream/typed/ActorSourceWithBackpressureExample.java
@@ -89,7 +89,7 @@ class StreamFeeder extends
AbstractBehavior<StreamFeeder.Emitted> {
else return Optional.empty();
},
(msg) -> {
- if (msg instanceof FailureOccured) return
Optional.of(((FailureOccured) msg).ex);
+ if (msg instanceof FailureOccured failure) return
Optional.of(failure.ex);
else return Optional.empty();
});
diff --git
a/stream-typed/src/test/java/org/apache/pekko/stream/typed/javadsl/ActorSourceSinkCompileTest.java
b/stream-typed/src/test/java/org/apache/pekko/stream/typed/javadsl/ActorSourceSinkCompileTest.java
index 32b57f6e8e..1bf5a06305 100644
---
a/stream-typed/src/test/java/org/apache/pekko/stream/typed/javadsl/ActorSourceSinkCompileTest.java
+++
b/stream-typed/src/test/java/org/apache/pekko/stream/typed/javadsl/ActorSourceSinkCompileTest.java
@@ -69,7 +69,7 @@ public class ActorSourceSinkCompileTest {
{
ActorSource.actorRef(
(m) -> false,
- (m) -> (m instanceof Failure) ? Optional.of(((Failure) m).ex) :
Optional.empty(),
+ (m) -> (m instanceof Failure failure) ? Optional.of(failure.ex) :
Optional.empty(),
10,
OverflowStrategy.dropBuffer())
.to(Sink.seq());
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]