Advertisement
Haydenman2

Database Manager

Jun 27th, 2022 (edited)
316
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.31 KB | None | 0 0
  1. package me.haydenb.smc;
  2.  
  3. import java.sql.Connection;
  4. import java.sql.SQLException;
  5. import java.util.function.Consumer;
  6. import java.util.function.Function;
  7. import java.util.function.Predicate;
  8. import java.util.function.Supplier;
  9. import java.util.logging.Logger;
  10.  
  11. import com.zaxxer.hikari.HikariConfig;
  12. import com.zaxxer.hikari.HikariDataSource;
  13.  
  14. /**
  15.  * Self contained database connection and management system, allowing easy transport. Call INSTANCE.get() in order to access database.
  16.  * @author Hayden Belanger
  17.  *
  18.  */
  19. public class DatabaseManager {
  20.  
  21.     private static final String URL_PORT = "127.0.0.1:3306";
  22.     private static final String USERNAME = "root";
  23.     private static final String PASSWORD = "password";
  24.     private static final String DATABASE = "serverdata";
  25.     private static final String CLASSNAME = "com.mysql.cj.jdbc.Driver";
  26.     private static final Logger LOGGER = Logger.getLogger("DATABASE");
  27.    
  28.     public static final PredicateLazy<Database> INSTANCE = PredicateLazy.of((db) -> !db.isClosed(), () -> new Database());
  29.    
  30.     public static class Database{
  31.        
  32.         private HikariDataSource hds;
  33.        
  34.         private Database() {
  35.             LOGGER.info("Initializing connection.");
  36.             HikariConfig hc = new HikariConfig();
  37.             hc.setJdbcUrl("jdbc:mysql://" + URL_PORT + "/" + DATABASE);
  38.             hc.setUsername(USERNAME);
  39.             hc.setPassword(PASSWORD);
  40.             hc.setDriverClassName(CLASSNAME);
  41.             hc.addDataSourceProperty("useSSL", "false");
  42.             hc.setMaximumPoolSize(3);
  43.             this.hds = new HikariDataSource(hc);
  44.             LOGGER.info("Connected to " + DATABASE + ".");
  45.         }
  46.        
  47.         public void endConnection() {
  48.             hds.close();
  49.         }
  50.        
  51.         public void execute(Consumer<Connection> callback) {
  52.             try (Connection con = hds.getConnection()){
  53.                 callback.accept(con);
  54.             }catch(SQLException e) {
  55.                 e.printStackTrace();
  56.             }
  57.         }
  58.        
  59.         public <T> T execute(Function<Connection, T> callback){
  60.             try (Connection con = hds.getConnection()){
  61.                 return callback.apply(con);
  62.             }catch(SQLException e) {
  63.                 e.printStackTrace();
  64.                 return null;
  65.             }
  66.         }
  67.        
  68.         public boolean isClosed() {
  69.             return hds.isClosed();
  70.         }
  71.     }
  72.    
  73.     /**
  74.      * 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
  75.      * 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.
  76.      * @author Hayden Belanger
  77.      */
  78.     public static class PredicateLazy<T> implements Supplier<T>{
  79.  
  80.         private final Supplier<T> supplier;
  81.         private final Predicate<T> predicate;
  82.         private T instance;
  83.  
  84.         private PredicateLazy(Predicate<T> predicate, Supplier<T> supplier) {
  85.             this.supplier = supplier;
  86.             this.predicate = predicate;
  87.             this.instance = null;
  88.         }
  89.        
  90.         /**
  91.          * Call to get the stored value within the PredicateLazy, calculating it if needed. Will always regenerate value if the stored predicate returns false.
  92.          */
  93.         @Override
  94.         public T get() {
  95.             if(instance == null || !predicate.test(instance)) instance = supplier.get();
  96.             return instance;
  97.         }
  98.        
  99.         /**
  100.          * Simple creation method for PredicateLazy.
  101.          */
  102.         public static <X> PredicateLazy<X> of(Predicate<X> predicate, Supplier<X> supplier){
  103.             return new PredicateLazy<>(predicate, supplier);
  104.         }
  105.     }
  106. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement