@Test public void testCollectionNotPersistedByLibrary1() { this.preferences.edit().putString(PREF_KEY, "<String[]>text,text").commit(); try { CollectionPreference.getFromPreferences(preferences, PREF_KEY, null); throw new AssertionError("No exception thrown."); } catch (IllegalStateException e) { assertThat( e.getMessage(), is("Trying to obtain a collection for the key(" + PREF_KEY + ") from shared preferences not saved by the Preferences library.") ); } }
@SuppressWarnings("ConstantConditions") public static <T> Collection<T> getFromPreferences(@NonNull final SharedPreferences preferences, @NonNull final String key, @Nullable final Collection<T> defValue) { final String value = preferences.getString(key, null); if (value == null) { return defValue; } try { final T[] array = ArrayPreference.getFromPreferences(preferences, key, null); return Arrays.asList(array); } catch (ClassCastException e) { final String arrayValue = ArrayPreference.extractArrayElementsFromPreferenceValue(value); throw new ClassCastException( "Cannot obtain a collection for the key(" + key + ") from shared preferences. " + "Value(" + arrayValue + ") is not a collection!" ); } catch (IllegalArgumentException e) { throw new IllegalArgumentException( "Failed to obtain collection for the key(" + key + ") from shared preferences. " + "Only collections of primitive types or theirs boxed representations including String are supported." ); } catch (IllegalStateException e) { throw new IllegalStateException( "Trying to obtain a collection for the key(" + key + ") from shared preferences not saved by the Preferences library." ); } }
CollectionPreference extends SharedPreference<Collection<T>> { @SuppressWarnings("ConstantConditions") public static <T> Collection<T> getFromPreferences(@NonNull final SharedPreferences preferences, @NonNull final String key, @Nullable final Collection<T> defValue) { final String value = preferences.getString(key, null); if (value == null) { return defValue; } try { final T[] array = ArrayPreference.getFromPreferences(preferences, key, null); return Arrays.asList(array); } catch (ClassCastException e) { final String arrayValue = ArrayPreference.extractArrayElementsFromPreferenceValue(value); throw new ClassCastException( "Cannot obtain a collection for the key(" + key + ") from shared preferences. " + "Value(" + arrayValue + ") is not a collection!" ); } catch (IllegalArgumentException e) { throw new IllegalArgumentException( "Failed to obtain collection for the key(" + key + ") from shared preferences. " + "Only collections of primitive types or theirs boxed representations including String are supported." ); } catch (IllegalStateException e) { throw new IllegalStateException( "Trying to obtain a collection for the key(" + key + ") from shared preferences not saved by the Preferences library." ); } } }
CollectionPreference extends SharedPreference<Collection<T>> { @SuppressWarnings("ConstantConditions") public static <T> Collection<T> getFromPreferences(@NonNull final SharedPreferences preferences, @NonNull final String key, @Nullable final Collection<T> defValue) { final String value = preferences.getString(key, null); if (value == null) { return defValue; } try { final T[] array = ArrayPreference.getFromPreferences(preferences, key, null); return Arrays.asList(array); } catch (ClassCastException e) { final String arrayValue = ArrayPreference.extractArrayElementsFromPreferenceValue(value); throw new ClassCastException( "Cannot obtain a collection for the key(" + key + ") from shared preferences. " + "Value(" + arrayValue + ") is not a collection!" ); } catch (IllegalArgumentException e) { throw new IllegalArgumentException( "Failed to obtain collection for the key(" + key + ") from shared preferences. " + "Only collections of primitive types or theirs boxed representations including String are supported." ); } catch (IllegalStateException e) { throw new IllegalStateException( "Trying to obtain a collection for the key(" + key + ") from shared preferences not saved by the Preferences library." ); } } CollectionPreference(@NonNull final String key, @NonNull final Class<T> componentType, @Nullable final Collection<T> defValue); }
CollectionPreference extends SharedPreference<Collection<T>> { @SuppressWarnings("ConstantConditions") public static <T> Collection<T> getFromPreferences(@NonNull final SharedPreferences preferences, @NonNull final String key, @Nullable final Collection<T> defValue) { final String value = preferences.getString(key, null); if (value == null) { return defValue; } try { final T[] array = ArrayPreference.getFromPreferences(preferences, key, null); return Arrays.asList(array); } catch (ClassCastException e) { final String arrayValue = ArrayPreference.extractArrayElementsFromPreferenceValue(value); throw new ClassCastException( "Cannot obtain a collection for the key(" + key + ") from shared preferences. " + "Value(" + arrayValue + ") is not a collection!" ); } catch (IllegalArgumentException e) { throw new IllegalArgumentException( "Failed to obtain collection for the key(" + key + ") from shared preferences. " + "Only collections of primitive types or theirs boxed representations including String are supported." ); } catch (IllegalStateException e) { throw new IllegalStateException( "Trying to obtain a collection for the key(" + key + ") from shared preferences not saved by the Preferences library." ); } } CollectionPreference(@NonNull final String key, @NonNull final Class<T> componentType, @Nullable final Collection<T> defValue); @SuppressWarnings("unchecked") @CheckResult static boolean putIntoPreferences(@NonNull final SharedPreferences preferences, @NonNull final String key, @Nullable final Collection<T> value, @NonNull final Class<T> componentType); @SuppressWarnings("ConstantConditions") static Collection<T> getFromPreferences(@NonNull final SharedPreferences preferences, @NonNull final String key, @Nullable final Collection<T> defValue); }
CollectionPreference extends SharedPreference<Collection<T>> { @SuppressWarnings("ConstantConditions") public static <T> Collection<T> getFromPreferences(@NonNull final SharedPreferences preferences, @NonNull final String key, @Nullable final Collection<T> defValue) { final String value = preferences.getString(key, null); if (value == null) { return defValue; } try { final T[] array = ArrayPreference.getFromPreferences(preferences, key, null); return Arrays.asList(array); } catch (ClassCastException e) { final String arrayValue = ArrayPreference.extractArrayElementsFromPreferenceValue(value); throw new ClassCastException( "Cannot obtain a collection for the key(" + key + ") from shared preferences. " + "Value(" + arrayValue + ") is not a collection!" ); } catch (IllegalArgumentException e) { throw new IllegalArgumentException( "Failed to obtain collection for the key(" + key + ") from shared preferences. " + "Only collections of primitive types or theirs boxed representations including String are supported." ); } catch (IllegalStateException e) { throw new IllegalStateException( "Trying to obtain a collection for the key(" + key + ") from shared preferences not saved by the Preferences library." ); } } CollectionPreference(@NonNull final String key, @NonNull final Class<T> componentType, @Nullable final Collection<T> defValue); @SuppressWarnings("unchecked") @CheckResult static boolean putIntoPreferences(@NonNull final SharedPreferences preferences, @NonNull final String key, @Nullable final Collection<T> value, @NonNull final Class<T> componentType); @SuppressWarnings("ConstantConditions") static Collection<T> getFromPreferences(@NonNull final SharedPreferences preferences, @NonNull final String key, @Nullable final Collection<T> defValue);  }