Skip to content

Instantly share code, notes, and snippets.

@alicommit-malp
Created April 11, 2019 13:49
Show Gist options
  • Save alicommit-malp/4ad752b32871ff11ed4a7a937a357361 to your computer and use it in GitHub Desktop.
Save alicommit-malp/4ad752b32871ff11ed4a7a937a357361 to your computer and use it in GitHub Desktop.
A Generic java class to set and get values from and to the Android's default shared preferences
/**
* A Generic class to set and get values from and to the default shared preferences
* @param <T>
*/
public class PrefManager<T> {
private Context context;
public PrefManager(Context context) {
if(context==null) throw new IllegalArgumentException("Context can not be null");
this.context = context;
}
/**
* Set any type of shared preferences value
* @param key of the shared preferences which the caller is desire to set
* @param value types:
* Boolean ,
* StringSet,
* String,
* Float,
* Long,
* Int
*/
public void set(String key, T value) {
SharedPreferences sharedPreference = PreferenceManager.getDefaultSharedPreferences(context);
SharedPreferences.Editor editor = sharedPreference.edit();
if(TextUtils.isEmpty(key)) throw new IllegalArgumentException("Key argument can not be empty");
if(value==null) throw new IllegalArgumentException("Value argument can not be null");
if (value instanceof Boolean) {
editor.putBoolean(key, (Boolean) value);
}
else if (value instanceof Set) {
editor.putStringSet(key, (Set<String>) value);
}
else if (value instanceof String) {
editor.putString(key, (String) value);
}
else if (value instanceof Float) {
editor.putFloat(key, (Float) value);
}
else if (value instanceof Long) {
editor.putLong(key, (Long) value);
}
else if (value instanceof Integer) {
editor.putInt(key, (Integer) value);
}else{
throw new IllegalArgumentException("Type " + value.getClass().getName() + " is not supported");
}
editor.apply();
}
/**
* Get any type of value from the shared preference
* @param key the key of the shared preference
* @param defaultValue types:
* Boolean ,
* StringSet,
* String,
* Float,
* Long,
* Int
* @return same type of the default value which has been passed in
*/
public T get(String key, T defaultValue) {
SharedPreferences sharedPreference = PreferenceManager.getDefaultSharedPreferences(context);
if(TextUtils.isEmpty(key)) throw new IllegalArgumentException("Key can not be empty");
if(defaultValue==null) throw new IllegalArgumentException("defaultValue can not be null");
if (defaultValue instanceof Boolean) {
Boolean ret = sharedPreference.getBoolean(key, (Boolean) defaultValue);
return (T) ret;
}
else if (defaultValue instanceof Collection) {
Set<String> result = sharedPreference.getStringSet(key, new HashSet<String>());
return (T) result;
}
else if (defaultValue instanceof String) {
String ret = sharedPreference.getString(key, (String) defaultValue);
return (T) ret;
}
else if (defaultValue instanceof Float) {
Float result = sharedPreference.getFloat(key, (Float) defaultValue);
return (T) result;
}
else if (defaultValue instanceof Long) {
Long result = sharedPreference.getLong(key, (Long) defaultValue);
return (T) result;
}
else if (defaultValue instanceof Integer) {
Integer result = sharedPreference.getInt(key, (Integer) defaultValue);
return (T) result;
}else{
throw new IllegalArgumentException("Type " + defaultValue.getClass().getName() + " is not supported");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment