Class JsonSettings

java.lang.Object
com.xpdustry.claj.server.util.JsonSettings
All Implemented Interfaces:
Autosaver.Saveable

public class JsonSettings extends Object implements Autosaver.Saveable
Simple re-implementation of Settings adding choice to store as plain or binary json.
  • Field Details

    • simple

      protected final arc.struct.ObjectMap<String,Object> simple
    • defaults

      protected final arc.struct.ObjectMap<String,Object> defaults
    • values

      protected final arc.struct.OrderedMap<String, arc.util.serialization.JsonValue> values
    • reader

      protected final arc.util.serialization.BaseJsonReader reader
    • builder

      protected final JsonWriterBuilder builder
    • file

      protected final arc.files.Fi file
    • backupFile

      protected final arc.files.Fi backupFile
    • plainJson

      protected final boolean plainJson
    • errorHandler

      protected arc.func.Cons<Throwable> errorHandler
    • json

      protected arc.util.serialization.Json json
    • loaded

      protected boolean loaded
    • modified

      protected boolean modified
    • autosaved

      protected boolean autosaved
    • backuped

      protected boolean backuped
    • compressed

      protected boolean compressed
  • Constructor Details

    • JsonSettings

      public JsonSettings(arc.files.Fi file)
    • JsonSettings

      public JsonSettings(arc.files.Fi file, arc.files.Fi backupFile)
      Enables backups implicitly.
    • JsonSettings

      public JsonSettings(arc.files.Fi file, boolean autosave)
    • JsonSettings

      public JsonSettings(arc.files.Fi file, arc.files.Fi backupFile, boolean autosave)
      Enables backups implicitly.
    • JsonSettings

      public JsonSettings(arc.files.Fi file, boolean plainJson, boolean autosave)
      Prefer compression when using binary json.
    • JsonSettings

      public JsonSettings(arc.files.Fi file, arc.files.Fi backupFile, boolean plainJson, boolean autosave)
      Enables backups implicitly. Prefer compression when using binary json.
    • JsonSettings

      public JsonSettings(arc.files.Fi file, boolean plainJson, boolean autosave, boolean backuped)
      Prefer compression when using binary json.
    • JsonSettings

      public JsonSettings(arc.files.Fi file, boolean plainJson, boolean autosave, boolean backuped, boolean compressed)
    • JsonSettings

      public JsonSettings(arc.files.Fi file, arc.files.Fi backupFile, boolean plainJson, boolean autosave, boolean backuped, boolean compressed)
      Container to store settings as json
      Parameters:
      file - the file to write/read
      backupFile - the backup file to use in case of corruption.
      plainJson - whether settings are stored in plain or binary json.
      autosave - to take advantage of regularly save.
  • Method Details

    • getJson

      public arc.util.serialization.Json getJson()
    • addSerializer

      public <T> void addSerializer(Class<T> type, arc.util.serialization.Json.Serializer<T> serializer)
    • isPlainJson

      public boolean isPlainJson()
    • setAutosaved

      public void setAutosaved(boolean autosave)
    • isAutosaved

      public boolean isAutosaved()
    • setBackuped

      public void setBackuped(boolean backuped)
    • isBackuped

      public boolean isBackuped()
    • setCompressed

      public void setCompressed(boolean compressed)
    • isCompressed

      public boolean isCompressed()
    • setErrorHandler

      public void setErrorHandler(arc.func.Cons<Throwable> handler)
      Sets the error handler function.
      This function gets called when save() or load() fails.
      This can occur most often on browsers, where extensions can block writing to local storage.
    • modified

      public boolean modified()
      Specified by:
      modified in interface Autosaver.Saveable
    • loaded

      public boolean loaded()
    • load

      public void load()
      Loads all values.
    • save

      public void save()
      Saves all values.
      Specified by:
      save in interface Autosaver.Saveable
    • forceSave

      public void forceSave()
      Specified by:
      forceSave in interface Autosaver.Saveable
    • loadValues

      public void loadValues(arc.files.Fi file)
    • saveValues

      public void saveValues(arc.files.Fi file)
    • exists

      public boolean exists()
      Returns:
      whether the file exists or not.
    • backupExists

      public boolean backupExists()
      Returns:
      whether the backup file exists or not.
    • name

      public String name()
      Description copied from interface: Autosaver.Saveable
      Used for logging.
      Specified by:
      name in interface Autosaver.Saveable
    • file

      public arc.files.Fi file()
      Returns:
      the file used for writing settings to.
    • backupFile

      public arc.files.Fi backupFile()
      Returns:
      the file used for recovering settings.
    • clear

      public void clear()
      Clears all preference values.
    • keys

      public Iterable<String> keys()
    • size

      public int size()
    • isKnownType

      protected boolean isKnownType(Class<?> known)
    • has

      public boolean has(String name)
    • remove

      public void remove(String name)
    • defaults

      public void defaults(Object... objects)
      Set up a list of defaults values.
      Format: name1, default1, name2, default2, etc
    • setDefault

      public void setDefault(String name, Object value)
    • getDefault

      public Object getDefault(String name)
    • putAll

      public void putAll(arc.struct.ObjectMap<String,Object> map)
    • put

      public void put(String name, Object value)
    • put

      public <E> void put(String name, Class<E> elementType, Object value)
    • put

      public <K,E> void put(String name, Class<E> elementType, Class<K> keyType, Object value)
    • get

      public <T> T get(String name, Class<T> type, arc.func.Prov<T> def)
    • get

      public <T,E> T get(String name, Class<T> type, Class<E> elementType, arc.func.Prov<T> def)
    • get

      public <T,K,E> T get(String name, Class<T> type, Class<E> elementType, Class<K> keyType, arc.func.Prov<T> def)
    • get

      protected <T,K,E> T get(String name, Class<T> type)
    • get

      protected <T,K,E> T get(String name, Class<T> type, Class<E> elementType, Class<K> keyType)
    • getOrPut

      public <T> T getOrPut(String name, Class<T> type, arc.func.Prov<T> def)
    • getOrPut

      public <T,E> T getOrPut(String name, Class<T> type, Class<E> elementType, arc.func.Prov<T> def)
    • getOrPut

      public <T,K,E> T getOrPut(String name, Class<T> type, Class<E> elementType, Class<K> keyType, arc.func.Prov<T> def)
      Put and return def if the name key is not found
    • getOrPut

      public <T,K,E> T getOrPut(String name, Class<T> type, T def)
      Put and return def if the name key is not found
    • getFloat

      public float getFloat(String name, float def)
    • getDouble

      public double getDouble(String name, double def)
    • getInt

      public int getInt(String name, int def)
    • getLong

      public long getLong(String name, long def)
    • getBool

      public boolean getBool(String name, boolean def)
    • getString

      public String getString(String name, String def)
    • getFloat

      public float getFloat(String name)
    • getDouble

      public double getDouble(String name)
    • getInt

      public int getInt(String name)
    • getLong

      public long getLong(String name)
    • getBool

      public boolean getBool(String name)
    • getBoolOnce

      public void getBoolOnce(String name, Runnable run)
      Runs the specified code once, and never again.
    • getBoolOnce

      public boolean getBoolOnce(String name)
      Returns false once, and never again.
    • getString

      public String getString(String name)
      Returns:
      null if not found