Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Key-Value store interface and shared preferences implementation example plus memory (mock) implemetnation
public interface KeyStoreDataSource {
String getString(String key, String defaultValue);
void putString(String key, String value);
int getInt(String key, int defaultValue);
void putInt(String key, int value);
long getLong(String key, long defaultValue);
void putLong(String key, long value);
float getFloat(String key, float defaultValue);
void putFloat(String key, float value);
boolean getBoolean(String key, boolean defaultValue);
void putBoolean(String key, boolean value);
void clear();
Map<String, ?> getAll();
}
public class KeyStoreDataSourceImpl implements KeyStoreDataSource {
private static KeyStoreDataSourceImpl sKeyStoreDataSourceImpl;
private String mSharedPrefsName;
private SharedPreferences mSharedPrefs;
public synchronized static KeyStoreDataSourceImpl getInstance(Context context){
if (sKeyStoreDataSourceImpl == null){
sKeyStoreDataSourceImpl = new KeyStoreDataSourceImpl(context, "sharedPreferenceName");
}
return sKeyStoreDataSourceImpl;
}
private KeyStoreDataSourceImpl(Context context, String sharedPreferencesName) {
mSharedPrefsName = sharedPreferencesName;
loadPreferences(context);
}
private void loadPreferences(Context context) {
mSharedPrefs = context.getSharedPreferences(mSharedPrefsName, Context.MODE_PRIVATE);
}
@Override
public String getString(String key, String defaultValue) {
return mSharedPrefs.getString(key, defaultValue);
}
@Override
public void putString(String key, String value) {
SharedPreferences.Editor editor = mSharedPrefs.edit();
editor.putString(key, value);
editor.apply();
}
@Override
public int getInt(String key, int defaultValue) {
return mSharedPrefs.getInt(key, defaultValue);
}
@Override
public void putInt(String key, int value) {
SharedPreferences.Editor editor = mSharedPrefs.edit();
editor.putInt(key, value);
editor.apply();
}
@Override
public long getLong(String key, long defaultValue) {
return mSharedPrefs.getLong(key, defaultValue);
}
@Override
public void putLong(String key, long value) {
SharedPreferences.Editor editor = mSharedPrefs.edit();
editor.putLong(key, value);
editor.apply();
}
@Override
public float getFloat(String key, float defaultValue) {
return mSharedPrefs.getFloat(key, defaultValue);
}
@Override
public void putFloat(String key, float value) {
SharedPreferences.Editor editor = mSharedPrefs.edit();
editor.putFloat(key, value);
editor.apply();
}
@Override
public boolean getBoolean(String key, boolean defaultValue) {
return mSharedPrefs.getBoolean(key, defaultValue);
}
@Override
public void putBoolean(String key, boolean value) {
SharedPreferences.Editor editor = mSharedPrefs.edit();
editor.putBoolean(key, value);
editor.apply();
}
@Override
public void clear() {
mSharedPrefs.edit().clear().apply();
}
@Override
public Map<String, ?> getAll() {
return mSharedPrefs.getAll();
}
}
public class MockKeyStore implements KeyStoreDataSource {
private Map<String, Object> store = new LinkedHashMap<>();
@Override
public String getString(String key, String defaultValue) {
Object value = store.get(key);
if (value == null){
return defaultValue;
}
return (String) value;
}
@Override
public void putString(String key, String value) {
store.put(key, value);
}
@Override
public int getInt(String key, int defaultValue) {
Object value = store.get(key);
if (value == null){
return defaultValue;
}
return (int) value;
}
@Override
public void putInt(String key, int value) {
store.put(key, value);
}
@Override
public long getLong(String key, long defaultValue) {
Object value = store.get(key);
if (value == null){
return defaultValue;
}
return (long) value;
}
@Override
public void putLong(String key, long value) {
store.put(key, value);
}
@Override
public float getFloat(String key, float defaultValue) {
Object value = store.get(key);
if (value == null){
return defaultValue;
}
return (float) value;
}
@Override
public void putFloat(String key, float value) {
store.put(key, value);
}
@Override
public boolean getBoolean(String key, boolean defaultValue) {
Object value = store.get(key);
if (value == null){
return defaultValue;
}
return (boolean) value;
}
@Override
public void putBoolean(String key, boolean value) {
store.put(key, value);
}
@Override
public void clear() {
store.clear();
}
@Override
public Map<String, ?> getAll() {
return store;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.