Skip to content

Commit

Permalink
Change example to use Coupon instead of Topic
Browse files Browse the repository at this point in the history
  • Loading branch information
vladmihalcea committed Oct 28, 2021
1 parent 07b20c8 commit 95ff7d1
Show file tree
Hide file tree
Showing 4 changed files with 319 additions and 231 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@

import javax.persistence.*;
import java.io.Serializable;
import java.sql.Timestamp;
import java.time.LocalDateTime;
import java.math.BigDecimal;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;

import static org.junit.Assert.assertEquals;

Expand All @@ -41,11 +42,15 @@ protected void additionalProperties(Properties properties) {
objectMapper.activateDefaultTypingAsProperty(
objectMapper.getPolymorphicTypeValidator(),
ObjectMapper.DefaultTyping.OBJECT_AND_NON_CONCRETE,
"className"
"type"
),
ArrayList.class
),
"json-polymorphic-list"
) {
@Override
public String getName() {
return "json-polymorphic-list";
}
}
)
)
);
Expand All @@ -54,17 +59,15 @@ protected void additionalProperties(Properties properties) {
@Test
public void test() {

Timestamp validUntil = Timestamp.valueOf(LocalDateTime.now().plusDays(10));

doInJPA(entityManager -> {
entityManager.persist(
new Book()
.setIsbn("978-9730228236")
.addTopic(new Post("High-Performance Java Persistence")
.setContent("It rocks!")
.addCoupon(new AmountDiscountCoupon("PPP")
.setAmount(new BigDecimal("4.99"))
)
.addTopic(new Announcement("Black Friday - 50% discount")
.setValidUntil(validUntil)
.addCoupon(new PercentageDiscountCoupon("Black Friday")
.setPercentage(BigDecimal.valueOf(0.02))
)
);
});
Expand All @@ -74,14 +77,28 @@ public void test() {
.bySimpleNaturalId(Book.class)
.load("978-9730228236");

List<Topic> topics = book.getTopics();
Map<String, DiscountCoupon> topics = book.getCoupons()
.stream()
.collect(
Collectors.toMap(
DiscountCoupon::getName,
Function.identity()
)
);

assertEquals(2, topics.size());
Post post = (Post) topics.get(0);
assertEquals("It rocks!", post.getContent());
Announcement announcement = (Announcement) topics.get(1);
AmountDiscountCoupon amountDiscountCoupon = (AmountDiscountCoupon)
topics.get("PPP");
assertEquals(
new BigDecimal("4.99"),
amountDiscountCoupon.getAmount()
);

PercentageDiscountCoupon percentageDiscountCoupon = (PercentageDiscountCoupon)
topics.get("Black Friday");
assertEquals(
validUntil.getTime(),
announcement.getValidUntil().getTime()
BigDecimal.valueOf(0.02),
percentageDiscountCoupon.getPercentage()
);
});
}
Expand All @@ -100,7 +117,7 @@ public static class Book {

@Type(type = "json-polymorphic-list")
@Column(columnDefinition = "jsonb")
private List<Topic> topics = new ArrayList<>();
private List<DiscountCoupon> coupons = new ArrayList<>();

public String getIsbn() {
return isbn;
Expand All @@ -111,83 +128,92 @@ public Book setIsbn(String isbn) {
return this;
}

public List<Topic> getTopics() {
return topics;
public List<DiscountCoupon> getCoupons() {
return coupons;
}

public Book setTopics(List<Topic> topics) {
this.topics = topics;
public Book setCoupons(List<DiscountCoupon> coupons) {
this.coupons = coupons;
return this;
}

public Book addTopic(Topic topic) {
topics.add(topic);
public Book addCoupon(DiscountCoupon topic) {
coupons.add(topic);
return this;
}
}

public static abstract class Topic implements Serializable {
public abstract static class DiscountCoupon implements Serializable {

private String title;
private String name;

public DiscountCoupon() {
}

public Topic() {
public DiscountCoupon(String name) {
this.name = name;
}

public Topic(String title) {
this.title = title;
public String getName() {
return name;
}

public String getTitle() {
return title;
public void setName(String name) {
this.name = name;
}

public void setTitle(String title) {
this.title = title;
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof DiscountCoupon)) return false;
DiscountCoupon that = (DiscountCoupon) o;
return Objects.equals(getName(), that.getName());
}

String getType() {
return getClass().getSimpleName();
@Override
public int hashCode() {
return Objects.hash(getName());
}
}

public static class Post extends Topic {
public static class AmountDiscountCoupon extends DiscountCoupon {

private String content;
private BigDecimal amount;

public Post() {
public AmountDiscountCoupon() {
}

public Post(String title) {
super(title);
public AmountDiscountCoupon(String name) {
super(name);
}

public String getContent() {
return content;
public BigDecimal getAmount() {
return amount;
}

public Post setContent(String content) {
this.content = content;
public AmountDiscountCoupon setAmount(BigDecimal amount) {
this.amount = amount;
return this;
}
}

public static class Announcement extends Topic {
public static class PercentageDiscountCoupon extends DiscountCoupon {

private Date validUntil;
private BigDecimal percentage;

public Announcement() {
public PercentageDiscountCoupon() {
}

public Announcement(String title) {
super(title);
public PercentageDiscountCoupon(String name) {
super(name);
}

public Date getValidUntil() {
return validUntil;
public BigDecimal getPercentage() {
return percentage;
}

public Announcement setValidUntil(Date validUntil) {
this.validUntil = validUntil;
public PercentageDiscountCoupon setPercentage(BigDecimal amount) {
this.percentage = amount;
return this;
}
}
Expand Down
Loading

0 comments on commit 95ff7d1

Please sign in to comment.