Skip to content

Commit

Permalink
Using interfaces instead of heritage
Browse files Browse the repository at this point in the history
  • Loading branch information
sleipnir committed Jul 30, 2024
1 parent 63d647f commit d4b0d8f
Show file tree
Hide file tree
Showing 10 changed files with 52 additions and 77 deletions.
22 changes: 22 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
name: CI

on:
push:
branches: [main]
pull_request:
branches: [main]

jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3

- name: Set up JDK 11
uses: actions/setup-java@v3
with:
java-version: '11'
distribution: 'temurin'

- name: Run tests
run: mvn compile && mvn test
32 changes: 16 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,7 @@ import io.eigr.spawn.java.demo.domain.Actor.State;

import static io.eigr.spawn.api.actors.behaviors.ActorBehavior.*;

public final class JoeActor extends StatefulActor<State> {
public final class JoeActor implements StatefulActor<State> {

@Override
public ActorBehavior configure(BehaviorCtx context) {
Expand Down Expand Up @@ -345,19 +345,19 @@ package io.eigr.spawn.java.demo;
import io.eigr.spawn.api.actors.StatefulActor;
import io.eigr.spawn.java.demo.domain.Actor.State;

public final class JoeActor extends StatefulActor<State> {
public final class JoeActor implements StatefulActor<State> {
// ...
}
```

The `JoeActor` class extends `StatefulActor<State>`. `StatefulActor` is a generic class provided by the Spawn API,
The `JoeActor` class implements `StatefulActor<State>` interface. `StatefulActor` is a generic interface provided by the Spawn API,
which takes a type parameter for the state. In this case, the state type is `io.eigr.spawn.java.demo.domain.Actor.State`
defined in above protobuf file.

***Configure Actor Behavior***

```java
public final class JoeActor extends StatefulActor<State> {
public final class JoeActor implements StatefulActor<State> {
@Override
public ActorBehavior configure(BehaviorCtx context) {
return new NamedActorBehavior(
Expand All @@ -382,7 +382,7 @@ This `configure` method is overridden from `StatefulActor` and is used to config
***Handle request***

```java
public final class JoeActor extends StatefulActor<State> {
public final class JoeActor implements StatefulActor<State> {
//
private Value setLanguage(ActorContext<State> context, Request msg) {
if (context.getState().isPresent()) {
Expand Down Expand Up @@ -534,7 +534,7 @@ import io.eigr.spawn.java.demo.domain.Actor.State;

import static io.eigr.spawn.api.actors.behaviors.ActorBehavior.action;
import static io.eigr.spawn.api.actors.behaviors.ActorBehavior.name;
public final class JoeActor extends StatefulActor<State> {
public final class JoeActor implements StatefulActor<State> {

private String defaultMessage;

Expand Down Expand Up @@ -648,7 +648,7 @@ import io.eigr.spawn.java.demo.domain.Actor.State;

import static io.eigr.spawn.api.actors.behaviors.ActorBehavior.action;
import static io.eigr.spawn.api.actors.behaviors.ActorBehavior.name;
public final class JoeActor extends StatefulActor<State> {
public final class JoeActor implements StatefulActor<State> {

private String defaultMessage;

Expand Down Expand Up @@ -738,14 +738,14 @@ recommended for handling serverless loads.

In addition to these types, Spawn also allows the developer to choose Stateful actors, who need to maintain the state,
or Stateless, those who do not need to maintain the state.
For this the developer just needs to make extend of the correct base class. For example, I could declare a Serverless Actor using the following code:
For this the developer just needs to make extend of the correct base interface. For example, I could declare a Serverless Actor using the following code:

```java
package io.eigr.spawn.java.demo.actors;

import io.eigr.spawn.api.actors.ActorContext;
import io.eigr.spawn.api.actors.StatelessActor;
import io.eigr.spawn.api.actors.Value;F
import io.eigr.spawn.api.actors.Value;
import io.eigr.spawn.api.actors.behaviors.ActorBehavior;
import io.eigr.spawn.api.actors.behaviors.BehaviorCtx;
import io.eigr.spawn.api.actors.behaviors.NamedActorBehavior;
Expand All @@ -756,7 +756,7 @@ import io.eigr.spawn.java.demo.domain.Actor.Request;
import static io.eigr.spawn.api.actors.behaviors.ActorBehavior.action;
import static io.eigr.spawn.api.actors.behaviors.ActorBehavior.name;

public final class StatelessNamedActor extends StatelessActor {
public final class StatelessNamedActor implements StatelessActor {

@Override
public ActorBehavior configure(BehaviorCtx context) {
Expand Down Expand Up @@ -826,7 +826,7 @@ import io.eigr.spawn.java.demo.domain.Actor.State;

import static io.eigr.spawn.api.actors.behaviors.ActorBehavior.*;

public final class LoopActor extends StatefulActor<State> {
public final class LoopActor implements StatefulActor<State> {

@Override
public ActorBehavior configure(BehaviorCtx context) {
Expand Down Expand Up @@ -872,7 +872,7 @@ import io.eigr.spawn.java.demo.domain.Actor.State;

import static io.eigr.spawn.api.actors.behaviors.ActorBehavior.*;

public final class JoeActor extends StatefulActor<State> {
public final class JoeActor implements StatefulActor<State> {

@Override
public ActorBehavior configure(BehaviorCtx context) {
Expand Down Expand Up @@ -928,7 +928,7 @@ import io.eigr.spawn.java.demo.domain.Actor.State;

import static io.eigr.spawn.api.actors.behaviors.ActorBehavior.*;

public final class RoutingActor extends StatefulActor<State> {
public final class RoutingActor implements StatefulActor<State> {

@Override
public ActorBehavior configure(BehaviorCtx context) {
Expand Down Expand Up @@ -974,7 +974,7 @@ import io.eigr.spawn.java.demo.domain.Actor.State;

import static io.eigr.spawn.api.actors.behaviors.ActorBehavior.*;

public final class PipeActor extends StatefulActor<State> {
public final class PipeActor implements StatefulActor<State> {

@Override
public ActorBehavior configure(BehaviorCtx context) {
Expand Down Expand Up @@ -1037,7 +1037,7 @@ import io.eigr.spawn.java.demo.domain.Actor.State;

import static io.eigr.spawn.api.actors.behaviors.ActorBehavior.*;

public final class JoeActor extends StatefulActor<State> {
public final class JoeActor implements StatefulActor<State> {

@Override
public ActorBehavior configure(BehaviorCtx context) {
Expand Down Expand Up @@ -1170,7 +1170,7 @@ import io.eigr.spawn.java.demo.domain.Actor.State;

import static io.eigr.spawn.api.actors.behaviors.ActorBehavior.*;

public final class MikeActor extends StatefulActor<State> {
public final class MikeActor implements StatefulActor<State> {

@Override
public ActorBehavior configure(BehaviorCtx context) {
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/io/eigr/spawn/api/actors/BaseActor.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
import io.eigr.spawn.api.actors.behaviors.ActorBehavior;
import io.eigr.spawn.api.actors.behaviors.BehaviorCtx;

public abstract class BaseActor {
public interface BaseActor {

public abstract ActorBehavior configure(BehaviorCtx ctx);
ActorBehavior configure(BehaviorCtx ctx);

public abstract Boolean isStateful();
Boolean isStateful();

}
8 changes: 3 additions & 5 deletions src/main/java/io/eigr/spawn/api/actors/StatefulActor.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,14 @@

import java.lang.reflect.ParameterizedType;

public abstract class StatefulActor<S> extends BaseActor {
public Class<S> getStateType() {
public interface StatefulActor<S> extends BaseActor {
default Class<S> getStateType() {
return (Class<S>) ((ParameterizedType)
getClass().getGenericSuperclass())
.getActualTypeArguments()[0];
}

@Override
public Boolean isStateful() {
default Boolean isStateful() {
return true;
}

}
4 changes: 2 additions & 2 deletions src/main/java/io/eigr/spawn/api/actors/StatelessActor.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package io.eigr.spawn.api.actors;

public abstract class StatelessActor extends BaseActor {
public interface StatelessActor extends BaseActor {
@Override
public Boolean isStateful() {
default Boolean isStateful() {
return false;
}
}
45 changes: 0 additions & 45 deletions src/test/java/io/eigr/spawn/MyTestActorEntity.java

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

import static io.eigr.spawn.api.actors.behaviors.ActorBehavior.action;
import static io.eigr.spawn.api.actors.behaviors.ActorBehavior.name;
public final class ActorWithConstructor extends StatefulActor<State> {
public final class ActorWithConstructor implements StatefulActor<State> {

private String defaultMessage;

Expand Down
2 changes: 1 addition & 1 deletion src/test/java/io/eigr/spawn/test/actors/JoeActor.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

import static io.eigr.spawn.api.actors.behaviors.ActorBehavior.*;

public final class JoeActor extends StatefulActor<State> {
public final class JoeActor implements StatefulActor<State> {

@Override
public ActorBehavior configure(BehaviorCtx context) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
import static io.eigr.spawn.api.actors.behaviors.ActorBehavior.action;
import static io.eigr.spawn.api.actors.behaviors.ActorBehavior.name;

public final class StatelessNamedActor extends StatelessActor {
public class StatelessNamedActor implements StatelessActor {

@Override
public ActorBehavior configure(BehaviorCtx context) {
Expand Down
6 changes: 3 additions & 3 deletions src/test/java/io/eigr/spawn/test/actors/UnNamedActor.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,16 @@
import io.eigr.spawn.api.actors.Value;
import io.eigr.spawn.api.actors.behaviors.ActorBehavior;
import io.eigr.spawn.api.actors.behaviors.BehaviorCtx;
import io.eigr.spawn.api.actors.behaviors.NamedActorBehavior;
import io.eigr.spawn.api.actors.behaviors.UnNamedActorBehavior;
import io.eigr.spawn.internal.ActionBindings;
import io.eigr.spawn.java.test.domain.Actor.Reply;
import io.eigr.spawn.java.test.domain.Actor.Request;
import io.eigr.spawn.java.test.domain.Actor.State;

import static io.eigr.spawn.api.actors.behaviors.ActorBehavior.*;
import static io.eigr.spawn.api.actors.behaviors.ActorBehavior.action;
import static io.eigr.spawn.api.actors.behaviors.ActorBehavior.name;

public final class UnNamedActor extends StatefulActor<State> {
public final class UnNamedActor implements StatefulActor<State> {

@Override
public ActorBehavior configure(BehaviorCtx context) {
Expand Down

0 comments on commit d4b0d8f

Please sign in to comment.