Skip to content

Commit

Permalink
Added nullability annotations to entire project
Browse files Browse the repository at this point in the history
Closes aikar#14
  • Loading branch information
NoJokeFNA committed Mar 1, 2022
1 parent 71fe65c commit 5b433e5
Show file tree
Hide file tree
Showing 12 changed files with 146 additions and 129 deletions.
3 changes: 0 additions & 3 deletions .idea/codeStyles/Project.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 3 additions & 4 deletions bukkit/src/main/java/co/aikar/idb/BukkitDB.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
package co.aikar.idb;

import lombok.NonNull;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.server.PluginDisableEvent;
import org.bukkit.plugin.Plugin;
import org.jetbrains.annotations.NotNull;

public class BukkitDB {

public static PooledDatabaseOptions getRecommendedOptions(Plugin plugin, @NonNull String user, @NonNull String pass, @NonNull String db, @NonNull String hostAndPort) {
public static PooledDatabaseOptions getRecommendedOptions(Plugin plugin, @NotNull String user, @NotNull String pass, @NotNull String db, @NotNull String hostAndPort) {
DatabaseOptions options = DatabaseOptions
.builder()
.poolName(plugin.getDescription().getName() + " DB")
Expand All @@ -22,7 +22,7 @@ public static PooledDatabaseOptions getRecommendedOptions(Plugin plugin, @NonNul
return poolOptions;
}

public static Database createHikariDatabase(Plugin plugin, @NonNull String user, @NonNull String pass, @NonNull String db, @NonNull String hostAndPort) {
public static Database createHikariDatabase(Plugin plugin, @NotNull String user, @NotNull String pass, @NotNull String db, @NotNull String hostAndPort) {
return createHikariDatabase(plugin, getRecommendedOptions(plugin, user, pass, db, hostAndPort));
}

Expand All @@ -45,5 +45,4 @@ public void onPluginDisable(PluginDisableEvent event) {
}, plugin);
return db;
}

}
15 changes: 9 additions & 6 deletions core/src/main/java/co/aikar/idb/BaseDatabase.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package co.aikar.idb;

import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import javax.sql.DataSource;
import java.io.Closeable;
import java.io.IOException;
Expand All @@ -21,7 +24,7 @@ public class BaseDatabase implements Database {
private ExecutorService threadPool;
DataSource dataSource;

public BaseDatabase(DatabaseOptions options) {
public BaseDatabase(@NotNull DatabaseOptions options) {
this.options = options;
if (options.driverClassName != null && !options.favorDataSourceOverDriver) {
options.dataSourceClassName = null;
Expand Down Expand Up @@ -49,7 +52,7 @@ public BaseDatabase(DatabaseOptions options) {
this.logger.info("Connecting to Database: " + options.dsn);
}

public void close(long timeout, TimeUnit unit) {
public void close(long timeout, @NotNull TimeUnit unit) {
threadPool.shutdown();
try {
threadPool.awaitTermination(timeout, unit);
Expand All @@ -69,7 +72,7 @@ public void close(long timeout, TimeUnit unit) {


@Override
public synchronized <T> CompletableFuture<T> dispatchAsync(Callable<T> task) {
public synchronized <T> CompletableFuture<T> dispatchAsync(@NotNull Callable<T> task) {
CompletableFuture<T> future = new CompletableFuture<>();
Runnable run = () -> {
try {
Expand All @@ -87,11 +90,10 @@ public synchronized <T> CompletableFuture<T> dispatchAsync(Callable<T> task) {
}

@Override
public DatabaseTiming timings(String name) {
public DatabaseTiming timings(@NotNull String name) {
return timingsProvider.of(options.poolName + " - " + name, sqlTiming);
}


@Override
public DatabaseOptions getOptions() {
return this.options;
Expand All @@ -102,7 +104,8 @@ public Logger getLogger() {
return logger;
}

public Connection getConnection() throws SQLException {
@Override
public @Nullable Connection getConnection() throws SQLException {
return dataSource != null ? dataSource.getConnection() : null;
}
}
61 changes: 35 additions & 26 deletions core/src/main/java/co/aikar/idb/DB.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
package co.aikar.idb;

import org.intellij.lang.annotations.Language;
import org.jetbrains.annotations.NotNull;

import java.sql.SQLException;
import java.util.List;
Expand All @@ -36,14 +37,17 @@

public final class DB {
private static final Pattern NEWLINE = Pattern.compile("\n");
private DB() {}

private DB() {
}

private static Database globalDatabase;

public synchronized static Database getGlobalDatabase() {
return globalDatabase;
}
public synchronized static void setGlobalDatabase(Database database) {

public synchronized static void setGlobalDatabase(@NotNull Database database) {
globalDatabase = database;
}

Expand All @@ -54,7 +58,7 @@ public synchronized static void close() {
close(120, TimeUnit.SECONDS);
}

public synchronized static void close(long timeout, TimeUnit unit) {
public synchronized static void close(long timeout, @NotNull TimeUnit unit) {
if (globalDatabase != null) {
globalDatabase.close(timeout, unit);
globalDatabase = null;
Expand All @@ -69,9 +73,10 @@ public synchronized static void close(long timeout, TimeUnit unit) {
* @param params The parameters to execute the statement with
* @return DbRow of your results (HashMap with template return type)
*/
public static DbRow getFirstRow(@Language("SQL") String query, Object... params) throws SQLException {
public static DbRow getFirstRow(@Language("SQL") @NotNull String query, @NotNull Object... params) throws SQLException {
return globalDatabase.getFirstRow(query, params);
}

/**
* Utility method to execute a query and retrieve the first row, then close statement.
* You should ensure result will only return 1 row for maximum performance.
Expand All @@ -80,7 +85,7 @@ public static DbRow getFirstRow(@Language("SQL") String query, Object... params)
* @param params The parameters to execute the statement with
* @return DbRow of your results (HashMap with template return type)
*/
public static CompletableFuture<DbRow> getFirstRowAsync(@Language("SQL") String query, Object... params) {
public static CompletableFuture<DbRow> getFirstRowAsync(@Language("SQL") @NotNull String query, @NotNull Object... params) {
return globalDatabase.getFirstRowAsync(query, params);
}

Expand All @@ -92,9 +97,10 @@ public static CompletableFuture<DbRow> getFirstRowAsync(@Language("SQL") String
* @param params The parameters to execute the statement with
* @return DbRow of your results (HashMap with template return type)
*/
public static <T> T getFirstColumn(@Language("SQL") String query, Object... params) throws SQLException {
public static <T> T getFirstColumn(@Language("SQL") @NotNull String query, @NotNull Object... params) throws SQLException {
return globalDatabase.getFirstColumn(query, params);
}

/**
* Utility method to execute a query and retrieve the first column of the first row, then close statement.
* You should ensure result will only return 1 row for maximum performance.
Expand All @@ -103,50 +109,51 @@ public static <T> T getFirstColumn(@Language("SQL") String query, Object... para
* @param params The parameters to execute the statement with
* @return DbRow of your results (HashMap with template return type)
*/
public static <T> CompletableFuture<T> getFirstColumnAsync(@Language("SQL") String query, Object... params) {
public static <T> CompletableFuture<T> getFirstColumnAsync(@Language("SQL") @NotNull String query, @NotNull Object... params) {
return globalDatabase.getFirstColumnAsync(query, params);
}

/**
* Utility method to execute a query and retrieve first column of all results, then close statement.
*
* <p>
* Meant for single queries that will not use the statement multiple times.
*/
public static <T> List<T> getFirstColumnResults(@Language("SQL") String query, Object... params) throws SQLException {
public static <T> List<T> getFirstColumnResults(@Language("SQL") @NotNull String query, @NotNull Object... params) throws SQLException {
return globalDatabase.getFirstColumnResults(query, params);
}

/**
* Utility method to execute a query and retrieve first column of all results, then close statement.
*
* <p>
* Meant for single queries that will not use the statement multiple times.
*/
public static <T> CompletableFuture<List<T>> getFirstColumnResultsAsync(@Language("SQL") String query, Object... params) {
public static <T> CompletableFuture<List<T>> getFirstColumnResultsAsync(@Language("SQL") @NotNull String query, @NotNull Object... params) {
return globalDatabase.getFirstColumnResultsAsync(query, params);
}

/**
* Utility method to execute a query and retrieve all results, then close statement.
*
* <p>
* Meant for single queries that will not use the statement multiple times.
*
* @param query The query to run
* @param params The parameters to execute the statement with
* @return List of DbRow of your results (HashMap with template return type)
*/
public static List<DbRow> getResults(@Language("SQL") String query, Object... params) throws SQLException {
public static List<DbRow> getResults(@Language("SQL") @NotNull String query, @NotNull Object... params) throws SQLException {
return globalDatabase.getResults(query, params);
}

/**
* Utility method to execute a query and retrieve all results, then close statement.
*
* <p>
* Meant for single queries that will not use the statement multiple times.
*
* @param query The query to run
* @param params The parameters to execute the statement with
* @return List of DbRow of your results (HashMap with template return type)
*/
public static CompletableFuture<List<DbRow>> getResultsAsync(@Language("SQL") String query, Object... params) {
public static CompletableFuture<List<DbRow>> getResultsAsync(@Language("SQL") @NotNull String query, @NotNull Object... params) {
return globalDatabase.getResultsAsync(query, params);
}

Expand All @@ -158,17 +165,18 @@ public static CompletableFuture<List<DbRow>> getResultsAsync(@Language("SQL") St
* @param params Params to execute the statement with.
* @return Inserted Row Id.
*/
public static Long executeInsert(@Language("SQL") String query, Object... params) throws SQLException {
public static Long executeInsert(@Language("SQL") @NotNull String query, @NotNull Object... params) throws SQLException {
return globalDatabase.executeInsert(query, params);
}

/**
* Utility method for executing an update synchronously, and then close the statement.
*
* @param query Query to run
* @param params Params to execute the statement with.
* @return Number of rows modified.
*/
public static int executeUpdate(@Language("SQL") String query, Object... params) throws SQLException {
public static int executeUpdate(@Language("SQL") @NotNull String query, @NotNull Object... params) throws SQLException {
return globalDatabase.executeUpdate(query, params);
}

Expand All @@ -178,38 +186,39 @@ public static int executeUpdate(@Language("SQL") String query, Object... params)
* @param query Query to run
* @param params Params to execute the update with
*/
public static CompletableFuture<Integer> executeUpdateAsync(@Language("SQL") String query, final Object... params) {
public static CompletableFuture<Integer> executeUpdateAsync(@Language("SQL") String query, @NotNull final Object... params) {
return globalDatabase.executeUpdateAsync(query, params);
}

private synchronized static <T> CompletableFuture<T> dispatchAsync(Callable<T> task) {
private synchronized static <T> CompletableFuture<T> dispatchAsync(@NotNull Callable<T> task) {
return globalDatabase.dispatchAsync(task);
}

public static void createTransactionAsync(TransactionCallback run) {
public static void createTransactionAsync(@NotNull TransactionCallback run) {
globalDatabase.createTransactionAsync(run, null, null);
}

public static void createTransactionAsync(TransactionCallback run, Runnable onSuccess, Runnable onFail) {
public static void createTransactionAsync(@NotNull TransactionCallback run, Runnable onSuccess, Runnable onFail) {
globalDatabase.createTransactionAsync(run, onSuccess, onFail);
}

public static boolean createTransaction(TransactionCallback run) {
public static boolean createTransaction(@NotNull TransactionCallback run) {
return globalDatabase.createTransaction(run);
}

public static void logException(Exception e) {
public static void logException(@NotNull Exception e) {
globalDatabase.logException(e.getMessage(), e);
}
public static void logException(String message, Exception e) {

public static void logException(@NotNull String message, @NotNull Exception e) {
globalDatabase.logException(message, e);
}

public static void fatalError(Exception e) {
public static void fatalError(@NotNull Exception e) {
globalDatabase.fatalError(e);
}

public static void logException(Logger logger, Level logLevel, String message, Exception e) {
public static void logException(@NotNull Logger logger, @NotNull Level logLevel, @NotNull String message, Exception e) {
logger.log(logLevel, message);

if (e != null) {
Expand Down
Loading

0 comments on commit 5b433e5

Please sign in to comment.