-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: switch cache with db implementation
library may now be instantiated with a custom database implementation to fit everyone's needs. support for nameless, temporary books added BREAKING CHANGE: feeding books to listener is no longer necessary. Signed-off-by: Jasper Lutz Severino <jasperlutzseverino@gmail.com>
- Loading branch information
1 parent
c313d80
commit 9d547dd
Showing
8 changed files
with
96 additions
and
105 deletions.
There are no files selected for viewing
58 changes: 33 additions & 25 deletions
58
core/src/main/java/com/lutzseverino/discordbooks/DiscordBooks.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,44 +1,52 @@ | ||
package com.lutzseverino.discordbooks; | ||
|
||
import com.lutzseverino.discordbooks.book.Book; | ||
import com.lutzseverino.discordbooks.cache.BookCache; | ||
import com.lutzseverino.discordbooks.cache.Cache; | ||
import com.lutzseverino.discordbooks.database.BookDB; | ||
import com.lutzseverino.discordbooks.database.impl.MapDB; | ||
import org.jetbrains.annotations.NotNull; | ||
|
||
public class DiscordBooks { | ||
protected final Cache<Book> books = new BookCache(); | ||
private static BookDB namelessDatabase = new MapDB(); | ||
private static BookDB database = new MapDB(); | ||
|
||
/** | ||
* Registers new books to the cache. | ||
* | ||
* @param books the books to add | ||
*/ | ||
public void addBooks(@NotNull Book... books) { | ||
for (Book book : books) | ||
this.books.add(book); | ||
public DiscordBooks() { | ||
} | ||
|
||
/** | ||
* Registers a new book to the cache. | ||
* | ||
* @param book the book to add | ||
*/ | ||
public void addBook(Book book) { | ||
addBooks(book); | ||
public static BookDB getNamelessDatabase() { | ||
return namelessDatabase; | ||
} | ||
|
||
public DiscordBooks setNamelessDatabase(BookDB database) { | ||
namelessDatabase = database; | ||
return this; | ||
} | ||
|
||
public static BookDB getDatabase() { | ||
return database; | ||
} | ||
|
||
public DiscordBooks setDatabase(BookDB database) { | ||
DiscordBooks.database = database; | ||
return this; | ||
} | ||
|
||
/** | ||
* @param name the name of the book | ||
* @return the book with the given name | ||
* @param id the id of the book | ||
* @return the book with the given id, or null if it doesn't exist | ||
*/ | ||
public Book getBook(String name) { | ||
return books.get(name); | ||
public static Book getBook(String id) { | ||
return database.get(id); | ||
} | ||
|
||
/** | ||
* @return the book cache | ||
* Registers books. These will persist | ||
* as long as they exist in your source, | ||
* assuming you add them on every startup. | ||
* | ||
* @param books the books to add | ||
*/ | ||
public Cache<Book> getBooks() { | ||
return books; | ||
public void registerBooks(@NotNull Book... books) { | ||
for (var book : books) | ||
database.set(book.getId(), book); | ||
} | ||
} |
31 changes: 0 additions & 31 deletions
31
core/src/main/java/com/lutzseverino/discordbooks/cache/BookCache.java
This file was deleted.
Oops, something went wrong.
15 changes: 0 additions & 15 deletions
15
core/src/main/java/com/lutzseverino/discordbooks/cache/Cache.java
This file was deleted.
Oops, something went wrong.
9 changes: 9 additions & 0 deletions
9
core/src/main/java/com/lutzseverino/discordbooks/database/BookDB.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package com.lutzseverino.discordbooks.database; | ||
|
||
import com.lutzseverino.discordbooks.book.Book; | ||
|
||
public interface BookDB { | ||
Book get(String key); | ||
|
||
void set(String key, Book value); | ||
} |
19 changes: 19 additions & 0 deletions
19
core/src/main/java/com/lutzseverino/discordbooks/database/impl/MapDB.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package com.lutzseverino.discordbooks.database.impl; | ||
|
||
import com.lutzseverino.discordbooks.book.Book; | ||
import com.lutzseverino.discordbooks.database.BookDB; | ||
|
||
import java.util.HashMap; | ||
|
||
|
||
public class MapDB implements BookDB { | ||
private final java.util.Map<String, Book> map = new HashMap<>(); | ||
|
||
@Override public Book get(String key) { | ||
return map.get(key); | ||
} | ||
|
||
@Override public void set(String key, Book value) { | ||
map.put(key, value); | ||
} | ||
} |
7 changes: 6 additions & 1 deletion
7
core/src/main/java/com/lutzseverino/discordbooks/discord/channel/Receivable.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,13 @@ | ||
package com.lutzseverino.discordbooks.discord.channel; | ||
|
||
import com.lutzseverino.discordbooks.book.Book; | ||
import com.lutzseverino.discordbooks.discord.message.Sendable; | ||
import org.jetbrains.annotations.NotNull; | ||
|
||
public interface Receivable { | ||
Receivable receive(Sendable message); | ||
void receive(Sendable message); | ||
|
||
void receive(Book book); | ||
|
||
void receive(@NotNull Book book, int index); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters