@Test public void testNotPersistedArray() { assertThat(ArrayPreference.getFromPreferences(preferences, PREF_KEY, null), is(nullValue())); }
@SuppressWarnings("unchecked") @Nullable public static <A> A getFromPreferences(@NonNull final SharedPreferences preferences, @NonNull final String key, @Nullable final Object defValue) { final String value = preferences.getString(key, null); if (TextUtils.isEmpty(value)) { return (A) defValue; } Object array; final Matcher valueMatcher = VALUE_PATTERN.matcher(value); if (valueMatcher.reset(value).matches()) { final String arrayClassName = valueMatcher.group(1) + "[]"; final Class<?> arrayClass = resolveArrayClassByName(arrayClassName); if (arrayClass == null) { final String componentName = arrayClassName.substring(0, arrayClassName.length() - 2); throw new IllegalArgumentException( "Failed to obtain an array of(" + componentName + ") for the key(" + key + ") from shared preferences. " + "Only arrays of primitive types or theirs boxed representations including String are supported." ); } final String jsonArrayValue = "[" + valueMatcher.group(2) + "]"; JSONArray jsonArray; try { jsonArray = new JSONArray(jsonArrayValue); } catch (JSONException e) { throw new ClassCastException( "Cannot obtain an array for the key(" + key + ") from shared preferences. " + "Value(" + jsonArrayValue + ") is not an array!" ); } final int n = jsonArray.length(); array = createArrayInSize(arrayClass.getComponentType(), n); for (int i = 0; i < n; i++) { setArrayValueAt(arrayClass, array, i, jsonArray.opt(i)); } } else { throw new IllegalStateException( "Trying to obtain an array for the key(" + key + ") from shared preferences not saved by the Preferences library." ); } return (A) array; }
ArrayPreference extends SharedPreference<T> { @SuppressWarnings("unchecked") @Nullable public static <A> A getFromPreferences(@NonNull final SharedPreferences preferences, @NonNull final String key, @Nullable final Object defValue) { final String value = preferences.getString(key, null); if (TextUtils.isEmpty(value)) { return (A) defValue; } Object array; final Matcher valueMatcher = VALUE_PATTERN.matcher(value); if (valueMatcher.reset(value).matches()) { final String arrayClassName = valueMatcher.group(1) + "[]"; final Class<?> arrayClass = resolveArrayClassByName(arrayClassName); if (arrayClass == null) { final String componentName = arrayClassName.substring(0, arrayClassName.length() - 2); throw new IllegalArgumentException( "Failed to obtain an array of(" + componentName + ") for the key(" + key + ") from shared preferences. " + "Only arrays of primitive types or theirs boxed representations including String are supported." ); } final String jsonArrayValue = "[" + valueMatcher.group(2) + "]"; JSONArray jsonArray; try { jsonArray = new JSONArray(jsonArrayValue); } catch (JSONException e) { throw new ClassCastException( "Cannot obtain an array for the key(" + key + ") from shared preferences. " + "Value(" + jsonArrayValue + ") is not an array!" ); } final int n = jsonArray.length(); array = createArrayInSize(arrayClass.getComponentType(), n); for (int i = 0; i < n; i++) { setArrayValueAt(arrayClass, array, i, jsonArray.opt(i)); } } else { throw new IllegalStateException( "Trying to obtain an array for the key(" + key + ") from shared preferences not saved by the Preferences library." ); } return (A) array; } }
ArrayPreference extends SharedPreference<T> { @SuppressWarnings("unchecked") @Nullable public static <A> A getFromPreferences(@NonNull final SharedPreferences preferences, @NonNull final String key, @Nullable final Object defValue) { final String value = preferences.getString(key, null); if (TextUtils.isEmpty(value)) { return (A) defValue; } Object array; final Matcher valueMatcher = VALUE_PATTERN.matcher(value); if (valueMatcher.reset(value).matches()) { final String arrayClassName = valueMatcher.group(1) + "[]"; final Class<?> arrayClass = resolveArrayClassByName(arrayClassName); if (arrayClass == null) { final String componentName = arrayClassName.substring(0, arrayClassName.length() - 2); throw new IllegalArgumentException( "Failed to obtain an array of(" + componentName + ") for the key(" + key + ") from shared preferences. " + "Only arrays of primitive types or theirs boxed representations including String are supported." ); } final String jsonArrayValue = "[" + valueMatcher.group(2) + "]"; JSONArray jsonArray; try { jsonArray = new JSONArray(jsonArrayValue); } catch (JSONException e) { throw new ClassCastException( "Cannot obtain an array for the key(" + key + ") from shared preferences. " + "Value(" + jsonArrayValue + ") is not an array!" ); } final int n = jsonArray.length(); array = createArrayInSize(arrayClass.getComponentType(), n); for (int i = 0; i < n; i++) { setArrayValueAt(arrayClass, array, i, jsonArray.opt(i)); } } else { throw new IllegalStateException( "Trying to obtain an array for the key(" + key + ") from shared preferences not saved by the Preferences library." ); } return (A) array; } ArrayPreference(@NonNull final String key, @Nullable final T defValue); }
ArrayPreference extends SharedPreference<T> { @SuppressWarnings("unchecked") @Nullable public static <A> A getFromPreferences(@NonNull final SharedPreferences preferences, @NonNull final String key, @Nullable final Object defValue) { final String value = preferences.getString(key, null); if (TextUtils.isEmpty(value)) { return (A) defValue; } Object array; final Matcher valueMatcher = VALUE_PATTERN.matcher(value); if (valueMatcher.reset(value).matches()) { final String arrayClassName = valueMatcher.group(1) + "[]"; final Class<?> arrayClass = resolveArrayClassByName(arrayClassName); if (arrayClass == null) { final String componentName = arrayClassName.substring(0, arrayClassName.length() - 2); throw new IllegalArgumentException( "Failed to obtain an array of(" + componentName + ") for the key(" + key + ") from shared preferences. " + "Only arrays of primitive types or theirs boxed representations including String are supported." ); } final String jsonArrayValue = "[" + valueMatcher.group(2) + "]"; JSONArray jsonArray; try { jsonArray = new JSONArray(jsonArrayValue); } catch (JSONException e) { throw new ClassCastException( "Cannot obtain an array for the key(" + key + ") from shared preferences. " + "Value(" + jsonArrayValue + ") is not an array!" ); } final int n = jsonArray.length(); array = createArrayInSize(arrayClass.getComponentType(), n); for (int i = 0; i < n; i++) { setArrayValueAt(arrayClass, array, i, jsonArray.opt(i)); } } else { throw new IllegalStateException( "Trying to obtain an array for the key(" + key + ") from shared preferences not saved by the Preferences library." ); } return (A) array; } ArrayPreference(@NonNull final String key, @Nullable final T defValue); @CheckResult static boolean putIntoPreferences(@NonNull final SharedPreferences preferences, @NonNull final String key, @Nullable final Object value); @SuppressWarnings("unchecked") @Nullable static A getFromPreferences(@NonNull final SharedPreferences preferences, @NonNull final String key, @Nullable final Object defValue); }
ArrayPreference extends SharedPreference<T> { @SuppressWarnings("unchecked") @Nullable public static <A> A getFromPreferences(@NonNull final SharedPreferences preferences, @NonNull final String key, @Nullable final Object defValue) { final String value = preferences.getString(key, null); if (TextUtils.isEmpty(value)) { return (A) defValue; } Object array; final Matcher valueMatcher = VALUE_PATTERN.matcher(value); if (valueMatcher.reset(value).matches()) { final String arrayClassName = valueMatcher.group(1) + "[]"; final Class<?> arrayClass = resolveArrayClassByName(arrayClassName); if (arrayClass == null) { final String componentName = arrayClassName.substring(0, arrayClassName.length() - 2); throw new IllegalArgumentException( "Failed to obtain an array of(" + componentName + ") for the key(" + key + ") from shared preferences. " + "Only arrays of primitive types or theirs boxed representations including String are supported." ); } final String jsonArrayValue = "[" + valueMatcher.group(2) + "]"; JSONArray jsonArray; try { jsonArray = new JSONArray(jsonArrayValue); } catch (JSONException e) { throw new ClassCastException( "Cannot obtain an array for the key(" + key + ") from shared preferences. " + "Value(" + jsonArrayValue + ") is not an array!" ); } final int n = jsonArray.length(); array = createArrayInSize(arrayClass.getComponentType(), n); for (int i = 0; i < n; i++) { setArrayValueAt(arrayClass, array, i, jsonArray.opt(i)); } } else { throw new IllegalStateException( "Trying to obtain an array for the key(" + key + ") from shared preferences not saved by the Preferences library." ); } return (A) array; } ArrayPreference(@NonNull final String key, @Nullable final T defValue); @CheckResult static boolean putIntoPreferences(@NonNull final SharedPreferences preferences, @NonNull final String key, @Nullable final Object value); @SuppressWarnings("unchecked") @Nullable static A getFromPreferences(@NonNull final SharedPreferences preferences, @NonNull final String key, @Nullable final Object defValue);  }