Skip to content

Commit

Permalink
feat(settings): add helper methods to get list of float or double
Browse files Browse the repository at this point in the history
  • Loading branch information
munendrasn committed Jan 13, 2018
1 parent 2ac1852 commit a586b3d
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 2 deletions.
66 changes: 64 additions & 2 deletions pippo-core/src/main/java/ro/pippo/core/PippoSettings.java
Original file line number Diff line number Diff line change
Expand Up @@ -640,7 +640,7 @@ public List<String> getStrings(String name, String delimiter) {
* Returns a list of comma-delimited integers from the specified name.
*
* @param name
* @return list of strings
* @return list of integers
*/
public List<Integer> getIntegers(String name) {
return getIntegers(name, DEFAULT_LIST_DELIMITER);
Expand Down Expand Up @@ -672,7 +672,7 @@ public List<Integer> getIntegers(String name, String delimiter) {
* Returns a list of comma-delimited longs from the specified name.
*
* @param name
* @return list of strings
* @return list of longs
*/
public List<Long> getLongs(String name) {
return getLongs(name, DEFAULT_LIST_DELIMITER);
Expand Down Expand Up @@ -700,6 +700,68 @@ public List<Long> getLongs(String name, String delimiter) {
return Collections.unmodifiableList(longs);
}

/**
* Returns a list of comma-delimited floats from the specified name.
*
* @param name
* @return list of floats
*/
public List<Float> getFloats(String name) {
return getFloats(name, DEFAULT_LIST_DELIMITER);
}

/**
* Returns a list of floats from the specified name using the specified delimiter.
*
* @param name
* @param delimiter
* @return list of floats
*/
public List<Float> getFloats(String name, String delimiter) {
List<String> strings = getStrings(name, delimiter);

List<Float> floats = new ArrayList<>(strings.size());
for (String value : strings) {
try {
float i = Float.parseFloat(value);
floats.add(i);
} catch (NumberFormatException e) {
}
}
return Collections.unmodifiableList(floats);
}

/**
* Returns a list of comma-delimited doubles from the specified name.
*
* @param name
* @return list of doubles
*/
public List<Double> getDoubles(String name) {
return getDoubles(name, DEFAULT_LIST_DELIMITER);
}

/**
* Returns a list of doubles from the specified name using the specified delimiter.
*
* @param name
* @param delimiter
* @return list of doubles
*/
public List<Double> getDoubles(String name, String delimiter) {
List<String> strings = getStrings(name, delimiter);

List<Double> doubles = new ArrayList<>(strings.size());
for (String value : strings) {
try {
double i = Double.parseDouble(value);
doubles.add(i);
} catch (NumberFormatException e) {
}
}
return Collections.unmodifiableList(doubles);
}

/**
* Gets the duration setting and converts it to milliseconds.
* <p/>
Expand Down
16 changes: 16 additions & 0 deletions pippo-core/src/test/java/ro/pippo/core/PippoSettingsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,22 @@ public void testGetIntegers() {
assertTrue(values.isEmpty());
}

@Test
public void testGetDoubles() {
// tests would similar for getFloats(String, String)
Mockito.doReturn(" [1234, 123, value3]").when(pippoSettings)
.getString("key", null);
Mockito.doCallRealMethod().when(pippoSettings).getStrings("key", ",");
Mockito.doCallRealMethod().when(pippoSettings).getDoubles("key", ",");

List<Double> values = pippoSettings.getDoubles("key", ",");
assertEquals(Arrays.asList(1234d, 123d), values);

Mockito.doReturn(Collections.emptyList()).when(pippoSettings).getStrings("key", ",");
values = pippoSettings.getDoubles("key", ",");
assertTrue(values.isEmpty());
}

@Test
public void testGetNumber() {
Mockito.doReturn(" 1234").when(pippoSettings).getString("key", null);
Expand Down

0 comments on commit a586b3d

Please sign in to comment.