Skip to content

Commit

Permalink
Adds AI generated java doc
Browse files Browse the repository at this point in the history
  • Loading branch information
paxel committed Dec 7, 2023
1 parent 69807ad commit f2cbbd7
Show file tree
Hide file tree
Showing 13 changed files with 481 additions and 20 deletions.
13 changes: 12 additions & 1 deletion src/main/java/paxel/lintstone/api/ErrorHandler.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,17 @@
package paxel.lintstone.api;

/**
* ErrorHandler is a functional interface for handling errors.
* The handleError method is called to handle an error.
*/
@FunctionalInterface
public interface ErrorHandler {
boolean handleError(Object a);

/**
* Handles an error.
*
* @param failedMessage The object representing the error.
* @return True if the error was handled successfully, false otherwise.
*/
boolean handleError(Object failedMessage);
}
54 changes: 54 additions & 0 deletions src/main/java/paxel/lintstone/impl/Actor.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,15 @@ class Actor {
private final AtomicLong totalReplies = new AtomicLong();
private final MessageContextFactory messageContextFactory;

/**
* Constructs a new Actor with the given parameters.
*
* @param name The name of the actor.
* @param actorInstance The instance of the LintStoneActor interface that this actor represents.
* @param sequentialProcessor The SequentialProcessor responsible for processing messages sequentially.
* @param system The ActorSystem that manages this actor.
* @param sender The SelfUpdatingActorAccessor of the sender.
*/
Actor(String name, LintStoneActor actorInstance, SequentialProcessor sequentialProcessor, ActorSystem system, SelfUpdatingActorAccessor sender) {
this.name = name;
this.actorInstance = actorInstance;
Expand All @@ -31,10 +40,24 @@ class Actor {
}


/**
* Checks if the actor is valid.
*
* @return true if the actor is valid, false otherwise.
*/
boolean isValid() {
return registered;
}

/**
* Sends a message to an actor for processing.
*
* @param message The message to be sent.
* @param sender The SelfUpdatingActorAccessor of the sender.
* @param replyHandler The handler for the reply. If null, the message is sent to the sender without relation to the previous message.
* @param blockThreshold The threshold for back pressure. If null, the message is added to the sequential processor without back pressure.
* @throws UnregisteredRecipientException If the recipient actor is not registered.
*/
void send(Object message, SelfUpdatingActorAccessor sender, ReplyHandler replyHandler, Integer blockThreshold) throws UnregisteredRecipientException {
if (!registered) {
throw new UnregisteredRecipientException("Actor " + name + " is not registered");
Expand Down Expand Up @@ -94,15 +117,31 @@ private void handleReply(Object reply, SelfUpdatingActorAccessor self, SelfUpdat
}
}

/**
* Unregisters the actor gracefully. This method sets the 'registered' flag to false
* and calls the unregisterGracefully() method of the sequential processor associated with the actor.
*/
void unregisterGracefully() {
registered = false;
sequentialProcessor.unregisterGracefully();
}

/**
* Shuts down the sequential processor.
*
* @param now true if the shutdown should happen immediately, false if it should be graceful
*/
void shutdown(boolean now) {
sequentialProcessor.shutdown(now);
}

/**
* Executes the specified reply handler with the given reply object.
*
* @param replyHandler The handler for the reply.
* @param reply The reply object.
* @throws UnregisteredRecipientException If the actor is not registered.
*/
public void run(ReplyHandler replyHandler, Object reply) {
if (!registered) {
throw new UnregisteredRecipientException("Actor " + name + " is not registered");
Expand Down Expand Up @@ -130,14 +169,29 @@ public String toString() {
'}';
}

/**
* Returns the total number of messages processed by the actor.
*
* @return The total number of messages processed.
*/
public long getTotalMessages() {
return totalMessages.get();
}

/**
* Returns the total number of replies processed by the actor.
*
* @return The total number of replies processed.
*/
public long getTotalReplies() {
return totalReplies.get();
}

/**
* Returns the number of messages currently queued in the SequentialProcessor.
*
* @return The number of queued messages.
*/
public int getQueued() {
return sequentialProcessor.size();
}
Expand Down
20 changes: 19 additions & 1 deletion src/main/java/paxel/lintstone/impl/ActorSettingsBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,37 @@
import paxel.lintstone.api.ActorSettings;
import paxel.lintstone.api.ErrorHandler;

/**
* A builder class for creating instances of {@link ActorSettings}.
*/
public class ActorSettingsBuilder {
private ErrorHandler errorHandler = x -> true;

/**
* Sets the error handler for the actor settings.
*
* @param errorHandler the error handler to set
* @return the updated instance of ActorSettingsBuilder
*/
public ActorSettingsBuilder setErrorHandler(ErrorHandler errorHandler) {
this.errorHandler = errorHandler;
return this;
}


/**
* Builds an instance of {@link ActorSettings} with the specified configuration.
*
* @return the built instance of ActorSettings
*/
public ActorSettings build() {
return new ActorSettingsImpl( errorHandler);
}

/**
* Retrieves the error handler for the actor settings.
*
* @return the error handler.
*/
public ErrorHandler getErrorHandler() {
return this.errorHandler;
}
Expand Down
3 changes: 3 additions & 0 deletions src/main/java/paxel/lintstone/impl/ActorSettingsImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
import paxel.lintstone.api.ErrorHandler;
import paxel.lintstone.api.ActorSettings;

/**
* Implementation of the {@link ActorSettings} interface. Represents the actor settings for the creation of configured actors.
*/
public record ActorSettingsImpl(ErrorHandler errorHandler) implements ActorSettings {

}
106 changes: 104 additions & 2 deletions src/main/java/paxel/lintstone/impl/ActorSystem.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,39 +9,88 @@

import paxel.lintstone.api.*;

/**
* This class represents an Actor System in the LintStone framework.
*/
public class ActorSystem implements LintStoneSystem {

private final Map<String, Actor> actors = Collections.synchronizedMap(new HashMap<>());
private final GroupingExecutor groupingExecutor;

/**
* The ActorSystem class represents the LintStone Actor system.
*/
public ActorSystem() {
groupingExecutor = new GroupingExecutor();
}

/**
* Registers an actor in the system with the specified name, factory, settings, and optional init message.
*
* @param name The name of the actor. The name must be unique in the system.
* @param factory The factory to create the actor if not already exists.
* @param settings The actor settings. Use {@link ActorSettings#create()} to create a builder and {@link ActorSettingsBuilder#build()} to build the instance.
* @param initMessage The init message. Can be null, but you should use {@link #registerActor(String, LintStoneActorFactory, ActorSettings)} then instead.
* @return The {@link LintStoneActorAccessor} for the registered actor.
*/
@Override
public LintStoneActorAccessor registerActor(String name, LintStoneActorFactory factory, ActorSettings settings, Object initMessage) {
return registerActor(name, factory, null, settings, initMessage);
}

/**
* Registers an actor in the system with the specified name, factory, and settings.
*
* @param name The name of the actor. The name must be unique in the system.
* @param factory The factory to create the actor if it does not already exist.
* @param settings The actor settings.
* @return The LintStoneActorAccessor for the registered actor.
*/
@Override
public LintStoneActorAccessor registerActor(String name, LintStoneActorFactory factory, ActorSettings settings) {
return registerActor(name, factory, null, settings, null);
}

/**
* Retrieves the LintStoneActorAccessor for the actor with the specified name.
*
* @param name The name of the actor. The name must be unique in the system.
* @return The LintStoneActorAccessor for the actor with the specified name.
*/
@Override
public LintStoneActorAccessor getActor(String name) {
synchronized (actors) {
return new SelfUpdatingActorAccessor(name, actors.get(name), this, null);
}
}

/**
* Registers an actor in the system with the specified name, factory, sender, settings, and optional init message.
*
* @param name The name of the actor. The name must be unique in the system.
* @param factory The factory to create the actor if not already exists.
* @param sender The sender of the actor.
* @param settings The actor settings. Use {@link ActorSettings#create()} to create a builder and {@link ActorSettingsBuilder#build()} to build the instance.
* @param initMessage The init message. Can be null, but you should use {@link #registerActor(String, LintStoneActorFactory, ActorSettings)} then instead.
* @return The {@link LintStoneActorAccessor} for the registered actor.
*/
LintStoneActorAccessor registerActor(String name, LintStoneActorFactory factory, SelfUpdatingActorAccessor sender, ActorSettings settings, Object initMessage) {
SequentialProcessorBuilder sequentialProcessorBuilder = groupingExecutor.create();
sequentialProcessorBuilder.setErrorHandler(settings.errorHandler());
return registerActor(name, factory, initMessage, sender, sequentialProcessorBuilder);
}


/**
* Registers an actor in the system with the specified name, factory, initMessage, sender, and sequentialProcessor.
*
* @param name The name of the actor. The name must be unique in the system.
* @param factory The factory to create the actor if not already exists.
* @param initMessage The initialization message for the actor. Can be null.
* @param sender The sender of the actor.
* @param sequentialProcessor The sequential processor for the actor.
* @return The LintStoneActorAccessor for the registered actor.
*/
private LintStoneActorAccessor registerActor(String name, LintStoneActorFactory factory, Object initMessage, SelfUpdatingActorAccessor sender, SequentialProcessorBuilder sequentialProcessor) {
synchronized (actors) {
Actor existing = actors.get(name);
Expand All @@ -58,12 +107,30 @@ private LintStoneActorAccessor registerActor(String name, LintStoneActorFactory
}


/**
* Shuts down the Actor system.
*
* This method will stop all Actors in the system after all messages are processed.
* The method returns immediately, but it does not guarantee that all messages are processed on return.
*
* This method is called internally to gracefully shut down the system.
* It first calls the private method shutdownActors with a parameter "false" to shutdown all Actors in the system.
* Then it calls the shutdown method of the groupingExecutor to shut down the executor service used by the system.
*
* @see ActorSystem#shutdownActors(boolean)
* @see LintStoneSystem#shutDown()
*/
@Override
public void shutDown() {
shutdownActors(false);
groupingExecutor.shutdown();
}

/**
* Shuts down the ActorSystem and waits for all Actors to complete processing their messages.
*
* @throws InterruptedException If the thread is interrupted while waiting for termination.
*/
@Override
public void shutDownAndWait() throws InterruptedException {
shutdownActors(false);
Expand All @@ -72,26 +139,55 @@ public void shutDownAndWait() throws InterruptedException {
groupingExecutor.awaitTermination(Long.MAX_VALUE, TimeUnit.HOURS);
}

/**
* Shuts down the ActorSystem and waits for all Actors to complete processing their messages.
*
* @param timeout the duration to wait
* @return true if all Actors have terminated within the specified timeout, false otherwise
* @throws InterruptedException if the thread is interrupted while waiting for termination
*/
@Override
public boolean shutDownAndWait(Duration timeout) throws InterruptedException {
shutdownActors(false);
groupingExecutor.shutdown();
return groupingExecutor.awaitTermination(timeout.getSeconds(), TimeUnit.SECONDS);
}

/**
* Shuts down the ActorSystem immediately.
*
* This method shuts down all Actors in the system and terminates the executor service used by the system.
*
* @see ActorSystem#shutdownActors(boolean)
* @see ActorSystem#groupingExecutor
*/
@Override
public void shutDownNow() {
shutdownActors(true);
groupingExecutor.shutdownNow();
}

/**
* Shuts down the actors in the system.
*
* This method is called internally to gracefully shut down the system.
* It iterates over all the actors in the system and calls their shutdown method.
* The shutdown of each actor can be immediate or graceful depending on the value of the "now" parameter.
*
* @param now true if the shutdown should be immediate, false if it should be graceful
*/
private void shutdownActors(boolean now) {
synchronized (actors) {
actors.entrySet().stream().map(Map.Entry::getValue).forEach(a -> a.shutdown(now));
actors.values().stream().forEach(a -> a.shutdown(now));
}
}


/**
* Unregisters an actor from the system.
*
* @param name The name of the actor to be removed.
* @return true if the actor was successfully unregistered, false otherwise.
*/
@Override
public boolean unregisterActor(String name) {
synchronized (actors) {
Expand All @@ -105,6 +201,12 @@ public boolean unregisterActor(String name) {
}
}

/**
* Retrieves an optional Actor object by name.
*
* @param name The name of the Actor.
* @return An Optional<Actor> object representing the Actor with the specified name, or an empty Optional if the Actor does not exist.
*/
Optional<Actor> getOptionalActor(String name) {
synchronized (actors) {
return Optional.ofNullable(actors.get(name));
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/paxel/lintstone/impl/FailedMessage.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@

import java.util.Objects;

/**
* Represents a failed message in the framework.
* This class implements the LintStoneFailedMessage interface.
* It contains the failed message, the cause of the failure, and the name of the actor where the failure occurred.
*/
public record FailedMessage(Object message, Throwable cause, String actorName) implements LintStoneFailedMessage {

@Override
Expand Down
Loading

0 comments on commit f2cbbd7

Please sign in to comment.