Merge pull request #8125 from eugenp/revert-8119-BAEL-3275-2

Revert "BAEL-3275: Using blocking queue for pub-sub"
This commit is contained in:
Eric Martin
2019-10-31 20:43:47 -05:00
committed by GitHub
parent db85c8f275
commit 3225470df5
20543 changed files with 1642750 additions and 0 deletions
@@ -0,0 +1,12 @@
package com.baeldung.ddd;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class PersistingDddAggregatesApplication {
public static void main(String[] args) {
SpringApplication.run(PersistingDddAggregatesApplication.class, args);
}
}
@@ -0,0 +1,52 @@
package com.baeldung.ddd.order;
import java.util.ArrayList;
import java.util.List;
import org.joda.money.Money;
public class Order {
private final List<OrderLine> orderLines;
private Money totalCost;
public Order(List<OrderLine> orderLines) {
checkNotNull(orderLines);
if (orderLines.isEmpty()) {
throw new IllegalArgumentException("Order must have at least one order line item");
}
this.orderLines = new ArrayList<>(orderLines);
totalCost = calculateTotalCost();
}
public void addLineItem(OrderLine orderLine) {
checkNotNull(orderLine);
orderLines.add(orderLine);
totalCost = totalCost.plus(orderLine.cost());
}
public List<OrderLine> getOrderLines() {
return new ArrayList<>(orderLines);
}
public void removeLineItem(int line) {
OrderLine removedLine = orderLines.remove(line);
totalCost = totalCost.minus(removedLine.cost());
}
public Money totalCost() {
return totalCost;
}
private Money calculateTotalCost() {
return orderLines.stream()
.map(OrderLine::cost)
.reduce(Money::plus)
.get();
}
private static void checkNotNull(Object par) {
if (par == null) {
throw new NullPointerException("Parameter cannot be null");
}
}
}
@@ -0,0 +1,67 @@
package com.baeldung.ddd.order;
import org.joda.money.Money;
public class OrderLine {
private final Product product;
private final int quantity;
public OrderLine(Product product, int quantity) {
super();
this.product = product;
this.quantity = quantity;
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
OrderLine other = (OrderLine) obj;
if (product == null) {
if (other.product != null) {
return false;
}
} else if (!product.equals(other.product)) {
return false;
}
if (quantity != other.quantity) {
return false;
}
return true;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((product == null) ? 0 : product.hashCode());
result = prime * result + quantity;
return result;
}
@Override
public String toString() {
return "OrderLine [product=" + product + ", quantity=" + quantity + "]";
}
Money cost() {
return product.getPrice()
.multipliedBy(quantity);
}
Product getProduct() {
return product;
}
int getQuantity() {
return quantity;
}
}
@@ -0,0 +1,52 @@
package com.baeldung.ddd.order;
import org.joda.money.Money;
public class Product {
private final Money price;
public Product(Money price) {
super();
this.price = price;
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
Product other = (Product) obj;
if (price == null) {
if (other.price != null) {
return false;
}
} else if (!price.equals(other.price)) {
return false;
}
return true;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((price == null) ? 0 : price.hashCode());
return result;
}
@Override
public String toString() {
return "Product [price=" + price + "]";
}
Money getPrice() {
return price;
}
}
@@ -0,0 +1,15 @@
package com.baeldung.ddd.order.doubledispatch;
import org.joda.money.CurrencyUnit;
import org.joda.money.Money;
public class AmountBasedDiscountPolicy implements DiscountPolicy {
@Override
public double discount(Order order) {
if (order.totalCost()
.isGreaterThan(Money.of(CurrencyUnit.USD, 500.00))) {
return 0.10;
} else
return 0;
}
}
@@ -0,0 +1,5 @@
package com.baeldung.ddd.order.doubledispatch;
public interface DiscountPolicy {
double discount(Order order);
}
@@ -0,0 +1,8 @@
package com.baeldung.ddd.order.doubledispatch;
public class FlatDiscountPolicy implements DiscountPolicy {
@Override
public double discount(Order order) {
return 0.01;
}
}
@@ -0,0 +1,29 @@
package com.baeldung.ddd.order.doubledispatch;
import java.math.RoundingMode;
import java.util.List;
import org.joda.money.Money;
import com.baeldung.ddd.order.OrderLine;
import com.baeldung.ddd.order.doubledispatch.visitor.OrderVisitor;
import com.baeldung.ddd.order.doubledispatch.visitor.Visitable;
public class Order extends com.baeldung.ddd.order.Order implements Visitable<OrderVisitor> {
public Order(List<OrderLine> orderLines) {
super(orderLines);
}
public Money totalCost(SpecialDiscountPolicy discountPolicy) {
return totalCost().multipliedBy(1 - applyDiscountPolicy(discountPolicy), RoundingMode.HALF_UP);
}
protected double applyDiscountPolicy(SpecialDiscountPolicy discountPolicy) {
return discountPolicy.discount(this);
}
@Override
public void accept(OrderVisitor visitor) {
visitor.visit(this);
}
}
@@ -0,0 +1,5 @@
package com.baeldung.ddd.order.doubledispatch;
public interface SpecialDiscountPolicy extends DiscountPolicy {
double discount(SpecialOrder order);
}
@@ -0,0 +1,36 @@
package com.baeldung.ddd.order.doubledispatch;
import java.util.List;
import com.baeldung.ddd.order.OrderLine;
import com.baeldung.ddd.order.doubledispatch.visitor.OrderVisitor;
public class SpecialOrder extends Order {
private boolean eligibleForExtraDiscount;
public SpecialOrder(List<OrderLine> orderLines) {
super(orderLines);
this.eligibleForExtraDiscount = false;
}
public SpecialOrder(List<OrderLine> orderLines, boolean eligibleForSpecialDiscount) {
super(orderLines);
this.eligibleForExtraDiscount = eligibleForSpecialDiscount;
}
public boolean isEligibleForExtraDiscount() {
return eligibleForExtraDiscount;
}
@Override
protected double applyDiscountPolicy(SpecialDiscountPolicy discountPolicy) {
return discountPolicy.discount(this);
}
@Override
public void accept(OrderVisitor visitor) {
visitor.visit(this);
}
}
@@ -0,0 +1,24 @@
package com.baeldung.ddd.order.doubledispatch.visitor;
import com.baeldung.ddd.order.doubledispatch.Order;
import com.baeldung.ddd.order.doubledispatch.SpecialOrder;
public class HtmlOrderViewCreator implements OrderVisitor {
private String html;
public String getHtml() {
return html;
}
@Override
public void visit(Order order) {
html = String.format("<p>Regular order total cost: %s</p>", order.totalCost());
}
@Override
public void visit(SpecialOrder order) {
html = String.format("<h1>Special Order</h1><p>total cost: %s</p>", order.totalCost());
}
}
@@ -0,0 +1,9 @@
package com.baeldung.ddd.order.doubledispatch.visitor;
import com.baeldung.ddd.order.doubledispatch.Order;
import com.baeldung.ddd.order.doubledispatch.SpecialOrder;
public interface OrderVisitor {
void visit(Order order);
void visit(SpecialOrder order);
}
@@ -0,0 +1,5 @@
package com.baeldung.ddd.order.doubledispatch.visitor;
public interface Visitable<V> {
void accept(V visitor);
}
@@ -0,0 +1,111 @@
package com.baeldung.ddd.order.jpa;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.ElementCollection;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name = "order_table")
class JpaOrder {
private String currencyUnit;
@Id
@GeneratedValue
private Long id;
@ElementCollection(fetch = FetchType.EAGER)
private final List<JpaOrderLine> orderLines;
private BigDecimal totalCost;
JpaOrder() {
totalCost = null;
orderLines = new ArrayList<>();
}
JpaOrder(List<JpaOrderLine> orderLines) {
checkNotNull(orderLines);
if (orderLines.isEmpty()) {
throw new IllegalArgumentException("Order must have at least one order line item");
}
this.orderLines = new ArrayList<>(orderLines);
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
JpaOrder other = (JpaOrder) obj;
if (id == null) {
if (other.id != null) {
return false;
}
} else if (!id.equals(other.id)) {
return false;
}
return true;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((id == null) ? 0 : id.hashCode());
return result;
}
@Override
public String toString() {
return "JpaOrder [currencyUnit=" + currencyUnit + ", id=" + id + ", orderLines=" + orderLines + ", totalCost=" + totalCost + "]";
}
void addLineItem(JpaOrderLine orderLine) {
checkNotNull(orderLine);
orderLines.add(orderLine);
}
String getCurrencyUnit() {
return currencyUnit;
}
Long getId() {
return id;
}
List<JpaOrderLine> getOrderLines() {
return new ArrayList<>(orderLines);
}
BigDecimal getTotalCost() {
return totalCost;
}
void removeLineItem(int line) {
orderLines.remove(line);
}
void setCurrencyUnit(String currencyUnit) {
this.currencyUnit = currencyUnit;
}
void setTotalCost(BigDecimal totalCost) {
this.totalCost = totalCost;
}
private static void checkNotNull(Object par) {
if (par == null) {
throw new NullPointerException("Parameter cannot be null");
}
}
}
@@ -0,0 +1,70 @@
package com.baeldung.ddd.order.jpa;
import javax.persistence.Embeddable;
import javax.persistence.Embedded;
@Embeddable
class JpaOrderLine {
@Embedded
private final JpaProduct product;
private final int quantity;
JpaOrderLine() {
quantity = 0;
product = null;
}
JpaOrderLine(JpaProduct product, int quantity) {
super();
this.product = product;
this.quantity = quantity;
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
JpaOrderLine other = (JpaOrderLine) obj;
if (product == null) {
if (other.product != null) {
return false;
}
} else if (!product.equals(other.product)) {
return false;
}
if (quantity != other.quantity) {
return false;
}
return true;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((product == null) ? 0 : product.hashCode());
result = prime * result + quantity;
return result;
}
@Override
public String toString() {
return "JpaOrderLine [product=" + product + ", quantity=" + quantity + "]";
}
JpaProduct getProduct() {
return product;
}
int getQuantity() {
return quantity;
}
}
@@ -0,0 +1,7 @@
package com.baeldung.ddd.order.jpa;
import org.springframework.data.jpa.repository.JpaRepository;
public interface JpaOrderRepository extends JpaRepository<JpaOrder, Long> {
}
@@ -0,0 +1,79 @@
package com.baeldung.ddd.order.jpa;
import java.math.BigDecimal;
import javax.persistence.Embeddable;
@Embeddable
class JpaProduct {
private String currencyUnit;
private BigDecimal price;
public JpaProduct() {
}
public JpaProduct(BigDecimal price, String currencyUnit) {
super();
this.price = price;
this.currencyUnit = currencyUnit;
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
JpaProduct other = (JpaProduct) obj;
if (currencyUnit == null) {
if (other.currencyUnit != null) {
return false;
}
} else if (!currencyUnit.equals(other.currencyUnit)) {
return false;
}
if (price == null) {
if (other.price != null) {
return false;
}
} else if (!price.equals(other.price)) {
return false;
}
return true;
}
public String getCurrencyUnit() {
return currencyUnit;
}
public BigDecimal getPrice() {
return price;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((currencyUnit == null) ? 0 : currencyUnit.hashCode());
result = prime * result + ((price == null) ? 0 : price.hashCode());
return result;
}
public void setCurrencyUnit(String currencyUnit) {
this.currencyUnit = currencyUnit;
}
public void setPrice(BigDecimal price) {
this.price = price;
}
@Override
public String toString() {
return "JpaProduct [currencyUnit=" + currencyUnit + ", price=" + price + "]";
}
}
@@ -0,0 +1,9 @@
package com.baeldung.ddd.order.mongo;
import org.springframework.data.mongodb.repository.MongoRepository;
import com.baeldung.ddd.order.Order;
public interface OrderMongoRepository extends MongoRepository<Order, String> {
}