Added method for parsing Doubles

This commit is contained in:
Patrick Gotthard 2014-04-18 21:35:30 +02:00
parent 25abc65f91
commit 7fa58a5234
2 changed files with 51 additions and 0 deletions

View file

@ -0,0 +1,25 @@
package com.rometools.utils;
public class Doubles {
private Doubles() {
}
/**
* Converts a String into an Double.
*
* @param s The String to convert, may be null
* @return The parsed Double or null when parsing is not possible
*/
public static Double parse(final String s) {
Double parsed = null;
try {
if (s != null) {
parsed = Double.parseDouble(s);
}
} catch (final NumberFormatException e) {
}
return parsed;
}
}

View file

@ -0,0 +1,26 @@
package com.rometools.utils;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.nullValue;
import org.junit.Test;
public class DoublesTest {
@Test
public void testParse() {
final String nullString = null;
final String emptyString = null;
final String integerString = "1";
final String decimalString = "1.0";
assertThat(Doubles.parse(nullString), is(nullValue()));
assertThat(Doubles.parse(emptyString), is(nullValue()));
assertThat(Doubles.parse(integerString), is(1.0));
assertThat(Doubles.parse(decimalString), is(1.0));
}
}