@Test public void testAsInteger() throws ProvidenceConfigException { assertThat(asInteger(2, Integer.MIN_VALUE, Integer.MAX_VALUE), is(2)); assertThat(asInteger((Numeric) () -> 111, Integer.MIN_VALUE, Integer.MAX_VALUE), is(111)); assertThat(asInteger(false, Integer.MIN_VALUE, Integer.MAX_VALUE), is(0)); assertThat(asInteger(12345.0, Integer.MIN_VALUE, Integer.MAX_VALUE), is(12345)); try { asInteger("foo", Integer.MIN_VALUE, Integer.MAX_VALUE); fail("no exception"); } catch (ProvidenceConfigException e) { assertThat(e.getMessage(), is("Unable to convert String to an int")); } try { asInteger(1234567890123456789L, Integer.MIN_VALUE, Integer.MAX_VALUE); fail("no exception"); } catch (ProvidenceConfigException e) { assertThat(e.getMessage(), is("Long value outsize of bounds: 1234567890123456789 > 2147483647")); } try { asInteger(-1234, Byte.MIN_VALUE, Byte.MAX_VALUE); fail("no exception"); } catch (ProvidenceConfigException e) { assertThat(e.getMessage(), is("Integer value outsize of bounds: -1234 < -128")); } try { asInteger(12.345, Integer.MIN_VALUE, Integer.MAX_VALUE); fail("no exception"); } catch (ProvidenceConfigException e) { assertThat(e.getMessage(), is("Truncating integer decimals from 12.345")); } try { asInteger(new Object(), Byte.MIN_VALUE, Byte.MAX_VALUE); fail("no exception"); } catch (ProvidenceConfigException e) { assertThat(e.getMessage(), is("Unable to convert Object to an int")); } }
static int asInteger(Object value, int min, int max) throws ProvidenceConfigException { if (value instanceof Long) { return validateInRange("Long", (Long) value, min, max); } else if (value instanceof Float || value instanceof Double) { long l = ((Number) value).longValue(); if ((double) l != ((Number) value).doubleValue()) { throw new ProvidenceConfigException("Truncating integer decimals from " + value.toString()); } return validateInRange(value.getClass().getSimpleName(), l, min, max); } else if (value instanceof Number) { return validateInRange(value.getClass().getSimpleName(), ((Number) value).intValue(), min, max); } else if (value instanceof Numeric) { return validateInRange("Numeric", ((Numeric) value).asInteger(), min, max); } else if (value instanceof Boolean) { return ((Boolean) value) ? 1 : 0; } throw new ProvidenceConfigException("Unable to convert " + value.getClass().getSimpleName() + " to an int"); }
ProvidenceConfigUtil { static int asInteger(Object value, int min, int max) throws ProvidenceConfigException { if (value instanceof Long) { return validateInRange("Long", (Long) value, min, max); } else if (value instanceof Float || value instanceof Double) { long l = ((Number) value).longValue(); if ((double) l != ((Number) value).doubleValue()) { throw new ProvidenceConfigException("Truncating integer decimals from " + value.toString()); } return validateInRange(value.getClass().getSimpleName(), l, min, max); } else if (value instanceof Number) { return validateInRange(value.getClass().getSimpleName(), ((Number) value).intValue(), min, max); } else if (value instanceof Numeric) { return validateInRange("Numeric", ((Numeric) value).asInteger(), min, max); } else if (value instanceof Boolean) { return ((Boolean) value) ? 1 : 0; } throw new ProvidenceConfigException("Unable to convert " + value.getClass().getSimpleName() + " to an int"); } }
ProvidenceConfigUtil { static int asInteger(Object value, int min, int max) throws ProvidenceConfigException { if (value instanceof Long) { return validateInRange("Long", (Long) value, min, max); } else if (value instanceof Float || value instanceof Double) { long l = ((Number) value).longValue(); if ((double) l != ((Number) value).doubleValue()) { throw new ProvidenceConfigException("Truncating integer decimals from " + value.toString()); } return validateInRange(value.getClass().getSimpleName(), l, min, max); } else if (value instanceof Number) { return validateInRange(value.getClass().getSimpleName(), ((Number) value).intValue(), min, max); } else if (value instanceof Numeric) { return validateInRange("Numeric", ((Numeric) value).asInteger(), min, max); } else if (value instanceof Boolean) { return ((Boolean) value) ? 1 : 0; } throw new ProvidenceConfigException("Unable to convert " + value.getClass().getSimpleName() + " to an int"); } private  ProvidenceConfigUtil(); }
ProvidenceConfigUtil { static int asInteger(Object value, int min, int max) throws ProvidenceConfigException { if (value instanceof Long) { return validateInRange("Long", (Long) value, min, max); } else if (value instanceof Float || value instanceof Double) { long l = ((Number) value).longValue(); if ((double) l != ((Number) value).doubleValue()) { throw new ProvidenceConfigException("Truncating integer decimals from " + value.toString()); } return validateInRange(value.getClass().getSimpleName(), l, min, max); } else if (value instanceof Number) { return validateInRange(value.getClass().getSimpleName(), ((Number) value).intValue(), min, max); } else if (value instanceof Numeric) { return validateInRange("Numeric", ((Numeric) value).asInteger(), min, max); } else if (value instanceof Boolean) { return ((Boolean) value) ? 1 : 0; } throw new ProvidenceConfigException("Unable to convert " + value.getClass().getSimpleName() + " to an int"); } private  ProvidenceConfigUtil(); static Path canonicalFileLocation(@Nonnull Path file); }
ProvidenceConfigUtil { static int asInteger(Object value, int min, int max) throws ProvidenceConfigException { if (value instanceof Long) { return validateInRange("Long", (Long) value, min, max); } else if (value instanceof Float || value instanceof Double) { long l = ((Number) value).longValue(); if ((double) l != ((Number) value).doubleValue()) { throw new ProvidenceConfigException("Truncating integer decimals from " + value.toString()); } return validateInRange(value.getClass().getSimpleName(), l, min, max); } else if (value instanceof Number) { return validateInRange(value.getClass().getSimpleName(), ((Number) value).intValue(), min, max); } else if (value instanceof Numeric) { return validateInRange("Numeric", ((Numeric) value).asInteger(), min, max); } else if (value instanceof Boolean) { return ((Boolean) value) ? 1 : 0; } throw new ProvidenceConfigException("Unable to convert " + value.getClass().getSimpleName() + " to an int"); } private  ProvidenceConfigUtil(); static Path canonicalFileLocation(@Nonnull Path file); static final String UNDEFINED; }