Skip to content

Commit

Permalink
Refactoring (#47)
Browse files Browse the repository at this point in the history
  • Loading branch information
IsmailMarmoush authored Apr 15, 2024
1 parent 53e9092 commit 81c889c
Show file tree
Hide file tree
Showing 43 changed files with 172 additions and 170 deletions.
3 changes: 3 additions & 0 deletions .docs/ADRs/ADR003-MetaRecordToClass.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# CommandMeta and EventMeta are classes not records

https://stackoverflow.com/questions/73424610/optionalt-as-a-record-parameter-in-java
4 changes: 2 additions & 2 deletions core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@
<parent>
<groupId>io.memoria</groupId>
<artifactId>atom</artifactId>
<version>21.34.0</version>
<version>21.35.0</version>
</parent>
<groupId>io.memoria.atom</groupId>
<artifactId>core</artifactId>
<version>21.34.0</version>
<version>21.35.0</version>
<packaging>jar</packaging>
<name>${project.groupId}.${project.artifactId}</name>
<description>Core Module</description>
Expand Down
6 changes: 3 additions & 3 deletions eventsourcing/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@
<parent>
<groupId>io.memoria</groupId>
<artifactId>atom</artifactId>
<version>21.34.0</version>
<version>21.35.0</version>
</parent>

<groupId>io.memoria.atom</groupId>
<artifactId>eventsourcing</artifactId>
<version>21.34.0</version>
<version>21.35.0</version>
<packaging>jar</packaging>

<name>${project.groupId}.${project.artifactId}</name>
Expand All @@ -22,7 +22,7 @@
<!-- Modules Dependencies -->
<!--======================================================================================== -->
<dependency>
<groupId>io.memoria.atom</groupId>
<groupId>${project.groupId}</groupId>
<artifactId>core</artifactId>
<version>${project.parent.version}</version>
</dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public interface Decider {
* @param eventMeta the new EventMeta
* @return a new Event with eventMeta as its value
*/
Event createBy(Command command, EventMeta eventMeta);
Event decide(Command command, EventMeta eventMeta) throws CommandException;

/**
* @param state the initial state
Expand All @@ -33,11 +33,11 @@ public interface Decider {
*/
Event decide(State state, Command command, EventMeta eventMeta) throws CommandException;

default Event apply(Command command) {
return createBy(command, eventMeta(command));
default Event decide(Command command) throws CommandException {
return decide(command, eventMeta(command));
}

default Event apply(State state, Command command) throws CommandException {
default Event decide(State state, Command command) throws CommandException {
return decide(state, command, eventMeta(state, command));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,9 @@ public Optional<Event> handle(Command command) throws CommandException {
Event event;
if (stateRef.get() == null) {
eventRepo.fetch(stateId()).forEach(this::evolve);
event = decider.apply(command);
event = decider.decide(command);
} else {
event = decider.apply(stateRef.get(), command);
event = decider.decide(stateRef.get(), command);
}
eventRepo.append(event);
command.meta().sagaSource().ifPresent(sagaSources::add);
Expand All @@ -67,9 +67,9 @@ void evolve(Event event) {
State currentState = stateRef.get();
State newState;
if (isInitializerEvent(event)) {
newState = evolver.apply(event);
newState = evolver.evolve(event);
} else {
newState = evolver.apply(currentState, event);
newState = evolver.evolve(currentState, event);
}
stateRef.set(newState);
processedCommands.add(event.meta().commandId());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package io.memoria.atom.eventsourcing.aggregate;

import io.memoria.atom.eventsourcing.event.Event;
import io.memoria.atom.eventsourcing.event.exceptions.InvalidEvolutionEvent;
import io.memoria.atom.eventsourcing.event.exceptions.InvalidEvent;
import io.memoria.atom.eventsourcing.state.State;
import io.memoria.atom.eventsourcing.state.StateMeta;

Expand All @@ -12,7 +12,7 @@ public interface Evolver {
* @param stateMeta the new State stateMeta
* @return a new State with stateMeta as its meta value
*/
State createBy(Event event, StateMeta stateMeta);
State evolve(Event event, StateMeta stateMeta);

/**
* @param state initial State
Expand All @@ -22,11 +22,11 @@ public interface Evolver {
*/
State evolve(State state, Event event, StateMeta stateMeta);

default State apply(Event e) {
return createBy(e, stateMeta(e));
default State evolve(Event e) {
return evolve(e, stateMeta(e));
}

default State apply(State state, Event event) {
default State evolve(State state, Event event) {
return evolve(state, event, stateMeta(state, event));
}

Expand All @@ -36,7 +36,7 @@ default StateMeta stateMeta(Event e) {

default StateMeta stateMeta(State s, Event e) {
if (s.version() + 1 != e.version()) {
throw InvalidEvolutionEvent.of(s, e);
throw InvalidEvent.ofEvolution(s, e);
}
return s.meta().incrementVersion();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import java.util.Objects;
import java.util.Optional;

public final class CommandMeta implements Partitioned, Serializable {
public class CommandMeta implements Partitioned, Serializable {
@Serial
private static final long serialVersionUID = 0L;
private final CommandId commandId;
Expand Down Expand Up @@ -67,19 +67,7 @@ public int hashCode() {

@Override
public String toString() {
return "CommandMeta["
+ "commandId="
+ commandId
+ ", "
+ "stateId="
+ stateId
+ ", "
+ "timestamp="
+ timestamp
+ ", "
+ "sagaSource="
+ sagaSource
+ ']';
return STR."CommandMeta[commandId=\{commandId}, stateId=\{stateId}, timestamp=\{timestamp}, sagaSource=\{sagaSource}\{']'}";
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

import io.memoria.atom.eventsourcing.command.Command;

public class CommandRuntimeException extends RuntimeException {
public class CommandRTE extends RuntimeException {
private final Command command;

protected CommandRuntimeException(String msg, Command command) {
protected CommandRTE(String msg, Command command) {
super(msg);
this.command = command;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,21 @@
import io.memoria.atom.eventsourcing.command.Command;
import io.memoria.atom.eventsourcing.state.State;

public class InvalidEvolutionCommand extends CommandException {
protected InvalidEvolutionCommand(String msg, Command command) {
public class InvalidCommand extends CommandException {
protected InvalidCommand(String msg, Command command) {
super(msg, command);
}

public static InvalidEvolutionCommand of(State state, Command command) {
public static InvalidCommand ofCreation(Command command) {
var msg = "Invalid creation command: %s[%s]".formatted(command.getClass().getSimpleName(), command.meta());
return new InvalidCommand(msg, command);
}

public static InvalidCommand ofEvolution(State state, Command command) {
var msg = "Invalid evolution command: %s[%s] to the state: %s[%s]".formatted(command.getClass().getSimpleName(),
command.meta(),
state.getClass().getSimpleName(),
state.meta());
return new InvalidEvolutionCommand(msg, command);
return new InvalidCommand(msg, command);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import io.memoria.atom.eventsourcing.command.Command;
import io.memoria.atom.eventsourcing.state.StateId;

public class MismatchingCommandState extends CommandRuntimeException {
public class MismatchingCommandState extends CommandRTE {
protected MismatchingCommandState(String msg, Command command) {
super(msg, command);
}
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package io.memoria.atom.eventsourcing.command.exceptions;

import io.memoria.atom.eventsourcing.command.Command;

public class UnknownCommandRTE extends CommandRTE {
private static final String MESSAGE = "Unknown Command: %s[%s] implementation";

protected UnknownCommandRTE(Command command) {
this(MESSAGE.formatted(command.getClass().getSimpleName(), command.meta()), command);
}

protected UnknownCommandRTE(String message, Command command) {
super(message, command);
}

public static UnknownCommandRTE of(Command command) {
return new UnknownCommandRTE(command);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import java.util.Objects;
import java.util.Optional;

public final class EventMeta implements Partitioned, Versioned, Serializable {
public class EventMeta implements Partitioned, Versioned, Serializable {
@Serial
private static final long serialVersionUID = 0L;
private final EventId eventId;
Expand Down Expand Up @@ -87,26 +87,7 @@ public int hashCode() {

@Override
public String toString() {
return "EventMeta["
+ "eventId="
+ eventId
+ ", "
+ "version="
+ version
+ ", "
+ "stateId="
+ stateId
+ ", "
+ "commandId="
+ commandId
+ ", "
+ "timestamp="
+ timestamp
+ ", "
+ "sagaSource="
+ sagaSource
+ ']';
return STR."EventMeta[eventId=\{eventId}, version=\{version}, stateId=\{stateId}, commandId=\{commandId}, timestamp=\{timestamp}, sagaSource=\{sagaSource}\{']'}";
}

}

Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

import io.memoria.atom.eventsourcing.event.Event;

public class EventRuntimeException extends RuntimeException {
public class EventRTE extends RuntimeException {
private final Event event;

protected EventRuntimeException(String msg, Event event) {
protected EventRTE(String msg, Event event) {
super(msg);
this.event = event;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,21 @@
import io.memoria.atom.eventsourcing.event.Event;
import io.memoria.atom.eventsourcing.state.State;

public class InvalidEvolutionEvent extends EventRuntimeException {
protected InvalidEvolutionEvent(String msg, Event event) {
public class InvalidEvent extends EventRTE {
protected InvalidEvent(String msg, Event event) {
super(msg, event);
}

public static InvalidEvolutionEvent of(State state, Event event) {
public static InvalidEvent ofCreation(Event event) {
var msg = "Invalid creation event: %s[%s]".formatted(event.getClass().getSimpleName(), event.meta());
return new InvalidEvent(msg, event);
}

public static InvalidEvent ofEvolution(State state, Event event) {
var msg = "Invalid evolution event: %s[%s] to the state: %s[%s]".formatted(event.getClass().getSimpleName(),
event.meta(),
state.getClass().getSimpleName(),
state.meta());
return new InvalidEvolutionEvent(msg, event);
return new InvalidEvent(msg, event);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import io.memoria.atom.eventsourcing.event.Event;
import io.memoria.atom.eventsourcing.state.StateId;

public class MismatchingEvent extends EventRuntimeException {
public class MismatchingEvent extends EventRTE {
protected MismatchingEvent(String msg, Event event) {
super(msg, event);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import io.memoria.atom.eventsourcing.event.Event;

public class UnknownEvent extends EventRuntimeException {
public class UnknownEvent extends EventRTE {
private static final String MESSAGE = "Unknown Event: %s[%s] implementation";

protected UnknownEvent(Event event) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,14 @@
import io.memoria.atom.eventsourcing.state.StateId;

import java.util.Optional;
import java.util.function.Function;
import java.util.function.Supplier;

public interface Saga extends Function<Event, Optional<Command>> {
public interface Saga {
Supplier<Id> idSupplier();

Supplier<Long> timeSupplier();

Optional<Command> apply(Event event);
Optional<Command> react(Event event);

default CommandMeta commandMeta(StateId stateId, Event event) {
return new CommandMeta(CommandId.of(idSupplier().get()), stateId, timeSupplier().get(), event.meta().eventId());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ public SagaHandler(Saga saga, CommandPublisher commandPublisher) {
this.commandPublisher = commandPublisher;
}

public Optional<Command> handle(Event event) {
Optional<Command> result = saga.apply(event);
public Optional<Command> react(Event event) {
Optional<Command> result = saga.react(event);
result.ifPresent(commandPublisher::publish);
return result;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
public interface State extends Partitioned, Versioned, Serializable {
StateMeta meta();

State withMeta(StateMeta meta);

default StateId stateId() {
return meta().stateId();
}
Expand Down
Loading

0 comments on commit 81c889c

Please sign in to comment.