From bd69e854eb6c343e35e8bad116f40a180e815905 Mon Sep 17 00:00:00 2001 From: Olof Larsson Date: Wed, 10 Apr 2013 10:55:03 +0200 Subject: [PATCH] Another file not required. --- .../factions/zcore/util/Persist.java | 172 ------------------ 1 file changed, 172 deletions(-) delete mode 100644 src/com/massivecraft/factions/zcore/util/Persist.java diff --git a/src/com/massivecraft/factions/zcore/util/Persist.java b/src/com/massivecraft/factions/zcore/util/Persist.java deleted file mode 100644 index 115eaa28..00000000 --- a/src/com/massivecraft/factions/zcore/util/Persist.java +++ /dev/null @@ -1,172 +0,0 @@ -package com.massivecraft.factions.zcore.util; - -import java.io.File; -import java.lang.reflect.Type; -import java.util.logging.Level; - -import com.massivecraft.factions.zcore.MPlugin; -import com.massivecraft.mcore.util.DiscUtil; - -// TODO: Give better name and place to differenciate from the entity-orm-ish system in "com.massivecraft.core.persist". - -public class Persist { - - private MPlugin p; - public Persist(MPlugin p) - { - this.p = p; - } - - // ------------------------------------------------------------ // - // GET NAME - What should we call this type of object? - // ------------------------------------------------------------ // - - public static String getName(Class clazz) - { - return clazz.getSimpleName().toLowerCase(); - } - - public static String getName(Object o) - { - return getName(o.getClass()); - } - - public static String getName(Type type) - { - return getName(type.getClass()); - } - - // ------------------------------------------------------------ // - // GET FILE - In which file would we like to store this object? - // ------------------------------------------------------------ // - - public File getFile(String name) - { - return new File(p.getDataFolder(), name+".json"); - } - - public File getFile(Class clazz) - { - return getFile(getName(clazz)); - } - - public File getFile(Object obj) - { - return getFile(getName(obj)); - } - - public File getFile(Type type) - { - return getFile(getName(type)); - } - - - // NICE WRAPPERS - - public T loadOrSaveDefault(T def, Class clazz) - { - return loadOrSaveDefault(def, clazz, getFile(clazz)); - } - - public T loadOrSaveDefault(T def, Class clazz, String name) - { - return loadOrSaveDefault(def, clazz, getFile(name)); - } - - public T loadOrSaveDefault(T def, Class clazz, File file) - { - if ( ! file.exists()) - { - p.log("Creating default: "+file); - this.save(def, file); - return def; - } - - T loaded = this.load(clazz, file); - - if (loaded == null) - { - p.log(Level.WARNING, "Using default as I failed to load: "+file); - - // backup bad file, so user can attempt to recover their changes from it - File backup = new File(file.getPath()+"_bad"); - if (backup.exists()) backup.delete(); - p.log(Level.WARNING, "Backing up copy of bad file to: "+backup); - file.renameTo(backup); - - return def; - } - - return loaded; - } - - // SAVE - - public boolean save(Object instance) - { - return save(instance, getFile(instance)); - } - - public boolean save(Object instance, String name) - { - return save(instance, getFile(name)); - } - - public boolean save(Object instance, File file) - { - return DiscUtil.writeCatch(file, p.gson.toJson(instance)); - } - - // LOAD BY CLASS - - public T load(Class clazz) - { - return load(clazz, getFile(clazz)); - } - - public T load(Class clazz, String name) - { - return load(clazz, getFile(name)); - } - - public T load(Class clazz, File file) - { - String content = DiscUtil.readCatch(file); - if (content == null) - { - return null; - } - - try - { - T instance = p.gson.fromJson(content, clazz); - return instance; - } - catch (Exception ex) - { // output the error message rather than full stack trace; error parsing the file, most likely - p.log(Level.WARNING, ex.getMessage()); - } - - return null; - } - - - // LOAD BY TYPE - @SuppressWarnings("unchecked") - public T load(Type typeOfT, String name) - { - return (T) load(typeOfT, getFile(name)); - } - - @SuppressWarnings("unchecked") - public T load(Type typeOfT, File file) - { - String content = DiscUtil.readCatch(file); - if (content == null) { - return null; - } - - return (T) p.gson.fromJson(content, typeOfT); - } - -}