diff --git a/README.md b/README.md index c281293..afba9c2 100644 --- a/README.md +++ b/README.md @@ -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 COUNTRY_CODE = new PrefsKey("countryCode", String.class); +private static final TypedKey COUNTRY_CODE = new TypedKey("countryCode", String.class); SharedPrefs.put(COUNTRY_CODE, "US"); // bind the key COUNTRY_CODE to the value "US" .... // Anywhere in app @@ -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` 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` 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: ``` @@ -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 key = new PrefsKey<>("countryCode", CountryCode.class); +TypedKey key = new TypedKey<>("countryCode", CountryCode.class); prefs.put(key, CountryCode.US); CountryCode country = prefs.get(key); ``` diff --git a/pom.xml b/pom.xml index 1d2ffeb..6eb7316 100644 --- a/pom.xml +++ b/pom.xml @@ -11,7 +11,7 @@ com.github.PeelTechnologies android-typesafe-prefs - 1.1.0 + 1.1.1 jar UTF-8 diff --git a/src/main/java/com/peel/prefs/Prefs.java b/src/main/java/com/peel/prefs/Prefs.java index a3f36a8..d3ddd1e 100644 --- a/src/main/java/com/peel/prefs/Prefs.java +++ b/src/main/java/com/peel/prefs/Prefs.java @@ -40,8 +40,8 @@ public class Prefs { private static final Type STRING_SET_TYPE = new TypeToken>() {}.getType(); public interface EventListener { - void onPut(PrefsKey key, T value); - void onRemove(PrefsKey key); + void onPut(TypedKey key, T value); + void onRemove(TypedKey key); } private final List listeners = new ArrayList<>(); @@ -74,7 +74,7 @@ public Context context() { return context; } - public T get(PrefsKey key) { + public T get(TypedKey key) { return getInternal(key.getName(), key.getTypeOfValue()); } @@ -82,7 +82,7 @@ public T get(String key, Class keyClass) { return getInternal(key, keyClass); } - public T get(PrefsKey key, T defaultValue) { + public T get(TypedKey key, T defaultValue) { return contains(key) ? get(key) : defaultValue; } @@ -160,7 +160,7 @@ static String stripJsonQuotesIfPresent(String str) { return str; } - public boolean contains(PrefsKey key) { + public boolean contains(TypedKey key) { String name = key.getName(); return cache.get(name) != null || getPrefs().contains(name); } @@ -169,7 +169,7 @@ public boolean contains(String keyName, Class keyClass) { return getPrefs().contains(keyName); } - public void put(PrefsKey key, T value) { + public void put(TypedKey key, T value) { putInternal(key.getName(), key.getTypeOfValue(), value, key.isCacheableInMemory()); for (EventListener listener : listeners) listener.onPut(key, value); } @@ -177,7 +177,7 @@ public void put(PrefsKey key, T value) { public void put(String keyName, Class keyClass, T value) { putInternal(keyName, keyClass, value, false); if (!listeners.isEmpty()) { - PrefsKey key = new PrefsKey<>(keyName, keyClass, false); + TypedKey key = new TypedKey<>(keyName, keyClass, false); for (EventListener listener : listeners) listener.onPut(key, value); } } @@ -218,7 +218,7 @@ private void putInternal(String name, Type type, T value, boolean cacheable) * @param 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 void remove(PrefsKey key) { + public void remove(TypedKey key) { String keyName = key.getName(); boolean wasPresent = cache.get(keyName) != null; SharedPreferences prefs = getPrefs(); @@ -243,7 +243,7 @@ public void remove(String keyName, Class keyClass) { cache.remove(keyName); prefs.edit().remove(keyName).apply(); if (!listeners.isEmpty()) { - PrefsKey key = new PrefsKey<>(keyName, keyClass, false); + TypedKey key = new TypedKey<>(keyName, keyClass, false); for (EventListener listener : listeners) listener.onRemove(key); } } diff --git a/src/main/java/com/peel/prefs/SharedPrefs.java b/src/main/java/com/peel/prefs/SharedPrefs.java index b6ed8e1..b6f8f80 100644 --- a/src/main/java/com/peel/prefs/SharedPrefs.java +++ b/src/main/java/com/peel/prefs/SharedPrefs.java @@ -53,7 +53,7 @@ public static Context context() { return prefs.context(); } - public static T get(PrefsKey key) { + public static T get(TypedKey key) { return prefs.get(key); } @@ -61,7 +61,7 @@ public static T get(String keyName, Class keyClass) { return prefs.get(keyName, keyClass); } - public static T get(PrefsKey key, T defaultValue) { + public static T get(TypedKey key, T defaultValue) { return prefs.get(key, defaultValue); } @@ -69,7 +69,7 @@ public static T get(String keyName, Class keyClass, T defaultValue) { return prefs.get(keyName, keyClass, defaultValue); } - public static boolean contains(PrefsKey key) { + public static boolean contains(TypedKey key) { return prefs.contains(key); } @@ -77,7 +77,7 @@ public static boolean contains(String keyName, Class keyClass) { return prefs.contains(keyName, keyClass); } - public static void put(PrefsKey key, T value) { + public static void put(TypedKey key, T value) { prefs.put(key, value); } @@ -85,7 +85,7 @@ public static void put(String keyName, Class keyClass, T value) { prefs.put(keyName, keyClass, value); } - public static void remove(PrefsKey key) { + public static void remove(TypedKey key) { prefs.remove(key); } diff --git a/src/main/java/com/peel/prefs/PrefsKey.java b/src/main/java/com/peel/prefs/TypedKey.java similarity index 85% rename from src/main/java/com/peel/prefs/PrefsKey.java rename to src/main/java/com/peel/prefs/TypedKey.java index 8fe515b..2f08c1b 100644 --- a/src/main/java/com/peel/prefs/PrefsKey.java +++ b/src/main/java/com/peel/prefs/TypedKey.java @@ -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 Intended type of the content for the key * * @author Inderjeet Singh */ -public class PrefsKey { +public class TypedKey { private final String name; private final Type type; @@ -37,15 +37,15 @@ public class PrefsKey { * @param name Ensure that this name is Unique. * @param clazz the type of this key */ - public PrefsKey(String name, Class clazz) { + public TypedKey(String name, Class clazz) { this(name, clazz, true); } - public PrefsKey(String name, Class clazz, String... tags) { + public TypedKey(String name, Class clazz, String... tags) { this(name, clazz, true, tags); } - public PrefsKey(String name, Class clazz, boolean cacheableInMemory, String... tags) { + public TypedKey(String name, Class clazz, boolean cacheableInMemory, String... tags) { this.name = name; this.type = clazz; this.cacheableInMemory = cacheableInMemory; @@ -56,11 +56,11 @@ public PrefsKey(String name, Class 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 type) { + public TypedKey(String name, TypeToken type) { this(name, type, true); } - public PrefsKey(String name, TypeToken type, String... tags) { + public TypedKey(String name, TypeToken type, String... tags) { this(name, type, true, tags); } @@ -70,7 +70,7 @@ public PrefsKey(String name, TypeToken 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 type, boolean cacheableInMemory, String... tags) { + public TypedKey(String name, TypeToken type, boolean cacheableInMemory, String... tags) { this.name = name; this.type = type.getType(); this.cacheableInMemory = cacheableInMemory; @@ -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); } diff --git a/src/test/java/com/peel/prefs/BooleanTest.java b/src/test/java/com/peel/prefs/BooleanTest.java index 31b011c..84cce70 100644 --- a/src/test/java/com/peel/prefs/BooleanTest.java +++ b/src/test/java/com/peel/prefs/BooleanTest.java @@ -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))); } } diff --git a/src/test/java/com/peel/prefs/ByteTest.java b/src/test/java/com/peel/prefs/ByteTest.java index ae3ca83..6a1764e 100644 --- a/src/test/java/com/peel/prefs/ByteTest.java +++ b/src/test/java/com/peel/prefs/ByteTest.java @@ -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))); } } diff --git a/src/test/java/com/peel/prefs/DoubleTest.java b/src/test/java/com/peel/prefs/DoubleTest.java index 345de7c..1c868dd 100644 --- a/src/test/java/com/peel/prefs/DoubleTest.java +++ b/src/test/java/com/peel/prefs/DoubleTest.java @@ -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); } } diff --git a/src/test/java/com/peel/prefs/FloatTest.java b/src/test/java/com/peel/prefs/FloatTest.java index 68d0acd..e6c1049 100644 --- a/src/test/java/com/peel/prefs/FloatTest.java +++ b/src/test/java/com/peel/prefs/FloatTest.java @@ -56,44 +56,44 @@ public void setUp() { @Test public void primitiveStoredAsNumber() { - prefs.put(new PrefsKey<>("key", float.class), 1.1f); + prefs.put(new TypedKey<>("key", float.class), 1.1f); assertEquals(1.1f, (float) persistPrefs.getFloat("key", 0f), TOLERANCE); } @Test public void primitiveWrapperStoredAsNumber() { - prefs.put(new PrefsKey<>("key", Float.class), new Float((float)1.1f)); + prefs.put(new TypedKey<>("key", Float.class), new Float((float)1.1f)); assertEquals(1.1f, persistPrefs.getFloat("key", 0), TOLERANCE); } @Test public void restoreToPrimitive() { persistPrefs.edit().putFloat("key", 1.1f).apply(); - assertEquals(1.1f, (float) prefs.get(new PrefsKey<>("key", float.class)), TOLERANCE); + assertEquals(1.1f, (float) prefs.get(new TypedKey<>("key", float.class)), TOLERANCE); } @Test public void restoreToPrimitiveWrapper() { persistPrefs.edit().putFloat("key", 1.1f).apply(); - assertEquals(1.1f, (float) prefs.get(new PrefsKey<>("key", Float.class)), TOLERANCE); + assertEquals(1.1f, (float) prefs.get(new TypedKey<>("key", Float.class)), TOLERANCE); } @Test public void restoreStringToPrimitive() { persistPrefs.edit().putString("key", "1.1").apply(); - assertEquals(1.1f, (float) prefs.get(new PrefsKey<>("key", float.class)), TOLERANCE); + assertEquals(1.1f, (float) prefs.get(new TypedKey<>("key", float.class)), TOLERANCE); } @Test public void restoreStringToPrimitiveWrapper() { persistPrefs.edit().putString("key", "1.1").apply(); - assertEquals(1.1f, (float) prefs.get(new PrefsKey<>("key", Float.class)), TOLERANCE); + assertEquals(1.1f, (float) prefs.get(new TypedKey<>("key", Float.class)), TOLERANCE); } @Test public void restoreFromJson() { persistPrefs.edit().putString("key", "\"1.1\"").apply(); - assertEquals(1.1f, (float) prefs.get(new PrefsKey<>("key", Float.class)), TOLERANCE); - assertEquals(1.1f, (float) prefs.get(new PrefsKey<>("key", float.class)), TOLERANCE); + assertEquals(1.1f, (float) prefs.get(new TypedKey<>("key", Float.class)), TOLERANCE); + assertEquals(1.1f, (float) prefs.get(new TypedKey<>("key", float.class)), TOLERANCE); } } diff --git a/src/test/java/com/peel/prefs/IntegerTest.java b/src/test/java/com/peel/prefs/IntegerTest.java index a80d5ca..7497821 100644 --- a/src/test/java/com/peel/prefs/IntegerTest.java +++ b/src/test/java/com/peel/prefs/IntegerTest.java @@ -55,44 +55,44 @@ public void setUp() { @Test public void primitiveStoredAsNumber() { - prefs.put(new PrefsKey<>("key", int.class), 1); + prefs.put(new TypedKey<>("key", int.class), 1); assertEquals(1, persistPrefs.getInt("key", 0)); } @Test public void primitiveWrapperStoredAsNumber() { - prefs.put(new PrefsKey<>("key", Integer.class), 1); + prefs.put(new TypedKey<>("key", Integer.class), 1); assertEquals(1, persistPrefs.getInt("key", 0)); } @Test public void restoreToPrimitive() { persistPrefs.edit().putInt("key", 1).apply(); - assertEquals(1, (int) prefs.get(new PrefsKey<>("key", Integer.class))); + assertEquals(1, (int) prefs.get(new TypedKey<>("key", Integer.class))); } @Test public void restoreToPrimitiveWrapper() { persistPrefs.edit().putInt("key", 1).apply(); - assertEquals(1, (int) prefs.get(new PrefsKey<>("key", Integer.class))); + assertEquals(1, (int) prefs.get(new TypedKey<>("key", Integer.class))); } @Test public void restoreStringToPrimitive() { persistPrefs.edit().putString("key", "1").apply(); - assertEquals(1, (int) prefs.get(new PrefsKey<>("key", Integer.class))); + assertEquals(1, (int) prefs.get(new TypedKey<>("key", Integer.class))); } @Test public void restoreStringToPrimitiveWrapper() { persistPrefs.edit().putString("key", "1").apply(); - assertEquals(1, (int) prefs.get(new PrefsKey<>("key", Integer.class))); + assertEquals(1, (int) prefs.get(new TypedKey<>("key", Integer.class))); } @Test public void restoreFromJson() { persistPrefs.edit().putString("key", "\"1\"").apply(); - assertEquals(1, (int) prefs.get(new PrefsKey<>("key", Integer.class))); - assertEquals(1, (int) prefs.get(new PrefsKey<>("key", int.class))); + assertEquals(1, (int) prefs.get(new TypedKey<>("key", Integer.class))); + assertEquals(1, (int) prefs.get(new TypedKey<>("key", int.class))); } } diff --git a/src/test/java/com/peel/prefs/LongTest.java b/src/test/java/com/peel/prefs/LongTest.java index f75dbb8..5e70372 100644 --- a/src/test/java/com/peel/prefs/LongTest.java +++ b/src/test/java/com/peel/prefs/LongTest.java @@ -55,44 +55,44 @@ public void setUp() { @Test public void primitiveStoredAsNumber() { - prefs.put(new PrefsKey<>("key", long.class), 1L); + prefs.put(new TypedKey<>("key", long.class), 1L); assertEquals(1L, persistPrefs.getLong("key", 0L)); } @Test public void primitiveWrapperStoredAsNumber() { - prefs.put(new PrefsKey<>("key", Long.class), 1L); + prefs.put(new TypedKey<>("key", Long.class), 1L); assertEquals(1L, persistPrefs.getLong("key", 0L)); } @Test public void restoreToPrimitive() { persistPrefs.edit().putLong("key", 1L).apply(); - assertEquals(1L, (long) prefs.get(new PrefsKey<>("key", Long.class))); + assertEquals(1L, (long) prefs.get(new TypedKey<>("key", Long.class))); } @Test public void restoreToPrimitiveWrapper() { persistPrefs.edit().putLong("key", 1L).apply(); - assertEquals(1L, (long) prefs.get(new PrefsKey<>("key", Long.class))); + assertEquals(1L, (long) prefs.get(new TypedKey<>("key", Long.class))); } @Test public void restoreStringToPrimitive() { persistPrefs.edit().putString("key", "1").apply(); - assertEquals(1L, (long) prefs.get(new PrefsKey<>("key", Long.class))); + assertEquals(1L, (long) prefs.get(new TypedKey<>("key", Long.class))); } @Test public void restoreStringToPrimitiveWrapper() { persistPrefs.edit().putString("key", "1").apply(); - assertEquals(1L, (long) prefs.get(new PrefsKey<>("key", Long.class))); + assertEquals(1L, (long) prefs.get(new TypedKey<>("key", Long.class))); } @Test public void restoreFromJson() { persistPrefs.edit().putString("key", "\"1\"").apply(); - assertEquals(1L, (long) prefs.get(new PrefsKey<>("key", Long.class))); - assertEquals(1L, (long) prefs.get(new PrefsKey<>("key", long.class))); + assertEquals(1L, (long) prefs.get(new TypedKey<>("key", Long.class))); + assertEquals(1L, (long) prefs.get(new TypedKey<>("key", long.class))); } } diff --git a/src/test/java/com/peel/prefs/PersistentCacheFunctionalTest.java b/src/test/java/com/peel/prefs/PersistentCacheFunctionalTest.java index bada601..e7ca475 100644 --- a/src/test/java/com/peel/prefs/PersistentCacheFunctionalTest.java +++ b/src/test/java/com/peel/prefs/PersistentCacheFunctionalTest.java @@ -51,14 +51,14 @@ public void persistentPropertiesSavedEvenWhenEvictedInCache() { context = AndroidFixtures.createMockContext(); Prefs prefs = new Prefs(context, gson, null, 2); for (int i = 0; i < 20; ++i) { - PrefsKey key = new PrefsKey<>("key" + i, Boolean.class); + TypedKey key = new TypedKey<>("key" + i, Boolean.class); prefs.put(key, true); } // Even though we stored 20 persistent properties, only 2 remain in memory assertEquals(2, prefs.cache.size()); // Even though only 2 are available in cache, all of them are restored from prefs for (int i = 0; i < 20; ++i) { - PrefsKey key = new PrefsKey<>("key" + i, Boolean.class); + TypedKey key = new TypedKey<>("key" + i, Boolean.class); assertTrue(prefs.get(key)); } } diff --git a/src/test/java/com/peel/prefs/PrefsTest.java b/src/test/java/com/peel/prefs/PrefsTest.java index 3ba5f17..c4fdb7e 100644 --- a/src/test/java/com/peel/prefs/PrefsTest.java +++ b/src/test/java/com/peel/prefs/PrefsTest.java @@ -55,7 +55,7 @@ public void setUp() { @Test public void booleanDefaultValueOnGet() { - PrefsKey testKey = new PrefsKey<>("testKey", Boolean.class); + TypedKey testKey = new TypedKey<>("testKey", Boolean.class); prefs.remove(testKey); assertFalse(prefs.get(testKey)); prefs.put(testKey, true); @@ -65,7 +65,7 @@ public void booleanDefaultValueOnGet() { @Test public void testPut() { - PrefsKey key = new PrefsKey<>("userId", String.class); + TypedKey key = new TypedKey<>("userId", String.class); assertNull(prefs.get(key)); prefs.put(key, "19999999999"); assertNotNull(prefs.get(key)); @@ -74,7 +74,7 @@ public void testPut() { @Test public void testClear() { - PrefsKey key = new PrefsKey<>("userId", String.class); + TypedKey key = new TypedKey<>("userId", String.class); prefs.put(key, "a"); assertEquals("a", prefs.get(key)); prefs.clear(); @@ -98,7 +98,7 @@ public void testCustomPrefFile() { SharedPreferences persistPrefs = AndroidFixtures.createMockSharedPreferences(context, null); Mockito.when(context.getSharedPreferences("my_props_file", Context.MODE_PRIVATE)).thenReturn(persistPrefs); - PrefsKey key = new PrefsKey<>("key", String.class); + TypedKey key = new TypedKey<>("key", String.class); assertNull(prefs.get(key)); prefs.put(key, "19999999999"); assertNotNull(prefs.get(key)); diff --git a/src/test/java/com/peel/prefs/ShortTest.java b/src/test/java/com/peel/prefs/ShortTest.java index 7e9fc54..3bbe4b2 100644 --- a/src/test/java/com/peel/prefs/ShortTest.java +++ b/src/test/java/com/peel/prefs/ShortTest.java @@ -55,44 +55,44 @@ public void setUp() { @Test public void primitiveStoredAsNumber() { - prefs.put(new PrefsKey<>("key", short.class), (short)1); + prefs.put(new TypedKey<>("key", short.class), (short)1); assertEquals(1, persistPrefs.getInt("key", 0)); } @Test public void primitiveWrapperStoredAsNumber() { - prefs.put(new PrefsKey<>("key", Short.class), new Short((short)1)); + prefs.put(new TypedKey<>("key", Short.class), new Short((short)1)); assertEquals(1, persistPrefs.getInt("key", 0)); } @Test public void restoreToPrimitive() { persistPrefs.edit().putInt("key", 1).apply(); - assertEquals(1, (short) prefs.get(new PrefsKey<>("key", short.class))); + assertEquals(1, (short) prefs.get(new TypedKey<>("key", short.class))); } @Test public void restoreToPrimitiveWrapper() { persistPrefs.edit().putInt("key", 1).apply(); - assertEquals(1, (short) prefs.get(new PrefsKey<>("key", Short.class))); + assertEquals(1, (short) prefs.get(new TypedKey<>("key", Short.class))); } @Test public void restoreStringToPrimitive() { persistPrefs.edit().putString("key", "1").apply(); - assertEquals(1, (short) prefs.get(new PrefsKey<>("key", short.class))); + assertEquals(1, (short) prefs.get(new TypedKey<>("key", short.class))); } @Test public void restoreStringToPrimitiveWrapper() { persistPrefs.edit().putString("key", "1").apply(); - assertEquals(1, (short) prefs.get(new PrefsKey<>("key", Short.class))); + assertEquals(1, (short) prefs.get(new TypedKey<>("key", Short.class))); } @Test public void restoreFromJson() { persistPrefs.edit().putString("key", "\"1\"").apply(); - assertEquals(1, (short) prefs.get(new PrefsKey<>("key", Short.class))); - assertEquals(1, (short) prefs.get(new PrefsKey<>("key", short.class))); + assertEquals(1, (short) prefs.get(new TypedKey<>("key", Short.class))); + assertEquals(1, (short) prefs.get(new TypedKey<>("key", short.class))); } } diff --git a/src/test/java/com/peel/prefs/StringSetTest.java b/src/test/java/com/peel/prefs/StringSetTest.java index 32cce1f..66861c5 100644 --- a/src/test/java/com/peel/prefs/StringSetTest.java +++ b/src/test/java/com/peel/prefs/StringSetTest.java @@ -61,7 +61,7 @@ public void setUp() { @Test public void store() { Set values = Collections.singleton("abcd"); - prefs.put(new PrefsKey<>("key", stringSetType), values); + prefs.put(new TypedKey<>("key", stringSetType), values); assertEquals("abcd", persistPrefs.getStringSet("key", null).iterator().next()); } @@ -70,12 +70,12 @@ public void restore() { Set values = Collections.singleton("abcd"); System.out.println("Values: " + persistPrefs.getStringSet("key", null)); persistPrefs.edit().putStringSet("key", values).apply(); - assertEquals("abcd", prefs.get(new PrefsKey<>("key", stringSetType)).iterator().next()); + assertEquals("abcd", prefs.get(new TypedKey<>("key", stringSetType)).iterator().next()); } @Test public void restoreFromJson() { persistPrefs.edit().putString("key", "[\"abcd\"]").apply(); - assertEquals("abcd", prefs.get(new PrefsKey<>("key", stringSetType)).iterator().next()); + assertEquals("abcd", prefs.get(new TypedKey<>("key", stringSetType)).iterator().next()); } } diff --git a/src/test/java/com/peel/prefs/StringTest.java b/src/test/java/com/peel/prefs/StringTest.java index 4b51389..430ee88 100644 --- a/src/test/java/com/peel/prefs/StringTest.java +++ b/src/test/java/com/peel/prefs/StringTest.java @@ -55,19 +55,19 @@ public void setUp() { @Test public void store() { - prefs.put(new PrefsKey<>("key", String.class), "abcd"); + prefs.put(new TypedKey<>("key", String.class), "abcd"); assertEquals("abcd", persistPrefs.getString("key", null)); } @Test public void restore() { persistPrefs.edit().putString("key", "abcd").apply(); - assertEquals("abcd", prefs.get(new PrefsKey<>("key", String.class))); + assertEquals("abcd", prefs.get(new TypedKey<>("key", String.class))); } @Test public void restoreFromJson() { persistPrefs.edit().putString("key", new Gson().toJson("abcd")).apply(); - assertEquals("abcd", prefs.get(new PrefsKey<>("key", String.class))); + assertEquals("abcd", prefs.get(new TypedKey<>("key", String.class))); } }