Skip to content

Instantly share code, notes, and snippets.

@ttencate
Created July 8, 2016 11:24
Show Gist options
  • Save ttencate/23b095fedcf6f4ab1559aba8ff9cbeca to your computer and use it in GitHub Desktop.
Save ttencate/23b095fedcf6f4ab1559aba8ff9cbeca to your computer and use it in GitHub Desktop.
package com.frozenfractal;
import com.badlogic.gdx.Preferences;
import com.badlogic.gdx.utils.Json;
import java.util.Locale;
public class BaseSettings {
protected final Preferences prefs;
protected BaseSettings(Preferences prefs) {
this.prefs = prefs;
}
protected BooleanSetting booleanSetting(String key, boolean defaultValue) {
return new BooleanSetting(key, defaultValue);
}
protected IntSetting intSetting(String key, int defaultValue) {
return new IntSetting(key, defaultValue);
}
protected StringSetting stringSetting(String key, String defaultValue) {
return new StringSetting(key, defaultValue);
}
protected <T extends Enum<T>> EnumSetting<T> enumSetting(Class<T> clazz, String key, T defaultValue) {
return new EnumSetting<T>(clazz, key, defaultValue);
}
protected LocaleSetting localeSetting(String keyPrefix, Locale defaultValue) {
return new LocaleSetting(keyPrefix, defaultValue);
}
protected <T> JsonSetting<T> jsonSetting(Class<T> clazz, Json json, String key, T defaultValue) {
return new JsonSetting<T>(clazz, json, key, defaultValue);
}
protected abstract class Setting<T> {
protected final String key;
protected final T defaultValue;
private T cachedValue;
protected Setting(String key, T defaultValue) {
this.key = key;
this.defaultValue = defaultValue;
}
protected abstract T getPref();
protected abstract void setPref(T value);
public T get() {
if (cachedValue == null) {
cachedValue = getPref();
}
return cachedValue;
}
public void set(T value) {
this.cachedValue = value;
setPref(value);
prefs.flush();
}
}
public class BooleanSetting extends Setting<Boolean> {
private BooleanSetting(String key, boolean defaultValue) {
super(key, defaultValue);
}
@Override
protected Boolean getPref() {
return prefs.getBoolean(key, defaultValue);
}
@Override
protected void setPref(Boolean value) {
prefs.putBoolean(key, value);
}
}
public class IntSetting extends Setting<Integer> {
private IntSetting(String key, int defaultValue) {
super(key, defaultValue);
}
@Override
protected Integer getPref() {
return prefs.getInteger(key, defaultValue);
}
@Override
protected void setPref(Integer value) {
prefs.putInteger(key, value);
}
}
public class StringSetting extends Setting<String> {
private StringSetting(String key, String defaultValue) {
super(key, defaultValue);
}
@Override
protected String getPref() {
return prefs.getString(key, defaultValue);
}
@Override
protected void setPref(String value) {
prefs.putString(key, value);
}
}
public class EnumSetting<T extends Enum<T>> extends Setting<T> {
private final Class<T> clazz;
private EnumSetting(Class<T> clazz, String key, T defaultValue) {
super(key, defaultValue);
this.clazz = clazz;
}
@Override
protected T getPref() {
String s = prefs.getString(key, null);
if (s == null) {
return defaultValue;
}
try {
return Enum.valueOf(clazz, s);
} catch (IllegalArgumentException ex) {
return defaultValue;
}
}
@Override
protected void setPref(T value) {
prefs.putString(key, value.name());
}
}
public class LocaleSetting extends Setting<Locale> {
protected LocaleSetting(String key, Locale defaultValue) {
super(key, defaultValue);
}
@Override
protected Locale getPref() {
return new Locale(
prefs.getString(key + "Language", defaultValue.getLanguage()),
prefs.getString(key + "Country", defaultValue.getCountry()),
prefs.getString(key + "Variant", defaultValue.getVariant()));
}
@Override
protected void setPref(Locale value) {
prefs.putString(key + "Language", value.getLanguage());
prefs.putString(key + "Country", value.getCountry());
prefs.putString(key + "Variant", value.getVariant());
}
}
public class JsonSetting<T> extends Setting<T> {
private final Class<T> clazz;
private final Json json;
protected JsonSetting(Class<T> clazz, Json json, String key, T defaultValue) {
super(key, defaultValue);
this.clazz = clazz;
this.json = json;
}
@Override
protected T getPref() {
String s = prefs.getString(key, null);
if (s == null) {
return defaultValue;
}
try {
return json.fromJson(clazz, s);
} catch (Exception ex) {
return defaultValue;
}
}
@Override
protected void setPref(T value) {
if (value != null) {
prefs.putString(key, json.toJson(value));
} else {
prefs.remove(key);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment