Skip to content

Commit 1ab86b8

Browse files
Zabuzardilluminator3
authored andcommitted
Full Javadoc
1 parent cc37f49 commit 1ab86b8

10 files changed

Lines changed: 158 additions & 28 deletions

File tree

application/src/main/java/org/togetherjava/tjbot/Application.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@
1010
import java.nio.file.Path;
1111
import java.sql.SQLException;
1212

13+
/***
14+
* Main class of the application. Use {@link #main(String[])} to start an instance of it.
15+
*/
1316
public enum Application {
1417
;
1518

@@ -21,6 +24,7 @@ public enum Application {
2124
* @param args command line arguments - [the token of the bot, the path to the database]
2225
*/
2326
public static void main(final String[] args) {
27+
// Parse arguments
2428
if (args.length != 2) {
2529
throw new IllegalArgumentException("Expected two arguments but " + args.length
2630
+ " arguments were provided. The first argument must be the token of the bot"
@@ -29,13 +33,20 @@ public static void main(final String[] args) {
2933
String token = args[0];
3034
String databasePath = args[1];
3135

36+
// Start
3237
try {
3338
runBot(token, Path.of(databasePath));
3439
} catch (Exception t) {
3540
logger.error("Unknown error", t);
3641
}
3742
}
3843

44+
/**
45+
* Runs an instance of the bot, connecting to the given token and using the given database.
46+
*
47+
* @param token the Discord Bot token to connect with
48+
* @param databasePath the path to the database to use
49+
*/
3950
public static void runBot(String token, Path databasePath) {
4051
logger.info("Starting bot...");
4152
try {

application/src/main/java/org/togetherjava/tjbot/DatabaseListener.java

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,40 @@
1212

1313
import java.util.Optional;
1414

15+
/**
16+
* Implementation of an example command to illustrate how to use a database.
17+
* <p>
18+
* The implemented commands are {@code !dbput} and {@code !dbget}. They act like some sort of simple
19+
* {@code Map<String, String>}, allowing the user to store and retrieve key-value pairs from the
20+
* database.
21+
* <p>
22+
* For example:
23+
*
24+
* <pre>
25+
* {@code
26+
* !dbput hello Hello World!
27+
* // TJ-Bot: Saved under 'hello'.
28+
* !dbget hello
29+
* // TJ-Bot: Saved message: Hello World!
30+
* }
31+
* </pre>
32+
*/
1533
public final class DatabaseListener extends ListenerAdapter {
34+
/**
35+
* Logger for this class.
36+
*/
1637
private static final Logger logger = LoggerFactory.getLogger(DatabaseListener.class);
1738

39+
/**
40+
* The database instance used by this command.
41+
*/
1842
private final Database database;
1943

44+
/**
45+
* Creates a new command listener, using the given database
46+
*
47+
* @param database the database to store the key-value pairs in
48+
*/
2049
public DatabaseListener(Database database) {
2150
this.database = database;
2251
}
@@ -38,8 +67,18 @@ public void onMessageReceived(MessageReceivedEvent event) {
3867
}
3968
}
4069

70+
/**
71+
* Handler for the {@code !dbput} command.
72+
* <p>
73+
* If the message is in the wrong format, it will respond to the user instead of throwing any
74+
* exceptions.
75+
*
76+
* @param message the message to react to. For example {@code !dbput hello Hello World!}.
77+
* @param event the event the message belongs to, mainly used to respond back to the user
78+
*/
4179
private void handlePutMessage(String message, MessageReceivedEvent event) {
4280
// !dbput hello Hello World!
81+
// Parse message
4382
logger.info("#{}: Received '!dbput' command", event.getResponseNumber());
4483
String[] data = message.split(" ", 3);
4584
if (data.length != 3) {
@@ -51,6 +90,7 @@ private void handlePutMessage(String message, MessageReceivedEvent event) {
5190
String key = data[1];
5291
String value = data[2];
5392

93+
// Save the value in the database
5494
try {
5595
database.writeTransaction(ctx -> {
5696
StorageRecord storageRecord =
@@ -67,8 +107,18 @@ private void handlePutMessage(String message, MessageReceivedEvent event) {
67107
}
68108
}
69109

110+
/**
111+
* Handler for the {@code !dbget} command.
112+
* <p>
113+
* If the message is in the wrong format, it will respond to the user instead of throwing any
114+
* exceptions.
115+
*
116+
* @param message the message to react to. For example {@code !dbget hello}.
117+
* @param event the event the message belongs to, mainly used to respond back to the user
118+
*/
70119
private void handleGetMessage(String message, MessageReceivedEvent event) {
71120
// !dbget hello
121+
// Parse message
72122
logger.info("#{}: Received '!dbget' command", event.getResponseNumber());
73123
String[] data = message.split(" ", 2);
74124
if (data.length != 2) {
@@ -79,6 +129,7 @@ private void handleGetMessage(String message, MessageReceivedEvent event) {
79129
}
80130
String key = data[1];
81131

132+
// Retrieve the value from the database
82133
try {
83134
Optional<StorageRecord> foundValue = database.read(context -> {
84135
return Optional.ofNullable(context.selectFrom(Storage.STORAGE)

application/src/main/java/org/togetherjava/tjbot/PingPongListener.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,32 @@
66
import org.slf4j.Logger;
77
import org.slf4j.LoggerFactory;
88

9+
/**
10+
* Implementation of an example command to illustrate how to use JDA.
11+
* <p>
12+
* The implemented command is {@code !ping}. The bot will respond with a message {@code Pong!}.
13+
* <p>
14+
* For example:
15+
*
16+
* <pre>
17+
* {@code
18+
* !ping
19+
* // TJ-Bot: Pong!
20+
* }
21+
* </pre>
22+
*/
923
public final class PingPongListener extends ListenerAdapter {
24+
/**
25+
* Logger for this class
26+
*/
1027
private static final Logger logger = LoggerFactory.getLogger(PingPongListener.class);
1128

29+
/**
30+
* Handler for the {@code !ping} command. Will ignore any message that is not exactly
31+
* {@code !ping}.
32+
*
33+
* @param event the event the message belongs to
34+
*/
1235
@Override
1336
public void onMessageReceived(MessageReceivedEvent event) {
1437
if (event.getAuthor().isBot()) {

application/src/main/java/org/togetherjava/tjbot/db/Database.java

Lines changed: 41 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -16,23 +16,35 @@
1616
import java.util.concurrent.locks.ReentrantLock;
1717

1818
/**
19-
* The main database class.
19+
* The main database class used by the application.
20+
* <p>
21+
* Create an instance using {@link #Database(String)} and prefer to re-use it. The underlying
22+
* connections are handled automatically by the system.
23+
* <p>
24+
* Instances of this class are thread-safe and can be used to concurrently write to the database.
2025
*/
2126
public final class Database {
2227

28+
/**
29+
* The DSL context for this database.
30+
*/
2331
private final DSLContext dslContext;
32+
/**
33+
* Lock used to implement thread-safety across this class. Any database modifying method should
34+
* use this lock.
35+
*/
2436
private final Lock writeLock = new ReentrantLock();
2537

2638
/**
27-
* Creates a new database.
39+
* Creates an instance of a new database.
2840
*
29-
* @param jdbcUrl the url to the database
41+
* @param jdbcUrl the url to the database in the format expected by JDBC
3042
* @throws SQLException if no connection could be established
3143
*/
3244
public Database(String jdbcUrl) throws SQLException {
3345
SQLiteConfig sqliteConfig = new SQLiteConfig();
3446
sqliteConfig.enforceForeignKeys(true);
35-
// In WAL mode only concurrent writes pose a problem so we synchronize on those
47+
// In WAL mode only concurrent writes pose a problem, so we synchronize on those
3648
sqliteConfig.setJournalMode(SQLiteConfig.JournalMode.WAL);
3749

3850
SQLiteDataSource dataSource = new SQLiteDataSource(sqliteConfig);
@@ -45,10 +57,12 @@ public Database(String jdbcUrl) throws SQLException {
4557
}
4658

4759
/**
48-
* Acquires read access to the database.
60+
* Acquires read-only access to the database.
4961
*
50-
* @return a way to interact with the database in a read only way
51-
* @throws DatabaseException if an error occurs in the passed handler function
62+
* @param action the action to apply to the DSL context, e.g. a query
63+
* @param <T> the type returned by the given action
64+
* @return the result returned by the given action
65+
* @throws DatabaseException if an error occurs in the given action
5266
*/
5367
public <T> T read(
5468
CheckedFunction<? super DSLContext, T, ? extends DataAccessException> action) {
@@ -60,9 +74,10 @@ public <T> T read(
6074
}
6175

6276
/**
63-
* Acquires read access to the database.
77+
* Acquires read-only access to the database.
6478
*
65-
* @throws DatabaseException if an error occurs in the passed handler function
79+
* @param action the action that consumes the DSL context, e.g. a query
80+
* @throws DatabaseException if an error occurs in the given action
6681
*/
6782
public void read(CheckedConsumer<? super DSLContext, ? extends DataAccessException> action) {
6883
read(context -> {
@@ -74,8 +89,10 @@ public void read(CheckedConsumer<? super DSLContext, ? extends DataAccessExcepti
7489
/**
7590
* Acquires read and write access to the database.
7691
*
77-
* @return a way to interact with the database
78-
* @throws DatabaseException if an error occurs in the passed handler function
92+
* @param action the action to apply to the DSL context, e.g. a query
93+
* @param <T> the type returned by the given action
94+
* @return the result returned by the given action
95+
* @throws DatabaseException if an error occurs in the given action
7996
*/
8097
public <T> T write(
8198
CheckedFunction<? super DSLContext, T, ? extends DataAccessException> action) {
@@ -92,7 +109,8 @@ public <T> T write(
92109
/**
93110
* Acquires read and write access to the database.
94111
*
95-
* @throws DatabaseException if an error occurs in the passed handler function
112+
* @param action the action to apply to the DSL context, e.g. a query
113+
* @throws DatabaseException if an error occurs in the given action
96114
*/
97115
public void write(CheckedConsumer<? super DSLContext, ? extends DataAccessException> action) {
98116
write(context -> {
@@ -107,8 +125,8 @@ public void write(CheckedConsumer<? super DSLContext, ? extends DataAccessExcept
107125
* @param handler the handler that is executed within the context of the transaction. The
108126
* handler will be called once and its return value returned from the transaction.
109127
* @param <T> the handler's return type
110-
* @return whatever the handler returned
111-
* @throws DatabaseException if an error occurs in the passed handler function
128+
* @return the object that is returned by the given handler
129+
* @throws DatabaseException if an error occurs in the given handler function
112130
*/
113131
public <T> T readTransaction(
114132
CheckedFunction<? super DSLContext, T, DataAccessException> handler) {
@@ -128,7 +146,7 @@ public <T> T readTransaction(
128146
*
129147
* @param handler the handler that is executed within the context of the transaction. It has no
130148
* return value.
131-
* @throws DatabaseException if an error occurs in the passed handler function
149+
* @throws DatabaseException if an error occurs in the given handler function
132150
*/
133151
public void readTransaction(
134152
CheckedConsumer<? super DSLContext, ? extends DataAccessException> handler) {
@@ -142,10 +160,10 @@ public void readTransaction(
142160
* Acquires a transaction that can read and write to the database.
143161
*
144162
* @param handler the handler that is executed within the context of the transaction. The
145-
* handler will be called once and its return value returned from the transaction.
146-
* @param <T> the handler's return type
147-
* @return whatever the handler returned
148-
* @throws DatabaseException if an error occurs in the passed handler function
163+
* handler will be called once and its return value is returned from the transaction.
164+
* @param <T> the return type of the handler
165+
* @return the object that is returned by the given handler
166+
* @throws DatabaseException if an error occurs in the given handler function
149167
*/
150168
public <T> T writeTransaction(
151169
CheckedFunction<? super DSLContext, T, DataAccessException> handler) {
@@ -168,7 +186,7 @@ public <T> T writeTransaction(
168186
*
169187
* @param handler the handler that is executed within the context of the transaction. It has no
170188
* return value.
171-
* @throws DatabaseException if an error occurs in the passed handler function
189+
* @throws DatabaseException if an error occurs in the given handler function
172190
*/
173191
public void writeTransaction(
174192
CheckedConsumer<? super DSLContext, ? extends DataAccessException> handler) {
@@ -179,7 +197,9 @@ public void writeTransaction(
179197
}
180198

181199
/**
182-
* @return the database dsl context
200+
* Gets the DSL context for this database.
201+
*
202+
* @return the DSL context
183203
*/
184204
private DSLContext getDslContext() {
185205
return dslContext;

application/src/main/java/org/togetherjava/tjbot/db/DatabaseException.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,11 @@ public class DatabaseException extends RuntimeException {
1212
@Serial
1313
private static final long serialVersionUID = -4215197259643585552L;
1414

15+
/**
16+
* Creates a new instance of this exception with the given underlying cause.
17+
*
18+
* @param cause The cause of the exception
19+
*/
1520
public DatabaseException(Exception cause) {
1621
super(cause);
1722
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
/**
2+
* This package contains the database management of the application. See
3+
* {@link org.togetherjava.tjbot.db.Database} to get started.
4+
*/
5+
package org.togetherjava.tjbot.db;
Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
package org.togetherjava.tjbot.util;
22

33
/**
4-
* Represents a function that accepts a single input argument, returns a result and could possibly
4+
* {@link java.util.function.Consumer} extension that can possibly throw an exception.
5+
* <p>
6+
* Represents a function that accepts a single input argument, returns no result and could possibly
57
* throw an exception.
68
*
7-
* @param <T> the type of the input to the function
9+
* @param <T> the type of the input to the consumer
810
* @param <E> the type of the exception
911
*/
1012
@FunctionalInterface
@@ -13,10 +15,10 @@ public interface CheckedConsumer<T, E extends Throwable> {
1315
/**
1416
* Performs this operation on the given argument.
1517
*
16-
* @param t the input argument
18+
* @param input the input argument
1719
* @throws E an exception if any occurs during the execution of the operation
1820
*/
19-
void accept(T t) throws E;
21+
void accept(T input) throws E;
2022

2123
}
2224

application/src/main/java/org/togetherjava/tjbot/util/CheckedFunction.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package org.togetherjava.tjbot.util;
22

33
/**
4+
* {@link java.util.function.Function} extension that can possibly throw an exception.
5+
* <p>
46
* Represents a function that accepts a single input argument, returns a result and could possibly
57
* throw an exception.
68
*
@@ -14,11 +16,11 @@ public interface CheckedFunction<T, R, E extends Throwable> {
1416
/**
1517
* Performs this operation on the given argument.
1618
*
17-
* @param t the input argument
19+
* @param input the input argument
1820
* @return R on successful evaluation
1921
* @throws E an exception if any occurs during the execution of the operation
2022
*/
21-
R accept(T t) throws E;
23+
R accept(T input) throws E;
2224

2325
}
2426

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
/**
2+
* General utility package containing various helper classes and methods used throughout the
3+
* application.
4+
*/
5+
package org.togetherjava.tjbot.util;

0 commit comments

Comments
 (0)