-
-
Notifications
You must be signed in to change notification settings - Fork 26
Feature/support null as default value (primitive types) #114
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: RxJava1.x
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -48,7 +48,7 @@ private void createAccessors() { | |
| private void createBooleanAccessor() { | ||
| accessors.put(Boolean.class, new Accessor<Boolean>() { | ||
| @Override public Boolean get(String key, Boolean defaultValue) { | ||
| return preferences.getBoolean(key, defaultValue); | ||
| return preferences.getBoolean(key, (defaultValue == null) ? false : defaultValue); | ||
| } | ||
|
|
||
| @Override public void put(String key, Boolean value) { | ||
|
|
@@ -60,7 +60,7 @@ private void createBooleanAccessor() { | |
| private void createFloatAccessor() { | ||
| accessors.put(Float.class, new Accessor<Float>() { | ||
| @Override public Float get(String key, Float defaultValue) { | ||
| return preferences.getFloat(key, defaultValue); | ||
| return preferences.getFloat(key, (defaultValue == null) ? 0f : defaultValue); | ||
|
||
| } | ||
|
|
||
| @Override public void put(String key, Float value) { | ||
|
|
@@ -72,7 +72,7 @@ private void createFloatAccessor() { | |
| private void createIntegerAccessor() { | ||
| accessors.put(Integer.class, new Accessor<Integer>() { | ||
| @Override public Integer get(String key, Integer defaultValue) { | ||
| return preferences.getInt(key, defaultValue); | ||
| return preferences.getInt(key, (defaultValue == null) ? 0 : defaultValue); | ||
|
||
| } | ||
|
|
||
| @Override public void put(String key, Integer value) { | ||
|
|
@@ -84,7 +84,7 @@ private void createIntegerAccessor() { | |
| private void createLongAccessor() { | ||
| accessors.put(Long.class, new Accessor<Long>() { | ||
| @Override public Long get(String key, Long defaultValue) { | ||
| return preferences.getLong(key, defaultValue); | ||
| return preferences.getLong(key, (defaultValue == null) ? 0L : defaultValue); | ||
|
||
| } | ||
|
|
||
| @Override public void put(String key, Long value) { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1169,4 +1169,179 @@ public void testPutShouldThrowAnExceptionWhenKeyIsNullForRemove() { | |
| // then | ||
| assertThat(value).isNull(); | ||
| } | ||
|
|
||
| @Test public void testShouldReturnStringValueWithDefaultNull() { | ||
| // given | ||
| prefser.clear(); | ||
| String key = GIVEN_KEY; | ||
| String givenValue = "someText"; | ||
|
|
||
| // when | ||
| prefser.put(key, givenValue); | ||
| String readValue = prefser.get(key, String.class, null); | ||
|
|
||
| // then | ||
| assertThat(givenValue).isEqualTo(readValue); | ||
| } | ||
|
|
||
| // alteracoes comeca aqui | ||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sorry for that comment, after your review I can delete it.
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This comment should be deleted. |
||
|
|
||
| @Test public void testGetShouldReturnNullForCustomObject() { | ||
| // given | ||
| prefser.clear(); | ||
| String keyWhichDoesNotExist = KEY_WHICH_DOES_NOT_EXIST; | ||
|
|
||
| // when | ||
| CustomClass value = prefser.get(keyWhichDoesNotExist, CustomClass.class, null); | ||
|
|
||
| // then | ||
| assertThat(value).isNull(); | ||
| // CustomClass givenObject = new CustomClass(23, "someText"); | ||
| // CustomClass defaultObject = new CustomClass(67, "defaultText"); | ||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also here.
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Commented code should be removed |
||
| } | ||
|
|
||
| @Test public void testGetShouldReturnCustomObjectWithDefaultNull() { | ||
| // given | ||
| prefser.clear(); | ||
| String key = GIVEN_KEY; | ||
| CustomClass givenObject = new CustomClass(12, "someText"); | ||
|
|
||
| // when | ||
| prefser.put(key, givenObject); | ||
| CustomClass readObject = prefser.get(key, CustomClass.class, null); | ||
|
|
||
| // then | ||
| assertThat(givenObject).isEqualTo(readObject); | ||
| } | ||
|
|
||
| @Test public void testShouldReturnNullForDoubleType() { | ||
| // given | ||
| prefser.clear(); | ||
| String keyWhichDoesNotExist = KEY_WHICH_DOES_NOT_EXIST; | ||
|
|
||
| // when | ||
| Double value = prefser.get(keyWhichDoesNotExist, Double.class, null); | ||
|
|
||
| // then | ||
| assertThat(value).isNull(); | ||
| } | ||
|
|
||
| @Test public void testShouldReturnDoubleValueWithDefaultNull() { | ||
| // given | ||
| prefser.clear(); | ||
| String key = GIVEN_KEY; | ||
| Double givenValue = 7.77; | ||
|
|
||
| // when | ||
| prefser.put(key, givenValue); | ||
| Double readValue = prefser.get(key, Double.class, null); | ||
|
|
||
| // then | ||
| assertThat(givenValue).isEqualTo(readValue); | ||
| } | ||
|
|
||
| @Test public void testShouldReturnNullForBooleanType() { | ||
| // given | ||
| prefser.clear(); | ||
| String keyWhichDoesNotExist = KEY_WHICH_DOES_NOT_EXIST; | ||
|
|
||
| // when | ||
| Boolean value = prefser.get(keyWhichDoesNotExist, Boolean.class, null); | ||
|
|
||
| // then | ||
| assertThat(value).isNull(); | ||
| } | ||
|
|
||
| @Test public void testShouldReturnBooleanValueWithDefaultNull() { | ||
| // given | ||
| prefser.clear(); | ||
| String key = GIVEN_KEY; | ||
| Boolean givenValue = true; | ||
|
|
||
| // when | ||
| prefser.put(key, givenValue); | ||
| Boolean readValue = prefser.get(key, Boolean.class, null); | ||
|
|
||
| // then | ||
| assertThat(givenValue).isEqualTo(readValue); | ||
| } | ||
|
|
||
| @Test public void testShouldReturnNullForIntegerType() { | ||
| // given | ||
| prefser.clear(); | ||
| String keyWhichDoesNotExist = KEY_WHICH_DOES_NOT_EXIST; | ||
|
|
||
| // when | ||
| Integer value = prefser.get(keyWhichDoesNotExist, Integer.class, null); | ||
|
|
||
| // then | ||
| assertThat(value).isNull(); | ||
| } | ||
|
|
||
| @Test public void testShouldReturnIntegerValueWithDefaultNull() { | ||
| // given | ||
| prefser.clear(); | ||
| String key = GIVEN_KEY; | ||
| Integer givenValue = 7; | ||
|
|
||
| // when | ||
| prefser.put(key, givenValue); | ||
| Integer readValue = prefser.get(key, Integer.class, null); | ||
|
|
||
| // then | ||
| assertThat(givenValue).isEqualTo(readValue); | ||
| } | ||
|
|
||
| @Test public void testShouldReturnNullForFloatType() { | ||
| // given | ||
| prefser.clear(); | ||
| String keyWhichDoesNotExist = KEY_WHICH_DOES_NOT_EXIST; | ||
|
|
||
| // when | ||
| Float value = prefser.get(keyWhichDoesNotExist, Float.class, null); | ||
|
|
||
| // then | ||
| assertThat(value).isNull(); | ||
| } | ||
|
|
||
| @Test public void testShouldReturnFloatValueWithDefaultNull() { | ||
| // given | ||
| prefser.clear(); | ||
| String key = GIVEN_KEY; | ||
| Float givenValue = 7.45f; | ||
|
|
||
| // when | ||
| prefser.put(key, givenValue); | ||
| Float readValue = prefser.get(key, Float.class, null); | ||
|
|
||
| // then | ||
| assertThat(givenValue).isEqualTo(readValue); | ||
| } | ||
|
|
||
| @Test public void testShouldReturnNullForLongType() { | ||
| // given | ||
| prefser.clear(); | ||
| String keyWhichDoesNotExist = KEY_WHICH_DOES_NOT_EXIST; | ||
|
|
||
| // when | ||
| Long value = prefser.get(keyWhichDoesNotExist, Long.class, null); | ||
|
|
||
| // then | ||
| assertThat(value).isNull(); | ||
| } | ||
|
|
||
| @Test public void testShouldReturnLongValueWithDefaultNull() { | ||
| // given | ||
| prefser.clear(); | ||
| String key = GIVEN_KEY; | ||
| Long givenValue = 7L; | ||
|
|
||
| // when | ||
| prefser.put(key, givenValue); | ||
| Long readValue = prefser.get(key, Long.class, null); | ||
|
|
||
| // then | ||
| assertThat(givenValue).isEqualTo(readValue); | ||
| } | ||
|
|
||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In this use case, we are defining default behavior for the user in the case of passing
null. We don't know if a user wantstrueorfalse, but we're forcing using one of these. The same rule applies to all of the changes below.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No, we are not defining default behavior for the user, because if the user sends null in defValue, it would already return before in Prefser class (and it would return null).
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why would user pass the
null? Why should the user expectfalsein the case of passingnull, but nottrue? How could he or she guess such behavior just by looking at the library API without browsing source code or debugging?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The user will not get
trueorfalseif he/she passingnull, the user will getnull. Look the other comment I mention you, we are returning defValue before that check.Line 250 of Prefser.