Added method to parse Integers
This commit is contained in:
parent
94104fd50d
commit
90a3792ccc
2 changed files with 48 additions and 0 deletions
22
src/main/java/com/rometools/utils/Integers.java
Normal file
22
src/main/java/com/rometools/utils/Integers.java
Normal file
|
@ -0,0 +1,22 @@
|
|||
package com.rometools.utils;
|
||||
|
||||
public final class Integers {
|
||||
|
||||
private Integers() {
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts a String into an Integer.
|
||||
*
|
||||
* @param s The String to convert, may be null
|
||||
* @return The parsed Integer or null when parsing is not possible
|
||||
*/
|
||||
public static Integer parse(final String s) {
|
||||
try {
|
||||
return Integer.parseInt(s);
|
||||
} catch (final NumberFormatException e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
26
src/test/java/com/rometools/utils/IntegersTest.java
Normal file
26
src/test/java/com/rometools/utils/IntegersTest.java
Normal 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 IntegersTest {
|
||||
|
||||
@Test
|
||||
public void testParse() {
|
||||
|
||||
final String nullString = null;
|
||||
final String emptyString = null;
|
||||
final String integerString = "1";
|
||||
final String decimalString = "1.0";
|
||||
|
||||
assertThat(Integers.parse(nullString), is(nullValue()));
|
||||
assertThat(Integers.parse(emptyString), is(nullValue()));
|
||||
assertThat(Integers.parse(integerString), is(1));
|
||||
assertThat(Integers.parse(decimalString), is(nullValue()));
|
||||
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in a new issue