From 5331e2bec93fa54d839a866fd691920b774d4fcd Mon Sep 17 00:00:00 2001 From: cnr Date: Wed, 25 May 2016 10:08:07 -0500 Subject: [PATCH] Allow PlayerKeyValueRepository Exceptions to propagate --- .../core/common/util/BukkitFuture.java | 15 ++++++ .../database/PlayerKeyValueRepository.java | 52 ++++++++++++------- 2 files changed, 47 insertions(+), 20 deletions(-) diff --git a/Plugins/Mineplex.Core.Common/src/mineplex/core/common/util/BukkitFuture.java b/Plugins/Mineplex.Core.Common/src/mineplex/core/common/util/BukkitFuture.java index e63644ac2..5591e918a 100644 --- a/Plugins/Mineplex.Core.Common/src/mineplex/core/common/util/BukkitFuture.java +++ b/Plugins/Mineplex.Core.Common/src/mineplex/core/common/util/BukkitFuture.java @@ -6,6 +6,7 @@ import org.bukkit.plugin.java.JavaPlugin; import java.util.concurrent.CompletableFuture; import java.util.concurrent.CompletionStage; +import java.util.function.BiConsumer; import java.util.function.Consumer; import java.util.function.Function; import java.util.function.Supplier; @@ -94,6 +95,20 @@ public class BukkitFuture }; } + /** + * Finalize a {@link CompletionStage} by executing code on the + * main thread after its normal or exceptional completion. + * + * @param action the {@link BiConsumer} that will execute + * @return a {@link BiConsumer} to be passed as an argument to + * {@link CompletionStage#whenComplete(BiConsumer)} + * @see CompletableFuture#whenComplete(BiConsumer) + */ + public static BiConsumer complete(BiConsumer action) + { + return (val, throwable) -> runBlocking(() -> action.accept(val, throwable)); + } + /** * Create a {@link CompletionStage} from a supplier executed on the * main thread. diff --git a/Plugins/Mineplex.Core/src/mineplex/core/database/PlayerKeyValueRepository.java b/Plugins/Mineplex.Core/src/mineplex/core/database/PlayerKeyValueRepository.java index e0148bd0a..74efc5086 100644 --- a/Plugins/Mineplex.Core/src/mineplex/core/database/PlayerKeyValueRepository.java +++ b/Plugins/Mineplex.Core/src/mineplex/core/database/PlayerKeyValueRepository.java @@ -117,9 +117,11 @@ public class PlayerKeyValueRepository return _mapper._deserializer.read(set, 1); } return null; - } catch (SQLException ignored) {} - - return null; // yuck + } + catch (SQLException e) + { + throw new RuntimeException(e); + } }); } @@ -146,9 +148,11 @@ public class PlayerKeyValueRepository results.put(set.getString(1), _mapper._deserializer.read(set, 2)); } return results; - } catch (SQLException ignored) {} - - return new HashMap<>(); // yuck + } + catch (SQLException e) + { + throw new RuntimeException(e); + } }); } @@ -174,9 +178,11 @@ public class PlayerKeyValueRepository stmt.executeUpdate(); return true; - } catch (SQLException ignored) {} - - return false; + } + catch (SQLException e) + { + throw new RuntimeException(e); + } }); } @@ -189,7 +195,7 @@ public class PlayerKeyValueRepository * @return a {@link CompletableFuture} whose value indicates * success or failure */ - public CompletableFuture putAll(UUID uuid, Map values) + public CompletableFuture putAll(UUID uuid, Map values) { return CompletableFuture.supplyAsync(() -> { @@ -205,11 +211,13 @@ public class PlayerKeyValueRepository stmt.addBatch(); } stmt.executeBatch(); - return true; + return null; - } catch (SQLException ignored) {} - - return false; + } + catch (SQLException e) + { + throw new RuntimeException(e); + } }); } @@ -233,9 +241,11 @@ public class PlayerKeyValueRepository stmt.executeUpdate(); return true; - } catch (SQLException ignored) {} - - return false; + } + catch (SQLException e) + { + throw new RuntimeException(e); + } }); } @@ -257,9 +267,11 @@ public class PlayerKeyValueRepository stmt.executeUpdate(); return true; - } catch (SQLException ignored) {} - - return false; + } + catch (SQLException e) + { + throw new RuntimeException(e); + } }); }