Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package me.haydenb.smc;
- import java.sql.Connection;
- import java.sql.SQLException;
- import java.util.function.Consumer;
- import java.util.function.Function;
- import java.util.function.Predicate;
- import java.util.function.Supplier;
- import java.util.logging.Logger;
- import com.zaxxer.hikari.HikariConfig;
- import com.zaxxer.hikari.HikariDataSource;
- /**
- * Self contained database connection and management system, allowing easy transport. Call INSTANCE.get() in order to access database.
- * @author Hayden Belanger
- *
- */
- public class DatabaseManager {
- private static final String URL_PORT = "127.0.0.1:3306";
- private static final String USERNAME = "root";
- private static final String PASSWORD = "password";
- private static final String DATABASE = "serverdata";
- private static final String CLASSNAME = "com.mysql.cj.jdbc.Driver";
- private static final Logger LOGGER = Logger.getLogger("DATABASE");
- public static final PredicateLazy<Database> INSTANCE = PredicateLazy.of((db) -> !db.isClosed(), () -> new Database());
- public static class Database{
- private HikariDataSource hds;
- private Database() {
- LOGGER.info("Initializing connection.");
- HikariConfig hc = new HikariConfig();
- hc.setJdbcUrl("jdbc:mysql://" + URL_PORT + "/" + DATABASE);
- hc.setUsername(USERNAME);
- hc.setPassword(PASSWORD);
- hc.setDriverClassName(CLASSNAME);
- hc.addDataSourceProperty("useSSL", "false");
- hc.setMaximumPoolSize(3);
- this.hds = new HikariDataSource(hc);
- LOGGER.info("Connected to " + DATABASE + ".");
- }
- public void endConnection() {
- hds.close();
- }
- public void execute(Consumer<Connection> callback) {
- try (Connection con = hds.getConnection()){
- callback.accept(con);
- }catch(SQLException e) {
- e.printStackTrace();
- }
- }
- public <T> T execute(Function<Connection, T> callback){
- try (Connection con = hds.getConnection()){
- return callback.apply(con);
- }catch(SQLException e) {
- e.printStackTrace();
- return null;
- }
- }
- public boolean isClosed() {
- return hds.isClosed();
- }
- }
- /**
- * PredicateLazy caches an instance. This cached instance is always returned as long as the predicate (on the stored instance object) is true. If for whatever reason the predicate
- * is false, the PredicateLazy will recache a new value. The first access always caches a new value and does not call the predicate as there is no value stored.
- * @author Hayden Belanger
- */
- public static class PredicateLazy<T> implements Supplier<T>{
- private final Supplier<T> supplier;
- private final Predicate<T> predicate;
- private T instance;
- private PredicateLazy(Predicate<T> predicate, Supplier<T> supplier) {
- this.supplier = supplier;
- this.predicate = predicate;
- this.instance = null;
- }
- /**
- * Call to get the stored value within the PredicateLazy, calculating it if needed. Will always regenerate value if the stored predicate returns false.
- */
- @Override
- public T get() {
- if(instance == null || !predicate.test(instance)) instance = supplier.get();
- return instance;
- }
- /**
- * Simple creation method for PredicateLazy.
- */
- public static <X> PredicateLazy<X> of(Predicate<X> predicate, Supplier<X> supplier){
- return new PredicateLazy<>(predicate, supplier);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement