Added method to parse Integers

This commit is contained in:
Patrick Gotthard 2014-04-18 19:28:04 +02:00
parent 94104fd50d
commit 90a3792ccc
2 changed files with 48 additions and 0 deletions

View 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;
}
}
}

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 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()));
}
}