From 452d36949f8da9d12eafddb4d88a7682cebdb28e Mon Sep 17 00:00:00 2001 From: Steven van Beelen Date: Fri, 19 Mar 2021 11:01:19 +0100 Subject: [PATCH] Update Query API Change the OrderedProduct model such that it contains a map of products having the count as its value, instead of a single product. #BAEL-4767 --- .../axon/coreapi/queries/OrderedProduct.java | 56 ++++++++++++------- 1 file changed, 37 insertions(+), 19 deletions(-) diff --git a/axon/src/main/java/com/baeldung/axon/coreapi/queries/OrderedProduct.java b/axon/src/main/java/com/baeldung/axon/coreapi/queries/OrderedProduct.java index d847bb2a98..69e705c2f6 100644 --- a/axon/src/main/java/com/baeldung/axon/coreapi/queries/OrderedProduct.java +++ b/axon/src/main/java/com/baeldung/axon/coreapi/queries/OrderedProduct.java @@ -1,16 +1,18 @@ package com.baeldung.axon.coreapi.queries; +import java.util.HashMap; +import java.util.Map; import java.util.Objects; public class OrderedProduct { private final String orderId; - private final String product; + private final Map products; private OrderStatus orderStatus; - public OrderedProduct(String orderId, String product) { + public OrderedProduct(String orderId) { this.orderId = orderId; - this.product = product; + this.products = new HashMap<>(); orderStatus = OrderStatus.PLACED; } @@ -18,14 +20,31 @@ public class OrderedProduct { return orderId; } - public String getProduct() { - return product; + public Map getProducts() { + return products; } public OrderStatus getOrderStatus() { return orderStatus; } + public void addProduct(String productId) { + products.putIfAbsent(productId, 1); + } + + public void incrementProductInstance(String productId) { + products.computeIfPresent(productId, (id, count) -> ++count); + } + + public void decrementProductInstance(String productId) { + products.computeIfPresent(productId, (id, count) -> --count); + } + + + public void removeProduct(String productId) { + products.remove(productId); + } + public void setOrderConfirmed() { this.orderStatus = OrderStatus.CONFIRMED; } @@ -35,29 +54,28 @@ public class OrderedProduct { } @Override - public int hashCode() { - return Objects.hash(orderId, product, orderStatus); + public boolean equals(Object o) { + if (this == o) { + return true; + } + if (o == null || getClass() != o.getClass()) { + return false; + } + OrderedProduct that = (OrderedProduct) o; + return Objects.equals(orderId, that.orderId) && Objects.equals(products, that.products) + && orderStatus == that.orderStatus; } @Override - public boolean equals(Object obj) { - if (this == obj) { - return true; - } - if (obj == null || getClass() != obj.getClass()) { - return false; - } - final OrderedProduct other = (OrderedProduct) obj; - return Objects.equals(this.orderId, other.orderId) - && Objects.equals(this.product, other.product) - && Objects.equals(this.orderStatus, other.orderStatus); + public int hashCode() { + return Objects.hash(orderId, products, orderStatus); } @Override public String toString() { return "OrderedProduct{" + "orderId='" + orderId + '\'' + - ", product='" + product + '\'' + + ", products=" + products + ", orderStatus=" + orderStatus + '}'; }