|
18 | 18 | import jakarta.nosql.Entity; |
19 | 19 | import jakarta.nosql.Id; |
20 | 20 |
|
| 21 | +import java.util.Objects; |
| 22 | +import java.util.UUID; |
| 23 | + |
21 | 24 | @Entity |
22 | | -public record Book(@Id String id, @Column("title") String title, @Column("edition") int edition) { |
| 25 | +public final class Book { |
| 26 | + @Id |
| 27 | + private final String id; |
| 28 | + @Column("title") |
| 29 | + private final String title; |
| 30 | + @Column("edition") |
| 31 | + private final int edition; |
| 32 | + @Column("author") |
| 33 | + private final Author author; |
| 34 | + |
| 35 | + public Book( |
| 36 | + @Id String id, |
| 37 | + @Column("title") String title, |
| 38 | + @Column("edition") int edition, |
| 39 | + @Column("author") Author author) { |
| 40 | + this.id = id; |
| 41 | + this.title = title; |
| 42 | + this.edition = edition; |
| 43 | + this.author = author; |
| 44 | + } |
| 45 | + |
| 46 | + public Book newEdition() { |
| 47 | + return new Book(UUID.randomUUID().toString(), |
| 48 | + this.title, |
| 49 | + this.edition + 1, |
| 50 | + this.author); |
| 51 | + } |
| 52 | + |
| 53 | + public Book updateEdition(int edition) { |
| 54 | + return new Book(this.id, |
| 55 | + this.title, |
| 56 | + this.edition + 1, |
| 57 | + this.author); |
| 58 | + } |
| 59 | + |
| 60 | + public String id() { |
| 61 | + return id; |
| 62 | + } |
| 63 | + |
| 64 | + public String title() { |
| 65 | + return title; |
| 66 | + } |
| 67 | + |
| 68 | + public int edition() { |
| 69 | + return edition; |
| 70 | + } |
| 71 | + |
| 72 | + public Author author() { |
| 73 | + return author; |
| 74 | + } |
| 75 | + |
| 76 | + @Override |
| 77 | + public boolean equals(Object obj) { |
| 78 | + if (obj == this) return true; |
| 79 | + if (obj == null || obj.getClass() != this.getClass()) return false; |
| 80 | + var that = (Book) obj; |
| 81 | + return Objects.equals(this.id, that.id) && |
| 82 | + Objects.equals(this.title, that.title) && |
| 83 | + this.edition == that.edition && |
| 84 | + Objects.equals(this.author, that.author); |
| 85 | + } |
| 86 | + |
| 87 | + @Override |
| 88 | + public int hashCode() { |
| 89 | + return Objects.hash(id, title, edition, author); |
| 90 | + } |
| 91 | + |
| 92 | + @Override |
| 93 | + public String toString() { |
| 94 | + return "Book[" + |
| 95 | + "id=" + id + ", " + |
| 96 | + "title=" + title + ", " + |
| 97 | + "edition=" + edition + ", " + |
| 98 | + "author=" + author + ']'; |
| 99 | + } |
23 | 100 |
|
24 | 101 |
|
25 | 102 | } |
0 commit comments