From a2224b326d19d80251351565179d6e5a320c1408 Mon Sep 17 00:00:00 2001 From: Fabian Schlenz Date: Wed, 22 Feb 2017 06:53:51 +0100 Subject: [PATCH] Database is now a singleton. --- .../java/de/fabianonline/telegram_backup/Database.java | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/main/java/de/fabianonline/telegram_backup/Database.java b/src/main/java/de/fabianonline/telegram_backup/Database.java index 9641ffa..9b945b6 100644 --- a/src/main/java/de/fabianonline/telegram_backup/Database.java +++ b/src/main/java/de/fabianonline/telegram_backup/Database.java @@ -53,6 +53,7 @@ public class Database { public UserManager user_manager; public TelegramClient client; private final static Logger logger = LoggerFactory.getLogger(Database.class); + private static Database instance = null; public Database(UserManager user_manager, TelegramClient client) { this(user_manager, client, true); @@ -80,6 +81,7 @@ public class Database { } this.init(update_db); + instance = this; System.out.println("Database is ready."); } @@ -89,6 +91,11 @@ public class Database { updates.doUpdates(); } + public static Database getInstance() { + if (instance == null) throw new RuntimeException("Database is not initialized but getInstance() was called."); + return instance; + } + public void backupDatabase(int currentVersion) { String filename = String.format(Config.FILE_NAME_DB_BACKUP, currentVersion); System.out.println(" Creating a backup of your database as " + filename);