mirror of
https://github.com/tiennm99/java-design-patterns.git
synced 2026-06-03 22:13:42 +00:00
Java 11 migrate remaining e (#1112)
* Moves eip-aggregator to Java 11 * Moves eip-message-channel to Java 11 * Moves eip-publish-subscribe to Java 11 * Moves eip-splitter to Java 11 * Moves eip-wire-tap to Java 11 * Moves event-aggregator to Java 11 * Moves event-asynchronous to Java 11 * Moves event-driven-architecture to Java 11 * Moves event-queue to Java 11 * Moves event-sourcing to Java 11 * Moves execute-around to Java 11 * Moves extension-objects to Java 11
This commit is contained in:
committed by
Ilkka Seppälä
parent
b09b100614
commit
fb2c026822
@@ -69,7 +69,7 @@ public class App {
|
||||
*/
|
||||
public static void main(String[] args) {
|
||||
|
||||
DomainEventProcessor eventProcessor = new DomainEventProcessor();
|
||||
var eventProcessor = new DomainEventProcessor();
|
||||
|
||||
|
||||
LOGGER.info("Running the system first time............");
|
||||
|
||||
@@ -104,7 +104,7 @@ public class Account {
|
||||
* @return the account
|
||||
*/
|
||||
public Account copy() {
|
||||
Account account = new Account(accountNo, owner);
|
||||
var account = new Account(accountNo, owner);
|
||||
account.setMoney(money);
|
||||
return account;
|
||||
}
|
||||
@@ -135,7 +135,7 @@ public class Account {
|
||||
}
|
||||
|
||||
private void handleWithdrawal(BigDecimal money, boolean realTime) {
|
||||
if (this.money.compareTo(money) == -1) {
|
||||
if (this.money.compareTo(money) < 0) {
|
||||
throw new RuntimeException("Insufficient Account Balance");
|
||||
}
|
||||
|
||||
|
||||
+1
-1
@@ -72,7 +72,7 @@ public class AccountCreateEvent extends DomainEvent {
|
||||
|
||||
@Override
|
||||
public void process() {
|
||||
Account account = AccountAggregate.getAccount(accountNo);
|
||||
var account = AccountAggregate.getAccount(accountNo);
|
||||
if (account != null) {
|
||||
throw new RuntimeException("Account already exists");
|
||||
}
|
||||
|
||||
+3
-5
@@ -23,9 +23,9 @@
|
||||
|
||||
package com.iluwatar.event.sourcing.event;
|
||||
|
||||
import com.iluwatar.event.sourcing.domain.Account;
|
||||
import com.iluwatar.event.sourcing.state.AccountAggregate;
|
||||
import java.math.BigDecimal;
|
||||
import java.util.Optional;
|
||||
|
||||
/**
|
||||
* This is the class that implements money deposit event. Holds the necessary info for a money
|
||||
@@ -73,10 +73,8 @@ public class MoneyDepositEvent extends DomainEvent {
|
||||
|
||||
@Override
|
||||
public void process() {
|
||||
Account account = AccountAggregate.getAccount(accountNo);
|
||||
if (account == null) {
|
||||
throw new RuntimeException("Account not found");
|
||||
}
|
||||
var account = Optional.ofNullable(AccountAggregate.getAccount(accountNo))
|
||||
.orElseThrow(() -> new RuntimeException("Account not found"));
|
||||
account.handleEvent(this);
|
||||
}
|
||||
}
|
||||
|
||||
+5
-10
@@ -23,9 +23,9 @@
|
||||
|
||||
package com.iluwatar.event.sourcing.event;
|
||||
|
||||
import com.iluwatar.event.sourcing.domain.Account;
|
||||
import com.iluwatar.event.sourcing.state.AccountAggregate;
|
||||
import java.math.BigDecimal;
|
||||
import java.util.Optional;
|
||||
|
||||
/**
|
||||
* This is the class that implements money transfer event. Holds the necessary info for a money
|
||||
@@ -86,15 +86,10 @@ public class MoneyTransferEvent extends DomainEvent {
|
||||
|
||||
@Override
|
||||
public void process() {
|
||||
Account accountFrom = AccountAggregate.getAccount(accountNoFrom);
|
||||
if (accountFrom == null) {
|
||||
throw new RuntimeException("Account not found " + accountNoFrom);
|
||||
}
|
||||
Account accountTo = AccountAggregate.getAccount(accountNoTo);
|
||||
if (accountTo == null) {
|
||||
throw new RuntimeException("Account not found " + accountNoTo);
|
||||
}
|
||||
|
||||
var accountFrom = Optional.ofNullable(AccountAggregate.getAccount(accountNoFrom))
|
||||
.orElseThrow(() -> new RuntimeException("Account not found " + accountNoFrom));
|
||||
var accountTo = Optional.ofNullable(AccountAggregate.getAccount(accountNoTo))
|
||||
.orElseThrow(() -> new RuntimeException("Account not found " + accountNoTo));
|
||||
accountFrom.handleTransferFromEvent(this);
|
||||
accountTo.handleTransferToEvent(this);
|
||||
}
|
||||
|
||||
+2
-7
@@ -57,13 +57,8 @@ public class DomainEventProcessor {
|
||||
*/
|
||||
public void recover() {
|
||||
DomainEvent domainEvent;
|
||||
while (true) {
|
||||
domainEvent = processorJournal.readNext();
|
||||
if (domainEvent == null) {
|
||||
break;
|
||||
} else {
|
||||
domainEvent.process();
|
||||
}
|
||||
while ((domainEvent = processorJournal.readNext()) != null) {
|
||||
domainEvent.process();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+12
-12
@@ -38,7 +38,7 @@ import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStreamReader;
|
||||
import java.io.OutputStreamWriter;
|
||||
import java.io.Writer;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
@@ -60,8 +60,8 @@ public class JsonFileJournal {
|
||||
public JsonFileJournal() {
|
||||
file = new File("Journal.json");
|
||||
if (file.exists()) {
|
||||
try (BufferedReader input = new BufferedReader(
|
||||
new InputStreamReader(new FileInputStream(file), "UTF-8"))) {
|
||||
try (var input = new BufferedReader(
|
||||
new InputStreamReader(new FileInputStream(file), StandardCharsets.UTF_8))) {
|
||||
String line;
|
||||
while ((line = input.readLine()) != null) {
|
||||
events.add(line);
|
||||
@@ -81,7 +81,7 @@ public class JsonFileJournal {
|
||||
* @param domainEvent the domain event
|
||||
*/
|
||||
public void write(DomainEvent domainEvent) {
|
||||
Gson gson = new Gson();
|
||||
var gson = new Gson();
|
||||
JsonElement jsonElement;
|
||||
if (domainEvent instanceof AccountCreateEvent) {
|
||||
jsonElement = gson.toJsonTree(domainEvent, AccountCreateEvent.class);
|
||||
@@ -93,9 +93,9 @@ public class JsonFileJournal {
|
||||
throw new RuntimeException("Journal Event not recegnized");
|
||||
}
|
||||
|
||||
try (Writer output = new BufferedWriter(
|
||||
new OutputStreamWriter(new FileOutputStream(file, true), "UTF-8"))) {
|
||||
String eventString = jsonElement.toString();
|
||||
try (var output = new BufferedWriter(
|
||||
new OutputStreamWriter(new FileOutputStream(file, true), StandardCharsets.UTF_8))) {
|
||||
var eventString = jsonElement.toString();
|
||||
output.write(eventString + "\r\n");
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
@@ -120,13 +120,13 @@ public class JsonFileJournal {
|
||||
if (index >= events.size()) {
|
||||
return null;
|
||||
}
|
||||
String event = events.get(index);
|
||||
var event = events.get(index);
|
||||
index++;
|
||||
|
||||
JsonParser parser = new JsonParser();
|
||||
JsonElement jsonElement = parser.parse(event);
|
||||
String eventClassName = jsonElement.getAsJsonObject().get("eventClassName").getAsString();
|
||||
Gson gson = new Gson();
|
||||
var parser = new JsonParser();
|
||||
var jsonElement = parser.parse(event);
|
||||
var eventClassName = jsonElement.getAsJsonObject().get("eventClassName").getAsString();
|
||||
var gson = new Gson();
|
||||
DomainEvent domainEvent;
|
||||
if (eventClassName.equals("AccountCreateEvent")) {
|
||||
domainEvent = gson.fromJson(jsonElement, AccountCreateEvent.class);
|
||||
|
||||
+5
-5
@@ -26,6 +26,7 @@ package com.iluwatar.event.sourcing.state;
|
||||
import com.iluwatar.event.sourcing.domain.Account;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
|
||||
/**
|
||||
* This is the static accounts map holder class. This class holds the state of the accounts.
|
||||
@@ -55,11 +56,10 @@ public class AccountAggregate {
|
||||
* @return the copy of the account or null if not found
|
||||
*/
|
||||
public static Account getAccount(int accountNo) {
|
||||
Account account = accounts.get(accountNo);
|
||||
if (account == null) {
|
||||
return null;
|
||||
}
|
||||
return account.copy();
|
||||
return Optional.of(accountNo)
|
||||
.map(accounts::get)
|
||||
.map(Account::copy)
|
||||
.orElse(null);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user