Cleaned up code for FlatFileDatabaseHandler

This commit is contained in:
Tastybento 2018-02-19 22:45:53 -08:00
parent 6c4a7d6f97
commit 57cb919ebf

View File

@ -128,12 +128,6 @@ public class FlatFileDatabaseHandler<T> extends AbstractDatabaseHandler<T> {
* @param config - YAML config file * @param config - YAML config file
* *
* @return <T> filled with values * @return <T> filled with values
*/ */
private T createObject(YamlConfiguration config) throws InstantiationException, IllegalAccessException, IntrospectionException, IllegalArgumentException, InvocationTargetException, ClassNotFoundException { private T createObject(YamlConfiguration config) throws InstantiationException, IllegalAccessException, IntrospectionException, IllegalArgumentException, InvocationTargetException, ClassNotFoundException {
T instance = dataObject.newInstance(); T instance = dataObject.newInstance();
@ -145,7 +139,7 @@ public class FlatFileDatabaseHandler<T> extends AbstractDatabaseHandler<T> {
// Get the write method // Get the write method
Method method = propertyDescriptor.getWriteMethod(); Method method = propertyDescriptor.getWriteMethod();
if (DEBUG) { if (DEBUG) {
plugin.getLogger().info("DEBUG: " + field.getName() + ": " + propertyDescriptor.getPropertyType().getTypeName()); plugin.getLogger().info(() -> "DEBUG: " + field.getName() + ": " + propertyDescriptor.getPropertyType().getTypeName());
} }
String storageLocation = field.getName(); String storageLocation = field.getName();
// Check if there is an annotation on the field // Check if there is an annotation on the field
@ -157,7 +151,7 @@ public class FlatFileDatabaseHandler<T> extends AbstractDatabaseHandler<T> {
} }
if (!configEntry.specificTo().equals(GameType.BOTH) && !configEntry.specificTo().equals(Constants.GAMETYPE)) { if (!configEntry.specificTo().equals(GameType.BOTH) && !configEntry.specificTo().equals(Constants.GAMETYPE)) {
if (DEBUG) { if (DEBUG) {
Bukkit.getLogger().info(field.getName() + " not applicable to this game type"); Bukkit.getLogger().info(() -> field.getName() + " not applicable to this game type");
} }
continue; continue;
} }
@ -172,9 +166,9 @@ public class FlatFileDatabaseHandler<T> extends AbstractDatabaseHandler<T> {
Object value = config.get(storageLocation); Object value = config.get(storageLocation);
method.invoke(instance, ((AdapterInterface<?,?>)adapterNotation.value().newInstance()).serialize(value)); method.invoke(instance, ((AdapterInterface<?,?>)adapterNotation.value().newInstance()).serialize(value));
if (DEBUG) { if (DEBUG) {
plugin.getLogger().info("DEBUG: value = " + value); plugin.getLogger().info(() -> "DEBUG: value = " + value);
plugin.getLogger().info("DEBUG: property type = " + propertyDescriptor.getPropertyType()); plugin.getLogger().info(() -> "DEBUG: property type = " + propertyDescriptor.getPropertyType());
plugin.getLogger().info("DEBUG: " + value.getClass()); plugin.getLogger().info(() -> "DEBUG: " + value.getClass());
} }
if (value != null && !value.getClass().equals(MemorySection.class)) { if (value != null && !value.getClass().equals(MemorySection.class)) {
method.invoke(instance, deserialize(value,propertyDescriptor.getPropertyType())); method.invoke(instance, deserialize(value,propertyDescriptor.getPropertyType()));
@ -198,7 +192,7 @@ public class FlatFileDatabaseHandler<T> extends AbstractDatabaseHandler<T> {
Type keyType = collectionTypes.get(0); Type keyType = collectionTypes.get(0);
Type valueType = collectionTypes.get(1); Type valueType = collectionTypes.get(1);
if (DEBUG) { if (DEBUG) {
plugin.getLogger().info("DEBUG: is Map or HashMap<" + keyType.getTypeName() + ", " + valueType.getTypeName() + ">"); plugin.getLogger().info(() -> "DEBUG: is Map or HashMap<" + keyType.getTypeName() + ", " + valueType.getTypeName() + ">");
} }
// TODO: this may not work with all keys. Further serialization may be required. // TODO: this may not work with all keys. Further serialization may be required.
Map<Object,Object> value = new HashMap<>(); Map<Object,Object> value = new HashMap<>();
@ -211,15 +205,15 @@ public class FlatFileDatabaseHandler<T> extends AbstractDatabaseHandler<T> {
// Map values can be null - it is allowed here // Map values can be null - it is allowed here
Object mapValue = deserialize(config.get(storageLocation + "." + key), Class.forName(valueType.getTypeName())); Object mapValue = deserialize(config.get(storageLocation + "." + key), Class.forName(valueType.getTypeName()));
if (DEBUG) { if (DEBUG) {
plugin.getLogger().info("DEBUG: mapKey = " + mapKey + " (" + mapKey.getClass().getCanonicalName() + ")"); plugin.getLogger().info(() -> "DEBUG: mapKey = " + mapKey + " (" + mapKey.getClass().getCanonicalName() + ")");
plugin.getLogger().info("DEBUG: mapValue = " + mapValue + " (" + mapValue.getClass().getCanonicalName() + ")"); plugin.getLogger().info(() -> "DEBUG: mapValue = " + mapValue);
} }
value.put(mapKey, mapValue); value.put(mapKey, mapValue);
} }
method.invoke(instance, value); method.invoke(instance, value);
} else if (Set.class.isAssignableFrom(propertyDescriptor.getPropertyType())) { } else if (Set.class.isAssignableFrom(propertyDescriptor.getPropertyType())) {
if (DEBUG) { if (DEBUG) {
plugin.getLogger().info("DEBUG: is Set " + propertyDescriptor.getReadMethod().getGenericReturnType().getTypeName()); plugin.getLogger().info(() -> "DEBUG: is Set " + propertyDescriptor.getReadMethod().getGenericReturnType().getTypeName());
plugin.getLogger().info("DEBUG: adding a set"); plugin.getLogger().info("DEBUG: adding a set");
} }
// Loop through the collection resultset // Loop through the collection resultset
@ -228,23 +222,20 @@ public class FlatFileDatabaseHandler<T> extends AbstractDatabaseHandler<T> {
// collectionTypes should be only 1 long // collectionTypes should be only 1 long
Type setType = collectionTypes.get(0); Type setType = collectionTypes.get(0);
if (DEBUG) { if (DEBUG) {
plugin.getLogger().info("DEBUG: is HashSet<" + setType.getTypeName() + ">"); plugin.getLogger().info(() -> "DEBUG: is HashSet<" + setType.getTypeName() + ">");
} }
Set<Object> value = new HashSet<>(); Set<Object> value = new HashSet<>();
if (DEBUG) { if (DEBUG) {
plugin.getLogger().info("DEBUG: collection type argument = " + collectionTypes); plugin.getLogger().info(() -> "DEBUG: collection type argument = " + collectionTypes);
plugin.getLogger().info("DEBUG: setType = " + setType.getTypeName()); plugin.getLogger().info(() -> "DEBUG: setType = " + setType.getTypeName());
} }
for (Object listValue: config.getList(storageLocation)) { for (Object listValue: config.getList(storageLocation)) {
//plugin.getLogger().info("DEBUG: collectionResultSet size = " + collectionResultSet.getFetchSize());
value.add(deserialize(listValue,Class.forName(setType.getTypeName()))); value.add(deserialize(listValue,Class.forName(setType.getTypeName())));
} }
// TODO: this may not work with all keys. Further serialization may be required. // TODO: this may not work with all keys. Further serialization may be required.
//Set<Object> value = new HashSet((List<Object>) config.getList(storageLocation));
method.invoke(instance, value); method.invoke(instance, value);
} else if (List.class.isAssignableFrom(propertyDescriptor.getPropertyType())) { } else if (List.class.isAssignableFrom(propertyDescriptor.getPropertyType())) {
//plugin.getLogger().info("DEBUG: is Set " + propertyDescriptor.getReadMethod().getGenericReturnType().getTypeName());
if (DEBUG) { if (DEBUG) {
plugin.getLogger().info("DEBUG: adding a set"); plugin.getLogger().info("DEBUG: adding a set");
} }
@ -254,14 +245,10 @@ public class FlatFileDatabaseHandler<T> extends AbstractDatabaseHandler<T> {
// collectionTypes should be only 1 long // collectionTypes should be only 1 long
Type setType = collectionTypes.get(0); Type setType = collectionTypes.get(0);
List<Object> value = new ArrayList<>(); List<Object> value = new ArrayList<>();
//plugin.getLogger().info("DEBUG: collection type argument = " + collectionTypes);
//plugin.getLogger().info("DEBUG: setType = " + setType.getTypeName());
for (Object listValue: config.getList(storageLocation)) { for (Object listValue: config.getList(storageLocation)) {
//plugin.getLogger().info("DEBUG: collectionResultSet size = " + collectionResultSet.getFetchSize());
value.add(deserialize(listValue,Class.forName(setType.getTypeName()))); value.add(deserialize(listValue,Class.forName(setType.getTypeName())));
} }
// TODO: this may not work with all keys. Further serialization may be required. // TODO: this may not work with all keys. Further serialization may be required.
//Set<Object> value = new HashSet((List<Object>) config.getList(storageLocation));
method.invoke(instance, value); method.invoke(instance, value);
} else { } else {
// Not a collection // Not a collection
@ -270,10 +257,10 @@ public class FlatFileDatabaseHandler<T> extends AbstractDatabaseHandler<T> {
} }
Object value = config.get(storageLocation); Object value = config.get(storageLocation);
if (DEBUG) { if (DEBUG) {
plugin.getLogger().info("DEBUG: name = " + field.getName()); plugin.getLogger().info(() -> "DEBUG: name = " + field.getName());
plugin.getLogger().info("DEBUG: value = " + value); plugin.getLogger().info(() -> "DEBUG: value = " + value);
plugin.getLogger().info("DEBUG: property type = " + propertyDescriptor.getPropertyType()); plugin.getLogger().info(() -> "DEBUG: property type = " + propertyDescriptor.getPropertyType());
plugin.getLogger().info("DEBUG: value class " + value.getClass()); plugin.getLogger().info(() -> "DEBUG: value class " + value.getClass());
} }
if (value != null && !value.getClass().equals(MemorySection.class)) { if (value != null && !value.getClass().equals(MemorySection.class)) {
method.invoke(instance, deserialize(value,propertyDescriptor.getPropertyType())); method.invoke(instance, deserialize(value,propertyDescriptor.getPropertyType()));
@ -335,8 +322,8 @@ public class FlatFileDatabaseHandler<T> extends AbstractDatabaseHandler<T> {
// If there is a config path annotation then do something // If there is a config path annotation then do something
if (configEntry != null) { if (configEntry != null) {
if (DEBUG) { if (DEBUG) {
plugin.getLogger().info("DEBUG: configEntry fould " + configEntry.toString() + " " + configEntry.specificTo()); plugin.getLogger().info(() -> "DEBUG: configEntry fould " + configEntry.toString() + " " + configEntry.specificTo());
plugin.getLogger().info("DEBUG: " + field.getName()); plugin.getLogger().info(() -> "DEBUG: " + field.getName());
} }
if (!configEntry.specificTo().equals(GameType.BOTH) && !configEntry.specificTo().equals(Constants.GAMETYPE)) { if (!configEntry.specificTo().equals(GameType.BOTH) && !configEntry.specificTo().equals(Constants.GAMETYPE)) {
continue fields; continue fields;
@ -357,7 +344,7 @@ public class FlatFileDatabaseHandler<T> extends AbstractDatabaseHandler<T> {
try { try {
config.set(storageLocation, ((AdapterInterface<?,?>)adapterNotation.value().newInstance()).deserialize(value)); config.set(storageLocation, ((AdapterInterface<?,?>)adapterNotation.value().newInstance()).deserialize(value));
} catch (InstantiationException e) { } catch (InstantiationException e) {
plugin.getLogger().severe("Could not instatiate adapter " + adapterNotation.value().getName() + " " + e.getMessage()); plugin.getLogger().severe(() -> "Could not instatiate adapter " + adapterNotation.value().getName() + " " + e.getMessage());
} }
// We are done here // We are done here
continue fields; continue fields;
@ -396,9 +383,6 @@ public class FlatFileDatabaseHandler<T> extends AbstractDatabaseHandler<T> {
} }
} else if (Set.class.isAssignableFrom(propertyDescriptor.getPropertyType())) { } else if (Set.class.isAssignableFrom(propertyDescriptor.getPropertyType())) {
// Sets need to be serialized as string lists // Sets need to be serialized as string lists
if (DEBUG) {
plugin.getLogger().info("DEBUG: Set for " + storageLocation);
}
if (value != null) { if (value != null) {
List<Object> list = new ArrayList<>(); List<Object> list = new ArrayList<>();
for (Object object : (Set<Object>)value) { for (Object object : (Set<Object>)value) {
@ -447,7 +431,7 @@ public class FlatFileDatabaseHandler<T> extends AbstractDatabaseHandler<T> {
@SuppressWarnings({ "unchecked", "rawtypes" }) @SuppressWarnings({ "unchecked", "rawtypes" })
private Object deserialize(Object value, Class<? extends Object> clazz) { private Object deserialize(Object value, Class<? extends Object> clazz) {
if (DEBUG) { if (DEBUG) {
plugin.getLogger().info("DEBUG: deserialize - class is " + clazz.getCanonicalName()); plugin.getLogger().info(() -> "DEBUG: deserialize - class is " + clazz.getCanonicalName());
plugin.getLogger().info("DEBUG: value is " + value); plugin.getLogger().info("DEBUG: value is " + value);
if (value != null) { if (value != null) {
plugin.getLogger().info("DEBUG: value class is " + value.getClass().getCanonicalName()); plugin.getLogger().info("DEBUG: value class is " + value.getClass().getCanonicalName());
@ -491,7 +475,7 @@ public class FlatFileDatabaseHandler<T> extends AbstractDatabaseHandler<T> {
} catch (Exception e) { } catch (Exception e) {
// Maybe this value does not exist? // Maybe this value does not exist?
// TODO return something? // TODO return something?
plugin.getLogger().severe("Could not deserialize enum: " + clazz.getCanonicalName() + " " + value); plugin.getLogger().severe(() -> "Could not deserialize enum: " + clazz.getCanonicalName());
} }
} }
return value; return value;
@ -514,7 +498,7 @@ public class FlatFileDatabaseHandler<T> extends AbstractDatabaseHandler<T> {
try { try {
Files.delete(file.toPath()); Files.delete(file.toPath());
} catch (IOException e) { } catch (IOException e) {
plugin.getLogger().severe("Could not delete yaml database object! " + file.getName() + " - " + e.getMessage()); plugin.getLogger().severe(() -> "Could not delete yaml database object! " + file.getName() + " - " + e.getMessage());
} }
} }
} }