public void givenString_whenTryParse_shouldConvertToInt() {
String givenString = "42";
Integer result = Ints.tryParse(givenString);
assertThat(result).isEqualTo(42);
}
public void testTryParse() {
tryParseAndAssertEquals(0, "0");
tryParseAndAssertEquals(0, "-0");
tryParseAndAssertEquals(1, "1");
tryParseAndAssertEquals(-1, "-1");
tryParseAndAssertEquals(8900, "8900");
tryParseAndAssertEquals(-8900, "-8900");
tryParseAndAssertEquals(GREATEST, Integer.toString(GREATEST));
tryParseAndAssertEquals(LEAST, Integer.toString(LEAST));
assertNull(Ints.tryParse(""));
assertNull(Ints.tryParse("-"));
assertNull(Ints.tryParse("+1"));
assertNull(Ints.tryParse("9999999999999999"));
assertNull("Max integer + 1",
Ints.tryParse(Long.toString(((long) GREATEST) + 1)));
assertNull("Max integer * 10",
Ints.tryParse(Long.toString(((long) GREATEST) * 10)));
assertNull("Min integer - 1",
Ints.tryParse(Long.toString(((long) LEAST) - 1)));
assertNull("Min integer * 10",
Ints.tryParse(Long.toString(((long) LEAST) * 10)));
assertNull("Max long", Ints.tryParse(Long.toString(Long.MAX_VALUE)));
assertNull("Min long", Ints.tryParse(Long.toString(Long.MIN_VALUE)));
assertNull(Ints.tryParse("\u0662\u06f3"));
}
public void conversion() {
/**
* We can also convert objects from one type to another. Note that if parsing fails, they're no exceptions which is thrown but the returned result is
* null.
*/
Double price = Doubles.tryParse("19.30");
assertTrue("Parsed price should be 19.30 but is "+ price.doubleValue(), price.doubleValue() == 19.30d);
Integer age = Ints.tryParse("eighteen");
assertTrue("'eighteen' shouldn't be parsed to Integer, null was expected but "+age+" was received", age == null);
/**
* Another method to convert from String to Java's primitive is stringConverter() method from each wrapper. Note that, unlinke tryParse, it throws an exception if
* given String can't be parsed.
*/
Double normalPrice = Doubles.stringConverter().convert("19.32");
assertTrue("normalPrice should be 19.32 but is "+normalPrice.doubleValue(), normalPrice.doubleValue() == 19.32d);
try {
// this test should fail
Doubles.stringConverter().convert("xyz");
fail("The test should not pass here: 'xyz' isn't convertissable String");
} catch (Exception e) {
assertTrue("Exception should be NumberFormatExceptions but is "+e.getClass(), e.getClass() == NumberFormatException.class);
}
}
public static void main(String[] args) {
String numberAsString = "10";
Integer value = Ints.tryParse(numberAsString);
}
public static void main(String[] args) {
Integer a = Ints.tryParse("42");
Assert.assertEquals(Integer.valueOf(42), a);
// If long value is passed it returns null
Integer b = Ints.tryParse("87179869184");
Assert.assertTrue(b == null);
}
private static Set<ShardId> findAllShardsForIndex(Path indexPath) throws IOException {
Set<ShardId> shardIds = new HashSet<>();
if (Files.isDirectory(indexPath)) {
try (DirectoryStream<Path> stream = Files.newDirectoryStream(indexPath)) {
String currentIndex = indexPath.getFileName().toString();
for (Path shardPath : stream) {
if (Files.isDirectory(shardPath)) {
Integer shardId = Ints.tryParse(shardPath.getFileName().toString());
if (shardId != null) {
ShardId id = new ShardId(currentIndex, shardId);
shardIds.add(id);
}
}
}
}
}
return shardIds;
}
private Optional<Integer> configureErrorRetryCount(String property) {
Integer count = null;
if (null != property) {
count = Ints.tryParse(property);
if (null == count || count < 0) {
throw new IllegalArgumentException("System property [" + S3_MAX_ERROR_RETRY + "=" + property + "] must be a valid positive Integer");
}
}
return Optional.fromNullable(count);
}
public void save() {
String text = getTextField().getText();
Integer value = Ints.tryParse(text);
if (value == null) {
getOption().resetToDefault();
load();
} else {
getOption().set(value);
}
}
private void resizeImage() {
if (image.getUrl().equals(getIconImage().getUrl())) {
unclipImage();
return;
}
String width = getElement().getStyle().getWidth();
String height = getElement().getStyle().getHeight();
// the situation right after refreshing the page
if (width.isEmpty() || height.isEmpty()) {
return;
}
int frameWidth = Ints.tryParse(width.substring(0, width.indexOf("px")));
int frameHeight = Ints.tryParse(height.substring(0, height.indexOf("px")));
if (scalingMode.equals("0")) {
float ratio = Math.min(frameWidth / (float) getPreferredWidth(),
frameHeight / (float) getPreferredHeight());
int scaledWidth = Double.valueOf(getPreferredWidth() * ratio).intValue();
int scaledHeight = Double.valueOf(getPreferredHeight() * ratio).intValue();
image.setSize(scaledWidth + "px", scaledHeight + "px");
} else if (scalingMode.equals("1")) {
image.setSize("100%", "100%");
} else {
throw new IllegalStateException("Illegal scaling mode: " + scalingMode);
}
}
private static boolean isAnonymousRest(String lastPart) {
return Ints.tryParse(lastPart) != null;
}