Skip to content

Commit 898722f

Browse files
authored
Merge pull request #1372 from Privatech38/feat/book-author
feat(api): Add pages-only constructor to BookImpl
2 parents 621e48e + 4aa8675 commit 898722f

3 files changed

Lines changed: 39 additions & 12 deletions

File tree

api/src/main/java/net/kyori/adventure/inventory/Book.java

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
*/
2424
package net.kyori.adventure.inventory;
2525

26-
import java.util.ArrayList;
2726
import java.util.Collection;
2827
import java.util.List;
2928
import net.kyori.adventure.audience.Audience;
@@ -33,6 +32,8 @@
3332
import org.jetbrains.annotations.Contract;
3433
import org.jetbrains.annotations.Unmodifiable;
3534

35+
import static java.util.Objects.requireNonNull;
36+
3637
/**
3738
* Represents the in-game interface of a book.
3839
*
@@ -54,7 +55,7 @@ public sealed interface Book extends Buildable<Book.Builder>, BookLike permits B
5455
* @since 4.0.0
5556
*/
5657
static Book book(final Component title, final Component author, final Collection<Component> pages) {
57-
return new BookImpl(title, author, new ArrayList<>(pages));
58+
return new BookImpl(requireNonNull(title, "title"), requireNonNull(author, "author"), List.copyOf(requireNonNull(pages, "pages")));
5859
}
5960

6061
/**
@@ -67,7 +68,29 @@ static Book book(final Component title, final Component author, final Collection
6768
* @since 4.0.0
6869
*/
6970
static Book book(final Component title, final Component author, final Component... pages) {
70-
return book(title, author, List.of(pages));
71+
return book(title, author, List.of(requireNonNull(pages)));
72+
}
73+
74+
/**
75+
* Creates a book with title and author set to {@link Component#empty()}.
76+
*
77+
* @param pages the collection of pages
78+
* @return a book
79+
* @since 5.1.0
80+
*/
81+
static Book book(final Collection<Component> pages) {
82+
return book(Component.empty(), Component.empty(), pages);
83+
}
84+
85+
/**
86+
* Creates a book with title and author set to {@link Component#empty()}.
87+
*
88+
* @param pages an array of pages
89+
* @return a book
90+
* @since 5.1.0
91+
*/
92+
static Book book(final Component... pages) {
93+
return book(Component.empty(), Component.empty(), pages);
7194
}
7295

7396
/**

api/src/main/java/net/kyori/adventure/inventory/BookImpl.java

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,6 @@
3232
import static java.util.Objects.requireNonNull;
3333

3434
record BookImpl(Component title, Component author, List<Component> pages) implements Book {
35-
BookImpl(final Component title, final Component author, final List<Component> pages) {
36-
this.title = requireNonNull(title, "title");
37-
this.author = requireNonNull(author, "author");
38-
this.pages = List.copyOf(requireNonNull(pages, "pages"));
39-
}
40-
4135
@Override
4236
public Component title() {
4337
return this.title;
@@ -65,7 +59,7 @@ public List<Component> pages() {
6559

6660
@Override
6761
public Book pages(final List<Component> pages) {
68-
return new BookImpl(this.title, this.author, new ArrayList<>(requireNonNull(pages, "pages")));
62+
return new BookImpl(this.title, this.author, List.copyOf(requireNonNull(pages, "pages")));
6963
}
7064

7165
@Override
@@ -111,14 +105,15 @@ public Builder pages(final Collection<Component> pages) {
111105
}
112106

113107
@Override
114-
public Builder pages(final Component ... pages) {
108+
public Builder pages(final Component... pages) {
109+
requireNonNull(pages, "pages");
115110
Collections.addAll(this.pages, pages);
116111
return this;
117112
}
118113

119114
@Override
120115
public Book build() {
121-
return new BookImpl(this.title, this.author, List.copyOf(this.pages));
116+
return new BookImpl(this.title, this.author, this.pages);
122117
}
123118
}
124119
}

api/src/test/java/net/kyori/adventure/inventory/BookTest.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,4 +109,13 @@ void testEquality() {
109109
)
110110
.testEquals();
111111
}
112+
113+
@Test
114+
void testPagesOnly() {
115+
final Book b = Book.book(arrayOfPages(1));
116+
assertEquals(listOfPages(1), b.pages());
117+
assertEquals(Component.empty(), b.title());
118+
assertEquals(Component.empty(), b.author());
119+
}
120+
112121
}

0 commit comments

Comments
 (0)