From bf506a49b257d695858e419c692bef8da40e2f71 Mon Sep 17 00:00:00 2001 From: Florian CUNY Date: Sun, 28 Oct 2018 17:15:13 +0100 Subject: [PATCH] Fixed some code smells --- .../database/json/JSONDatabaseConnector.java | 9 ++++----- .../database/json/JSONDatabaseHandler.java | 14 +++++++------- .../mongodb/MongoDBDatabaseConnector.java | 4 ---- .../database/mysql/MySQLDatabaseConnector.java | 2 -- .../database/yaml/YamlDatabaseConnector.java | 15 ++++++++------- .../database/yaml/YamlDatabaseHandler.java | 8 +++++--- 6 files changed, 24 insertions(+), 28 deletions(-) diff --git a/src/main/java/world/bentobox/bentobox/database/json/JSONDatabaseConnector.java b/src/main/java/world/bentobox/bentobox/database/json/JSONDatabaseConnector.java index aa2fef0ae..2e657e7c3 100644 --- a/src/main/java/world/bentobox/bentobox/database/json/JSONDatabaseConnector.java +++ b/src/main/java/world/bentobox/bentobox/database/json/JSONDatabaseConnector.java @@ -10,29 +10,28 @@ public class JSONDatabaseConnector implements DatabaseConnector { private static final int MAX_LOOPS = 100; private static final String DATABASE_FOLDER_NAME = "database"; - private final BentoBox plugin; + private static final String JSON = ".json"; private final File dataFolder; public JSONDatabaseConnector(BentoBox plugin) { - this.plugin = plugin; dataFolder = new File(plugin.getDataFolder(), DATABASE_FOLDER_NAME); } @Override public String getUniqueId(String tableName) { UUID uuid = UUID.randomUUID(); - File file = new File(dataFolder, tableName + File.separator + uuid.toString() + ".json"); + File file = new File(dataFolder, tableName + File.separator + uuid.toString() + JSON); int limit = 0; while (file.exists() && limit++ < MAX_LOOPS) { uuid = UUID.randomUUID(); - file = new File(dataFolder, tableName + File.separator + uuid.toString() + ".json"); + file = new File(dataFolder, tableName + File.separator + uuid.toString() + JSON); } return uuid.toString(); } @Override public boolean uniqueIdExists(String tableName, String key) { - File file = new File(dataFolder, tableName + File.separator + key + ".json"); + File file = new File(dataFolder, tableName + File.separator + key + JSON); return file.exists(); } diff --git a/src/main/java/world/bentobox/bentobox/database/json/JSONDatabaseHandler.java b/src/main/java/world/bentobox/bentobox/database/json/JSONDatabaseHandler.java index cccbbf98f..f0f2eaa10 100644 --- a/src/main/java/world/bentobox/bentobox/database/json/JSONDatabaseHandler.java +++ b/src/main/java/world/bentobox/bentobox/database/json/JSONDatabaseHandler.java @@ -6,9 +6,7 @@ import world.bentobox.bentobox.database.DatabaseConnector; import java.beans.IntrospectionException; import java.beans.PropertyDescriptor; import java.io.File; -import java.io.FileInputStream; import java.io.FileNotFoundException; -import java.io.FileOutputStream; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; @@ -23,6 +21,8 @@ import java.util.Objects; public class JSONDatabaseHandler extends AbstractJSONDatabaseHandler { + private static final String JSON = ".json"; + /** * Constructor * @@ -52,7 +52,7 @@ public class JSONDatabaseHandler extends AbstractJSONDatabaseHandler { tableFolder.mkdirs(); } // Load each object from the file system, filtered, non-null - for (File file: Objects.requireNonNull(tableFolder.listFiles((dir, name) -> name.toLowerCase(Locale.ENGLISH).endsWith(".json")))) { + for (File file: Objects.requireNonNull(tableFolder.listFiles((dir, name) -> name.toLowerCase(Locale.ENGLISH).endsWith(JSON)))) { try { list.add(getGson().fromJson(new FileReader(file), dataObject)); } catch (FileNotFoundException e) { @@ -68,8 +68,8 @@ public class JSONDatabaseHandler extends AbstractJSONDatabaseHandler { String path = DATABASE_FOLDER_NAME + File.separator + dataObject.getSimpleName(); String fileName = path + File.separator + uniqueId; - if (!fileName.endsWith(".json")) { - fileName = fileName + ".json"; + if (!fileName.endsWith(JSON)) { + fileName = fileName + JSON; } T result = null; @@ -122,8 +122,8 @@ public class JSONDatabaseHandler extends AbstractJSONDatabaseHandler { String fileName = (String) method.invoke(instance); // The filename of the JSON file is the value of uniqueId field plus .json. Sometimes the .json is already appended. - if (!fileName.endsWith(".json")) { - fileName = fileName + ".json"; + if (!fileName.endsWith(JSON)) { + fileName = fileName + JSON; } // Get the database and table folders diff --git a/src/main/java/world/bentobox/bentobox/database/mongodb/MongoDBDatabaseConnector.java b/src/main/java/world/bentobox/bentobox/database/mongodb/MongoDBDatabaseConnector.java index 7c0ae2faf..86f349edd 100644 --- a/src/main/java/world/bentobox/bentobox/database/mongodb/MongoDBDatabaseConnector.java +++ b/src/main/java/world/bentobox/bentobox/database/mongodb/MongoDBDatabaseConnector.java @@ -1,9 +1,5 @@ package world.bentobox.bentobox.database.mongodb; -import java.util.Map; - -import org.bukkit.configuration.file.YamlConfiguration; - import com.mongodb.MongoClient; import com.mongodb.MongoClientOptions; import com.mongodb.MongoCredential; diff --git a/src/main/java/world/bentobox/bentobox/database/mysql/MySQLDatabaseConnector.java b/src/main/java/world/bentobox/bentobox/database/mysql/MySQLDatabaseConnector.java index 904055df3..c6b0b8f2f 100644 --- a/src/main/java/world/bentobox/bentobox/database/mysql/MySQLDatabaseConnector.java +++ b/src/main/java/world/bentobox/bentobox/database/mysql/MySQLDatabaseConnector.java @@ -3,10 +3,8 @@ package world.bentobox.bentobox.database.mysql; import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; -import java.util.Map; import org.bukkit.Bukkit; -import org.bukkit.configuration.file.YamlConfiguration; import world.bentobox.bentobox.database.DatabaseConnectionSettingsImpl; import world.bentobox.bentobox.database.DatabaseConnector; diff --git a/src/main/java/world/bentobox/bentobox/database/yaml/YamlDatabaseConnector.java b/src/main/java/world/bentobox/bentobox/database/yaml/YamlDatabaseConnector.java index 173e67cca..49681bfaf 100644 --- a/src/main/java/world/bentobox/bentobox/database/yaml/YamlDatabaseConnector.java +++ b/src/main/java/world/bentobox/bentobox/database/yaml/YamlDatabaseConnector.java @@ -25,6 +25,7 @@ public class YamlDatabaseConnector implements DatabaseConnector { private static final int MAX_LOOPS = 100; private static final String DATABASE_FOLDER_NAME = "database"; + private static final String YML = ".yml"; private final BentoBox plugin; private final File dataFolder; @@ -45,8 +46,8 @@ public class YamlDatabaseConnector implements DatabaseConnector { } public YamlConfiguration loadYamlFile(String tableName, String fileName) { - if (!fileName.endsWith(".yml")) { - fileName = fileName + ".yml"; + if (!fileName.endsWith(YML)) { + fileName = fileName + YML; } File yamlFile = new File(plugin.getDataFolder(), tableName + File.separator + fileName); @@ -79,8 +80,8 @@ public class YamlDatabaseConnector implements DatabaseConnector { } public void saveYamlFile(YamlConfiguration yamlConfig, String tableName, String fileName, Map commentMap) { - if (!fileName.endsWith(".yml")) { - fileName = fileName + ".yml"; + if (!fileName.endsWith(YML)) { + fileName = fileName + YML; } File tableFolder = new File(plugin.getDataFolder(), tableName); File file = new File(tableFolder, fileName); @@ -158,18 +159,18 @@ public class YamlDatabaseConnector implements DatabaseConnector { @Override public String getUniqueId(String tableName) { UUID uuid = UUID.randomUUID(); - File file = new File(dataFolder, tableName + File.separator + uuid.toString() + ".yml"); + File file = new File(dataFolder, tableName + File.separator + uuid.toString() + YML); int limit = 0; while (file.exists() && limit++ < MAX_LOOPS) { uuid = UUID.randomUUID(); - file = new File(dataFolder, tableName + File.separator + uuid.toString() + ".yml"); + file = new File(dataFolder, tableName + File.separator + uuid.toString() + YML); } return uuid.toString(); } @Override public boolean uniqueIdExists(String tableName, String key) { - File file = new File(dataFolder, tableName + File.separator + key + ".yml"); + File file = new File(dataFolder, tableName + File.separator + key + YML); return file.exists(); } diff --git a/src/main/java/world/bentobox/bentobox/database/yaml/YamlDatabaseHandler.java b/src/main/java/world/bentobox/bentobox/database/yaml/YamlDatabaseHandler.java index 948b479bc..a25965011 100644 --- a/src/main/java/world/bentobox/bentobox/database/yaml/YamlDatabaseHandler.java +++ b/src/main/java/world/bentobox/bentobox/database/yaml/YamlDatabaseHandler.java @@ -49,6 +49,8 @@ import world.bentobox.bentobox.util.Util; public class YamlDatabaseHandler extends AbstractDatabaseHandler { + private static final String YML = ".yml"; + /** * Flag to indicate if this is a config or a pure object database (difference is in comments and annotations) */ @@ -97,7 +99,7 @@ public class YamlDatabaseHandler extends AbstractDatabaseHandler { // In this case, all the objects of a specific type are being loaded. List list = new ArrayList<>(); // Look for any files that end in .yml in the folder - FilenameFilter ymlFilter = (dir, name) -> name.toLowerCase(java.util.Locale.ENGLISH).endsWith(".yml"); + FilenameFilter ymlFilter = (dir, name) -> name.toLowerCase(java.util.Locale.ENGLISH).endsWith(YML); // The path is the simple name of the class String path = dataObject.getSimpleName(); // The storeAt annotation may override the path @@ -528,8 +530,8 @@ public class YamlDatabaseHandler extends AbstractDatabaseHandler { Method method = propertyDescriptor.getReadMethod(); String fileName = (String) method.invoke(instance); // The filename of the YAML file is the value of uniqueId field plus .yml. Sometimes the .yml is already appended. - if (!fileName.endsWith(".yml")) { - fileName = fileName + ".yml"; + if (!fileName.endsWith(YML)) { + fileName = fileName + YML; } // Get the database and table folders File dataFolder = new File(plugin.getDataFolder(), DATABASE_FOLDER_NAME);