Skip to content

Commit

Permalink
Return unmodifiable string set from preferences.
Browse files Browse the repository at this point in the history
  • Loading branch information
NightlyNexus committed Mar 10, 2017
1 parent 58b5c23 commit 06e4e72
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,10 @@ public Preference<String> getString(@NonNull String key, @Nullable String defaul
return new RealPreference<>(preferences, key, defaultValue, StringAdapter.INSTANCE, keyChanges);
}

/** Create a string set preference for {@code key}. Default is an empty set. */
/**
* Create a string set preference for {@code key}. Default is an empty set. Note that returned set
* value will always be unmodifiable.
*/
@RequiresApi(HONEYCOMB)
@CheckResult @NonNull
public Preference<Set<String>> getStringSet(@NonNull String key) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import android.annotation.TargetApi;
import android.content.SharedPreferences;
import android.support.annotation.NonNull;
import java.util.Collections;
import java.util.Set;

import static android.os.Build.VERSION_CODES.HONEYCOMB;
Expand All @@ -14,7 +15,7 @@ final class StringSetAdapter implements Preference.Adapter<Set<String>> {
@Override public Set<String> get(@NonNull String key, @NonNull SharedPreferences preferences) {
Set<String> value = preferences.getStringSet(key, null);
assert value != null; // Not called unless key is present.
return value;
return Collections.unmodifiableSet(value);
}

@Override public void set(@NonNull String key, @NonNull Set<String> value,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import android.annotation.SuppressLint;
import android.content.SharedPreferences;
import io.reactivex.functions.Consumer;
import java.util.LinkedHashSet;
import java.util.Set;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
Expand All @@ -14,7 +16,7 @@
import static com.f2prateek.rx.preferences2.Roshambo.ROCK;
import static java.util.Collections.singleton;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.extractProperty;
import static org.assertj.core.api.Assertions.fail;

@RunWith(RobolectricTestRunner.class) //
@SuppressLint({ "NewApi", "CommitPrefEdits" }) //
Expand Down Expand Up @@ -169,6 +171,29 @@ public class PreferenceTest {
assertThat(preferences.contains("foo")).isFalse();
}

@Test public void stringSetDefaultIsUnmodifiable() {
Preference<Set<String>> preference = rxPreferences.getStringSet("foo");
Set<String> stringSet = preference.get();
try {
stringSet.add("");
fail(stringSet.getClass() + " should not be modifiable.");
} catch (UnsupportedOperationException expected) {
assertThat(expected).hasNoCause();
}
}

@Test public void stringSetIsUnmodifiable() {
Preference<Set<String>> preference = rxPreferences.getStringSet("foo");
preference.set(new LinkedHashSet<String>());
Set<String> stringSet = preference.get();
try {
stringSet.add("");
fail(stringSet.getClass() + " should not be modifiable.");
} catch (UnsupportedOperationException expected) {
assertThat(expected).hasNoCause();
}
}

@Test public void asObservable() {
Preference<String> preference = rxPreferences.getString("foo", "bar");

Expand Down

0 comments on commit 06e4e72

Please sign in to comment.