Java 26392 Move modules to patterns-module (#15060)
* JAVA-26392 Move axon module and ddd module to patterns-modules * JAVA-26292 Move ddd-contexts to patterns-module
This commit is contained in:
+79
@@ -0,0 +1,79 @@
|
||||
package com.baeldung.dddcontexts.infrastructure.db;
|
||||
|
||||
import com.baeldung.dddcontexts.ordercontext.model.CustomerOrder;
|
||||
import com.baeldung.dddcontexts.ordercontext.repository.CustomerOrderRepository;
|
||||
import com.baeldung.dddcontexts.shippingcontext.model.PackageItem;
|
||||
import com.baeldung.dddcontexts.shippingcontext.model.ShippableOrder;
|
||||
import com.baeldung.dddcontexts.shippingcontext.repository.ShippingOrderRepository;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class InMemoryOrderStore implements CustomerOrderRepository, ShippingOrderRepository {
|
||||
private Map<Integer, PersistenceOrder> ordersDb = new HashMap<>();
|
||||
private volatile static InMemoryOrderStore instance = new InMemoryOrderStore();
|
||||
|
||||
@Override
|
||||
public void saveCustomerOrder(CustomerOrder order) {
|
||||
this.ordersDb.put(order.getOrderId(), new PersistenceOrder(order.getOrderId(),
|
||||
order.getPaymentMethod(),
|
||||
order.getAddress(),
|
||||
order
|
||||
.getOrderItems()
|
||||
.stream()
|
||||
.map(orderItem ->
|
||||
new PersistenceOrder.OrderItem(orderItem.getProductId(),
|
||||
orderItem.getQuantity(),
|
||||
orderItem.getUnitWeight(),
|
||||
orderItem.getUnitPrice()))
|
||||
.collect(Collectors.toList())
|
||||
));
|
||||
}
|
||||
|
||||
@Override
|
||||
public Optional<ShippableOrder> findShippableOrder(int orderId) {
|
||||
if (!this.ordersDb.containsKey(orderId)) return Optional.empty();
|
||||
PersistenceOrder orderRecord = this.ordersDb.get(orderId);
|
||||
return Optional.of(
|
||||
new ShippableOrder(orderRecord.orderId, orderRecord.orderItems
|
||||
.stream().map(orderItem -> new PackageItem(orderItem.productId,
|
||||
orderItem.itemWeight,
|
||||
orderItem.quantity * orderItem.unitPrice)
|
||||
).collect(Collectors.toList())));
|
||||
}
|
||||
|
||||
public static InMemoryOrderStore provider() {
|
||||
return instance;
|
||||
}
|
||||
|
||||
public static class PersistenceOrder {
|
||||
public int orderId;
|
||||
public String paymentMethod;
|
||||
public String address;
|
||||
public List<OrderItem> orderItems;
|
||||
|
||||
public PersistenceOrder(int orderId, String paymentMethod, String address, List<OrderItem> orderItems) {
|
||||
this.orderId = orderId;
|
||||
this.paymentMethod = paymentMethod;
|
||||
this.address = address;
|
||||
this.orderItems = orderItems;
|
||||
}
|
||||
|
||||
public static class OrderItem {
|
||||
public int productId;
|
||||
public float unitPrice;
|
||||
public float itemWeight;
|
||||
public int quantity;
|
||||
|
||||
public OrderItem(int productId, int quantity, float unitWeight, float unitPrice) {
|
||||
this.itemWeight = unitWeight;
|
||||
this.quantity = quantity;
|
||||
this.unitPrice = unitPrice;
|
||||
this.productId = productId;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
+39
@@ -0,0 +1,39 @@
|
||||
package com.baeldung.dddcontexts.infrastructure.events;
|
||||
|
||||
import com.baeldung.dddcontexts.sharedkernel.events.ApplicationEvent;
|
||||
import com.baeldung.dddcontexts.sharedkernel.events.EventBus;
|
||||
import com.baeldung.dddcontexts.sharedkernel.events.EventSubscriber;
|
||||
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.concurrent.CopyOnWriteArraySet;
|
||||
|
||||
public class SimpleEventBus implements EventBus {
|
||||
private final Map<String, Set<EventSubscriber>> subscribers = new ConcurrentHashMap<>();
|
||||
|
||||
@Override
|
||||
public <E extends ApplicationEvent> void publish(E event) {
|
||||
if (subscribers.containsKey(event.getType())) {
|
||||
subscribers.get(event.getType())
|
||||
.forEach(subscriber -> subscriber.onEvent(event));
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public <E extends ApplicationEvent> void subscribe(String eventType, EventSubscriber subscriber) {
|
||||
Set<EventSubscriber> eventSubscribers = subscribers.get(eventType);
|
||||
if (eventSubscribers == null) {
|
||||
eventSubscribers = new CopyOnWriteArraySet<>();
|
||||
subscribers.put(eventType, eventSubscribers);
|
||||
}
|
||||
eventSubscribers.add(subscriber);
|
||||
}
|
||||
|
||||
@Override
|
||||
public <E extends ApplicationEvent> void unsubscribe(String eventType, EventSubscriber subscriber) {
|
||||
if (subscribers.containsKey(eventType)) {
|
||||
subscribers.get(eventType).remove(subscriber);
|
||||
}
|
||||
}
|
||||
}
|
||||
+14
@@ -0,0 +1,14 @@
|
||||
import com.baeldung.dddcontexts.infrastructure.db.InMemoryOrderStore;
|
||||
import com.baeldung.dddcontexts.infrastructure.events.SimpleEventBus;
|
||||
|
||||
module com.baeldung.dddcontexts.infrastructure {
|
||||
requires transitive com.baeldung.dddcontexts.sharedkernel;
|
||||
requires transitive com.baeldung.dddcontexts.ordercontext;
|
||||
requires transitive com.baeldung.dddcontexts.shippingcontext;
|
||||
provides com.baeldung.dddcontexts.sharedkernel.events.EventBus
|
||||
with SimpleEventBus;
|
||||
provides com.baeldung.dddcontexts.ordercontext.repository.CustomerOrderRepository
|
||||
with InMemoryOrderStore;
|
||||
provides com.baeldung.dddcontexts.shippingcontext.repository.ShippingOrderRepository
|
||||
with InMemoryOrderStore;
|
||||
}
|
||||
Reference in New Issue
Block a user