Fixed some code smells

This commit is contained in:
Florian CUNY 2018-10-28 17:15:13 +01:00
parent 37cb96f586
commit bf506a49b2
6 changed files with 24 additions and 28 deletions

View File

@ -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();
}

View File

@ -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<T> extends AbstractJSONDatabaseHandler<T> {
private static final String JSON = ".json";
/**
* Constructor
*
@ -52,7 +52,7 @@ public class JSONDatabaseHandler<T> extends AbstractJSONDatabaseHandler<T> {
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<T> extends AbstractJSONDatabaseHandler<T> {
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<T> extends AbstractJSONDatabaseHandler<T> {
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

View File

@ -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;

View File

@ -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;

View File

@ -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<String, String> 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();
}

View File

@ -49,6 +49,8 @@ import world.bentobox.bentobox.util.Util;
public class YamlDatabaseHandler<T> extends AbstractDatabaseHandler<T> {
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<T> extends AbstractDatabaseHandler<T> {
// In this case, all the objects of a specific type are being loaded.
List<T> 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<T> extends AbstractDatabaseHandler<T> {
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);