Skip to content

Commit

Permalink
Merge pull request #38 from fs/add-default-storage-abstraction
Browse files Browse the repository at this point in the history
Add default storage implementation
  • Loading branch information
Ilya Eremin authored Mar 10, 2017
2 parents 9d590fa + 6ae4122 commit 66836d7
Show file tree
Hide file tree
Showing 5 changed files with 209 additions and 40 deletions.
30 changes: 15 additions & 15 deletions .idea/codeStyleSettings.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 0 additions & 25 deletions app/src/main/java/com/flatstack/android/utils/HomeAsUp.java

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package com.flatstack.android.utils.storage;

import android.support.annotation.NonNull;
import android.support.annotation.Nullable;

import java.lang.reflect.Type;
import java.util.List;

/**
* Created by ereminilya on 8/1/17.
*/

public interface IStorage {

@Nullable <T> T get(@NonNull String key, @NonNull Type type);

<T> void put(@NonNull String key, @NonNull T item);

void putString(@NonNull final String key, @NonNull String str);

@Nullable String getString(@NonNull String key);

void putLong(@NonNull String key, long number);

long getLong(@NonNull String key, long defaultValue);

void putInt(@NonNull String key, int number);

int getInt(@NonNull String key, int defaultValue);

void putBoolean(@NonNull String key, boolean value);

boolean getBoolean(@NonNull String key, boolean defaultValue);

void remove(@NonNull String key);

<T> void putCollection(@NonNull String key, @NonNull List<T> items);

@Nullable <T> List<T> getCollection(@NonNull String key, @NonNull Type type);
}
83 changes: 83 additions & 0 deletions app/src/main/java/com/flatstack/android/utils/storage/Storage.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package com.flatstack.android.utils.storage;

import android.content.SharedPreferences;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;

import com.google.gson.Gson;

import java.lang.reflect.Type;
import java.util.List;

/**
* Created by ereminilya on 8/1/17.
*/

public class Storage implements IStorage {

private final SharedPreferences sp;
private final Gson gson;

public Storage(SharedPreferences sp, Gson gson) {
this.sp = sp;
this.gson = gson;
}

@Override
@Nullable public <T> T get(@NonNull String key, @NonNull Type type) {
String json = sp.getString(key, "");
if ("".equals(json)) return null;
else {
return gson.fromJson(json, type);
}
}

@Override public void put(@NonNull String key, @NonNull Object items) {
sp.edit().putString(key, gson.toJson(items)).apply();
}

@Override public void putString(@NonNull String key, @NonNull String str) {
sp.edit().putString(key, str).apply();
}

@Nullable @Override public String getString(@NonNull String key) {
return sp.getString(key, null);
}

@Override public void putLong(@NonNull String key, long number) {
sp.edit().putLong(key, number).apply();
}

@Override public long getLong(@NonNull String key, long defaultValue) {
return sp.getLong(key, defaultValue);
}

@Override public void putInt(@NonNull String key, int number) {
sp.edit().putInt(key, number).apply();
}

@Override public int getInt(@NonNull String key, int defaultValue) {
return sp.getInt(key, defaultValue);
}

@Override public void putBoolean(@NonNull String key, boolean value) {
sp.edit().putBoolean(key, value).apply();
}

@Override public boolean getBoolean(@NonNull String key, boolean defaultValue) {
return sp.getBoolean(key, defaultValue);
}

@Override public void remove(@NonNull String key) {
sp.edit().remove(key).apply();
}

public <T> void putCollection(@NonNull String key, @NonNull List<T> items) {
put(key, items);
}

@Override public <T> List<T> getCollection(@NonNull String key, @NonNull Type type) {
return get(key, type);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package com.flatstack.android.utils.storage;

import android.support.annotation.NonNull;
import android.support.annotation.Nullable;

import java.lang.reflect.Type;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class RuntimeStorage implements IStorage {

private Map<String, Object> map = new HashMap<>();

@Override public <T> void put(@NonNull String key, @NonNull T item) {
map.put(key, item);
}

@Nullable @Override
public <T> T get(@NonNull String key, @NonNull Type clazz) {
return (T) map.get(key);
}

@Override public void putLong(@NonNull String key, long number) {
map.put(key, Long.valueOf(number));
}

@Override public long getLong(@NonNull String key, long defaultValue) {
Object o = map.get(key);
return o != null ? (long) o : defaultValue;
}

@Override public void putInt(@NonNull String key, int number) {
map.put(key, Integer.valueOf(number));
}

@Override public int getInt(@NonNull String key, int defaultValue) {
Object o = map.get(key);
return o != null ? (int) o : defaultValue;
}

@Override public void putBoolean(@NonNull String key, boolean value) {
map.put(key, Boolean.valueOf(value));
}

@Override public boolean getBoolean(@NonNull String key, boolean defaultValue) {
Object o = map.get(key);
return o != null ? (boolean) o : defaultValue;
}

@Override public void putString(@NonNull String key, @NonNull String str) {
map.put(key, str);
}

@Override public String getString(@NonNull String key) {
Object o = map.get(key);
return (o != null) ? (String) o : null;
}

@Override public void remove(@NonNull String key) {
map.remove(key);
}

@Override public <T> void putCollection(@NonNull String key, @NonNull List<T> items) {
map.put(key, items);
}

@Override public <T> List<T> getCollection(@NonNull String key, @NonNull Type type) {
return (List<T>) map.get(key);
}
}

0 comments on commit 66836d7

Please sign in to comment.