Skip to content

Commit

Permalink
1.1.1: renamed PrefsKey to TypedKey
Browse files Browse the repository at this point in the history
  • Loading branch information
Inderjeet Singh committed Jul 13, 2018
1 parent d4731c0 commit 8494252
Show file tree
Hide file tree
Showing 16 changed files with 97 additions and 97 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ An Android library to access Android SharedPreferences in a TypeSafe manner.
Example:
```
SharedPrefs.init(context, new Gson()); // Initialize one-tine in your Application.onCreate() method
private static final PrefsKey<String> COUNTRY_CODE = new PrefsKey("countryCode", String.class);
private static final TypedKey<String> COUNTRY_CODE = new TypedKey("countryCode", String.class);
SharedPrefs.put(COUNTRY_CODE, "US"); // bind the key COUNTRY_CODE to the value "US"
.... // Anywhere in app
Expand All @@ -24,10 +24,10 @@ repositories {
}
```

In your app build.gradle, add: `compile "com.github.PeelTechnologies:android-typesafe-prefs:1.1.0"`
In your app build.gradle, add: `compile "com.github.PeelTechnologies:android-typesafe-prefs:1.1.1"`

# User Guide
PrefsKey can take arbitrarily complex Java object that Gson can serialize/deserialize. For example, `PrefsKey<Customer>` may represent a class with nested fields for `Address`, name, phone numbers, etc.
TypedKey can take arbitrarily complex Java object that Gson can serialize/deserialize. For example, `TypedKey<Customer>` may represent a class with nested fields for `Address`, name, phone numbers, etc.

`SharedPrefs` class uses the default preferences file for the context to store properties. If you want to use a a different file, specify it during initialization:
```
Expand All @@ -38,7 +38,7 @@ Note that 25 is the size of the in-memory cache for faster access of prefs.
`SharedPrefs` is a static singleton class for `Prefs` with convenience methods named `put` and `get`. For non-static access, use the `Prefs` class directly:
```
Prefs prefs = new Prefs(context, gson, prefsFileName, 25);
PrefsKey<CountryCode> key = new PrefsKey<>("countryCode", CountryCode.class);
TypedKey<CountryCode> key = new TypedKey<>("countryCode", CountryCode.class);
prefs.put(key, CountryCode.US);
CountryCode country = prefs.get(key);
```
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

<groupId>com.github.PeelTechnologies</groupId>
<artifactId>android-typesafe-prefs</artifactId>
<version>1.1.0</version>
<version>1.1.1</version>
<packaging>jar</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
Expand Down
18 changes: 9 additions & 9 deletions src/main/java/com/peel/prefs/Prefs.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ public class Prefs {
private static final Type STRING_SET_TYPE = new TypeToken<Set<String>>() {}.getType();

public interface EventListener {
<T> void onPut(PrefsKey<T> key, T value);
<T> void onRemove(PrefsKey<T> key);
<T> void onPut(TypedKey<T> key, T value);
<T> void onRemove(TypedKey<T> key);
}
private final List<EventListener> listeners = new ArrayList<>();

Expand Down Expand Up @@ -74,15 +74,15 @@ public Context context() {
return context;
}

public <T> T get(PrefsKey<T> key) {
public <T> T get(TypedKey<T> key) {
return getInternal(key.getName(), key.getTypeOfValue());
}

public <T> T get(String key, Class<T> keyClass) {
return getInternal(key, keyClass);
}

public <T> T get(PrefsKey<T> key, T defaultValue) {
public <T> T get(TypedKey<T> key, T defaultValue) {
return contains(key) ? get(key) : defaultValue;
}

Expand Down Expand Up @@ -160,7 +160,7 @@ static String stripJsonQuotesIfPresent(String str) {
return str;
}

public <T> boolean contains(PrefsKey<T> key) {
public <T> boolean contains(TypedKey<T> key) {
String name = key.getName();
return cache.get(name) != null || getPrefs().contains(name);
}
Expand All @@ -169,15 +169,15 @@ public <T> boolean contains(String keyName, Class<T> keyClass) {
return getPrefs().contains(keyName);
}

public <T> void put(PrefsKey<T> key, T value) {
public <T> void put(TypedKey<T> key, T value) {
putInternal(key.getName(), key.getTypeOfValue(), value, key.isCacheableInMemory());
for (EventListener listener : listeners) listener.onPut(key, value);
}

public <T> void put(String keyName, Class<T> keyClass, T value) {
putInternal(keyName, keyClass, value, false);
if (!listeners.isEmpty()) {
PrefsKey<T> key = new PrefsKey<>(keyName, keyClass, false);
TypedKey<T> key = new TypedKey<>(keyName, keyClass, false);
for (EventListener listener : listeners) listener.onPut(key, value);
}
}
Expand Down Expand Up @@ -218,7 +218,7 @@ private <T> void putInternal(String name, Type type, T value, boolean cacheable)
* @param <T> the type of the {@code TypedKey}
* @param key the key that was previously bound as an instance. If the key was not bound previously, nothing is done
*/
public <T> void remove(PrefsKey<T> key) {
public <T> void remove(TypedKey<T> key) {
String keyName = key.getName();
boolean wasPresent = cache.get(keyName) != null;
SharedPreferences prefs = getPrefs();
Expand All @@ -243,7 +243,7 @@ public <T> void remove(String keyName, Class<T> keyClass) {
cache.remove(keyName);
prefs.edit().remove(keyName).apply();
if (!listeners.isEmpty()) {
PrefsKey<T> key = new PrefsKey<>(keyName, keyClass, false);
TypedKey<T> key = new TypedKey<>(keyName, keyClass, false);
for (EventListener listener : listeners) listener.onRemove(key);
}
}
Expand Down
10 changes: 5 additions & 5 deletions src/main/java/com/peel/prefs/SharedPrefs.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,39 +53,39 @@ public static Context context() {
return prefs.context();
}

public static <T> T get(PrefsKey<T> key) {
public static <T> T get(TypedKey<T> key) {
return prefs.get(key);
}

public static <T> T get(String keyName, Class<T> keyClass) {
return prefs.get(keyName, keyClass);
}

public static <T> T get(PrefsKey<T> key, T defaultValue) {
public static <T> T get(TypedKey<T> key, T defaultValue) {
return prefs.get(key, defaultValue);
}

public static <T> T get(String keyName, Class<T> keyClass, T defaultValue) {
return prefs.get(keyName, keyClass, defaultValue);
}

public static <T> boolean contains(PrefsKey<T> key) {
public static <T> boolean contains(TypedKey<T> key) {
return prefs.contains(key);
}

public static <T> boolean contains(String keyName, Class<T> keyClass) {
return prefs.contains(keyName, keyClass);
}

public static <T> void put(PrefsKey<T> key, T value) {
public static <T> void put(TypedKey<T> key, T value) {
prefs.put(key, value);
}

public static <T> void put(String keyName, Class<T> keyClass, T value) {
prefs.put(keyName, keyClass, value);
}

public static <T> void remove(PrefsKey<T> key) {
public static <T> void remove(TypedKey<T> key) {
prefs.remove(key);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,13 @@
import com.google.gson.reflect.TypeToken;

/**
* A typed key
* A typed key with a name and a bound class/type
*
* @param <T> Intended type of the content for the key
*
* @author Inderjeet Singh
*/
public class PrefsKey<T> {
public class TypedKey<T> {

private final String name;
private final Type type;
Expand All @@ -37,15 +37,15 @@ public class PrefsKey<T> {
* @param name Ensure that this name is Unique.
* @param clazz the type of this key
*/
public PrefsKey(String name, Class<T> clazz) {
public TypedKey(String name, Class<T> clazz) {
this(name, clazz, true);
}

public PrefsKey(String name, Class<T> clazz, String... tags) {
public TypedKey(String name, Class<T> clazz, String... tags) {
this(name, clazz, true, tags);
}

public PrefsKey(String name, Class<T> clazz, boolean cacheableInMemory, String... tags) {
public TypedKey(String name, Class<T> clazz, boolean cacheableInMemory, String... tags) {
this.name = name;
this.type = clazz;
this.cacheableInMemory = cacheableInMemory;
Expand All @@ -56,11 +56,11 @@ public PrefsKey(String name, Class<T> clazz, boolean cacheableInMemory, String..
* @param name Ensure that this name is unique across the preference file
* @param type the type for this key
*/
public PrefsKey(String name, TypeToken<T> type) {
public TypedKey(String name, TypeToken<T> type) {
this(name, type, true);
}

public PrefsKey(String name, TypeToken<T> type, String... tags) {
public TypedKey(String name, TypeToken<T> type, String... tags) {
this(name, type, true, tags);
}

Expand All @@ -70,7 +70,7 @@ public PrefsKey(String name, TypeToken<T> type, String... tags) {
* @param cacheableInMemory Whether this key can be stored in an in-memory cache
* for faster access. If false, the key is loaded form disk on every access
*/
public PrefsKey(String name, TypeToken<T> type, boolean cacheableInMemory, String... tags) {
public TypedKey(String name, TypeToken<T> type, boolean cacheableInMemory, String... tags) {
this.name = name;
this.type = type.getType();
this.cacheableInMemory = cacheableInMemory;
Expand Down Expand Up @@ -112,7 +112,7 @@ public boolean equals(Object obj) {
if (obj == null) {
return false;
}
PrefsKey<?> other = (PrefsKey<?>) obj;
TypedKey<?> other = (TypedKey<?>) obj;
return name.equals(other.name);
}

Expand Down
18 changes: 9 additions & 9 deletions src/test/java/com/peel/prefs/BooleanTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,47 +56,47 @@ public void setUp() {

@Test
public void primitiveStoredAsNumber() {
prefs.put(new PrefsKey<>("key", boolean.class), true);
prefs.put(new TypedKey<>("key", boolean.class), true);
assertTrue(persistPrefs.getBoolean("key", false));
}

@Test
public void primitiveWrapperStoredAsNumber() {
prefs.put(new PrefsKey<>("key", Boolean.class), true);
prefs.put(new TypedKey<>("key", Boolean.class), true);
assertTrue(persistPrefs.getBoolean("key", false));
}

@Test
public void restoreToPrimitive() {
persistPrefs.edit().putBoolean("key", true).apply();
assertTrue(prefs.get(new PrefsKey<>("key", boolean.class)));
assertTrue(prefs.get(new TypedKey<>("key", boolean.class)));
}

@Test
public void restoreToPrimitiveWrapper() {
persistPrefs.edit().putBoolean("key", Boolean.TRUE).apply();
assertTrue(prefs.get(new PrefsKey<>("key", Boolean.class)));
assertTrue(prefs.get(new TypedKey<>("key", Boolean.class)));

persistPrefs.edit().putBoolean("key", false).apply();
assertFalse(prefs.get(new PrefsKey<>("key", Boolean.class)));
assertFalse(prefs.get(new TypedKey<>("key", Boolean.class)));
}

@Test
public void restoreStringToPrimitive() {
persistPrefs.edit().putString("key", "true").apply();
assertTrue(prefs.get(new PrefsKey<>("key", boolean.class)));
assertTrue(prefs.get(new TypedKey<>("key", boolean.class)));
}

@Test
public void restoreStringToPrimitiveWrapper() {
persistPrefs.edit().putString("key", "true").apply();
assertTrue(prefs.get(new PrefsKey<>("key", Boolean.class)));
assertTrue(prefs.get(new TypedKey<>("key", Boolean.class)));
}

@Test
public void restoreFromJson() {
persistPrefs.edit().putString("key", "\"true\"").apply();
assertTrue(prefs.get(new PrefsKey<>("key", Boolean.class)));
assertTrue(prefs.get(new PrefsKey<>("key", boolean.class)));
assertTrue(prefs.get(new TypedKey<>("key", Boolean.class)));
assertTrue(prefs.get(new TypedKey<>("key", boolean.class)));
}
}
16 changes: 8 additions & 8 deletions src/test/java/com/peel/prefs/ByteTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,44 +55,44 @@ public void setUp() {

@Test
public void primitiveStoredAsNumber() {
prefs.put(new PrefsKey<>("key", byte.class), (byte)1);
prefs.put(new TypedKey<>("key", byte.class), (byte)1);
assertEquals(1, persistPrefs.getInt("key", 0));
}

@Test
public void primitiveWrapperStoredAsNumber() {
prefs.put(new PrefsKey<>("key", Byte.class), new Byte((byte)1));
prefs.put(new TypedKey<>("key", Byte.class), new Byte((byte)1));
assertEquals(1, persistPrefs.getInt("key", 0));
}

@Test
public void restoreToPrimitive() {
persistPrefs.edit().putInt("key", 1).apply();
assertEquals(1, (byte) prefs.get(new PrefsKey<>("key", byte.class)));
assertEquals(1, (byte) prefs.get(new TypedKey<>("key", byte.class)));
}

@Test
public void restoreToPrimitiveWrapper() {
persistPrefs.edit().putInt("key", 1).apply();
assertEquals(1, (byte) prefs.get(new PrefsKey<>("key", Byte.class)));
assertEquals(1, (byte) prefs.get(new TypedKey<>("key", Byte.class)));
}

@Test
public void restoreStringToPrimitive() {
persistPrefs.edit().putString("key", "1").apply();
assertEquals(1, (byte) prefs.get(new PrefsKey<>("key", byte.class)));
assertEquals(1, (byte) prefs.get(new TypedKey<>("key", byte.class)));
}

@Test
public void restoreStringToPrimitiveWrapper() {
persistPrefs.edit().putString("key", "1").apply();
assertEquals(1, (byte) prefs.get(new PrefsKey<>("key", Byte.class)));
assertEquals(1, (byte) prefs.get(new TypedKey<>("key", Byte.class)));
}

@Test
public void restoreFromJson() {
persistPrefs.edit().putString("key", "\"1\"").apply();
assertEquals(1, (byte) prefs.get(new PrefsKey<>("key", Byte.class)));
assertEquals(1, (byte) prefs.get(new PrefsKey<>("key", byte.class)));
assertEquals(1, (byte) prefs.get(new TypedKey<>("key", Byte.class)));
assertEquals(1, (byte) prefs.get(new TypedKey<>("key", byte.class)));
}
}
16 changes: 8 additions & 8 deletions src/test/java/com/peel/prefs/DoubleTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,44 +56,44 @@ public void setUp() {

@Test
public void primitiveStoredAsNumber() {
prefs.put(new PrefsKey<>("key", double.class), 1.1d);
prefs.put(new TypedKey<>("key", double.class), 1.1d);
assertEquals(1.1d, (double) persistPrefs.getFloat("key", 0f), TOLERANCE);
}

@Test
public void primitiveWrapperStoredAsNumber() {
prefs.put(new PrefsKey<>("key", Double.class), new Double((double)1.1d));
prefs.put(new TypedKey<>("key", Double.class), new Double((double)1.1d));
assertEquals(1.1d, (double) persistPrefs.getFloat("key", 0), TOLERANCE);
}

@Test
public void restoreToPrimitive() {
persistPrefs.edit().putFloat("key", 1.1f).apply();
assertEquals(1.1d, (double) prefs.get(new PrefsKey<>("key", double.class)), TOLERANCE);
assertEquals(1.1d, (double) prefs.get(new TypedKey<>("key", double.class)), TOLERANCE);
}

@Test
public void restoreToPrimitiveWrapper() {
persistPrefs.edit().putFloat("key", 1.1f).apply();
assertEquals(1.1d, (double) prefs.get(new PrefsKey<>("key", Double.class)), TOLERANCE);
assertEquals(1.1d, (double) prefs.get(new TypedKey<>("key", Double.class)), TOLERANCE);
}

@Test
public void restoreStringToPrimitive() {
persistPrefs.edit().putString("key", "1.1").apply();
assertEquals(1.1d, (double) prefs.get(new PrefsKey<>("key", double.class)), TOLERANCE);
assertEquals(1.1d, (double) prefs.get(new TypedKey<>("key", double.class)), TOLERANCE);
}

@Test
public void restoreStringToPrimitiveWrapper() {
persistPrefs.edit().putString("key", "1.1").apply();
assertEquals(1.1d, (double) prefs.get(new PrefsKey<>("key", Double.class)), TOLERANCE);
assertEquals(1.1d, (double) prefs.get(new TypedKey<>("key", Double.class)), TOLERANCE);
}

@Test
public void restoreFromJson() {
persistPrefs.edit().putString("key", "\"1.1\"").apply();
assertEquals(1.1d, (double) prefs.get(new PrefsKey<>("key", Double.class)), TOLERANCE);
assertEquals(1.1d, (double) prefs.get(new PrefsKey<>("key", double.class)), TOLERANCE);
assertEquals(1.1d, (double) prefs.get(new TypedKey<>("key", Double.class)), TOLERANCE);
assertEquals(1.1d, (double) prefs.get(new TypedKey<>("key", double.class)), TOLERANCE);
}
}
Loading

0 comments on commit 8494252

Please sign in to comment.