@Test public void parseLong() { Function<Object, Long> parser = s -> StringOptionUtil.parseLong(new StringOption(String.valueOf(s))); assertThat(parser.apply("0"), is(0L)); assertThat(parser.apply("1"), is(1L)); assertThat(parser.apply("+100"), is(+100L)); assertThat(parser.apply("-100"), is(-100L)); assertThat(parser.apply(Integer.MAX_VALUE), is((long) Integer.MAX_VALUE)); assertThat(parser.apply(Integer.MIN_VALUE), is((long) Integer.MIN_VALUE)); assertThat(parser.apply(Long.MAX_VALUE), is(Long.MAX_VALUE)); assertThat(parser.apply(Long.MIN_VALUE), is(Long.MIN_VALUE)); raise(() -> parser.apply("")); raise(() -> parser.apply("Hello, world!")); raise(() -> parser.apply(BigInteger.valueOf(Long.MAX_VALUE).add(BigInteger.ONE))); raise(() -> parser.apply(BigInteger.valueOf(Long.MIN_VALUE).subtract(BigInteger.ONE))); }
public static long parseLong(StringOption contents) { CharBuffer buffer = DECODER_POOL.get().decode(contents.get()); if (buffer.hasRemaining() == false) { throw invalidNumber(contents); } boolean negative = false; char first = buffer.get(0); if (first < '0') { if (first == '+') { buffer.get(); } else if (first == '-') { buffer.get(); negative = true; } } long negativeResult = 0; while (buffer.hasRemaining()) { char c = buffer.get(); int column = Character.digit(c, 10); if (column < 0) { throw invalidNumber(contents); } if (negativeResult < (Long.MIN_VALUE / 10)) { throw invalidNumber(contents); } negativeResult *= 10; if (negativeResult < (Long.MIN_VALUE | column)) { throw invalidNumber(contents); } negativeResult -= column; } if (negative) { return negativeResult; } else { if (negativeResult == Long.MIN_VALUE) { throw invalidNumber(contents); } return -negativeResult; } }
StringOptionUtil { public static long parseLong(StringOption contents) { CharBuffer buffer = DECODER_POOL.get().decode(contents.get()); if (buffer.hasRemaining() == false) { throw invalidNumber(contents); } boolean negative = false; char first = buffer.get(0); if (first < '0') { if (first == '+') { buffer.get(); } else if (first == '-') { buffer.get(); negative = true; } } long negativeResult = 0; while (buffer.hasRemaining()) { char c = buffer.get(); int column = Character.digit(c, 10); if (column < 0) { throw invalidNumber(contents); } if (negativeResult < (Long.MIN_VALUE / 10)) { throw invalidNumber(contents); } negativeResult *= 10; if (negativeResult < (Long.MIN_VALUE | column)) { throw invalidNumber(contents); } negativeResult -= column; } if (negative) { return negativeResult; } else { if (negativeResult == Long.MIN_VALUE) { throw invalidNumber(contents); } return -negativeResult; } } }
StringOptionUtil { public static long parseLong(StringOption contents) { CharBuffer buffer = DECODER_POOL.get().decode(contents.get()); if (buffer.hasRemaining() == false) { throw invalidNumber(contents); } boolean negative = false; char first = buffer.get(0); if (first < '0') { if (first == '+') { buffer.get(); } else if (first == '-') { buffer.get(); negative = true; } } long negativeResult = 0; while (buffer.hasRemaining()) { char c = buffer.get(); int column = Character.digit(c, 10); if (column < 0) { throw invalidNumber(contents); } if (negativeResult < (Long.MIN_VALUE / 10)) { throw invalidNumber(contents); } negativeResult *= 10; if (negativeResult < (Long.MIN_VALUE | column)) { throw invalidNumber(contents); } negativeResult -= column; } if (negative) { return negativeResult; } else { if (negativeResult == Long.MIN_VALUE) { throw invalidNumber(contents); } return -negativeResult; } } private  StringOptionUtil(); }
StringOptionUtil { public static long parseLong(StringOption contents) { CharBuffer buffer = DECODER_POOL.get().decode(contents.get()); if (buffer.hasRemaining() == false) { throw invalidNumber(contents); } boolean negative = false; char first = buffer.get(0); if (first < '0') { if (first == '+') { buffer.get(); } else if (first == '-') { buffer.get(); negative = true; } } long negativeResult = 0; while (buffer.hasRemaining()) { char c = buffer.get(); int column = Character.digit(c, 10); if (column < 0) { throw invalidNumber(contents); } if (negativeResult < (Long.MIN_VALUE / 10)) { throw invalidNumber(contents); } negativeResult *= 10; if (negativeResult < (Long.MIN_VALUE | column)) { throw invalidNumber(contents); } negativeResult -= column; } if (negative) { return negativeResult; } else { if (negativeResult == Long.MIN_VALUE) { throw invalidNumber(contents); } return -negativeResult; } } private  StringOptionUtil(); static int countCodePoints(StringOption option); static Reader asReader(StringOption option); static void trim(StringOption option); static void append(StringOption target, StringOption contents); static void append(StringOption target, String contents); static void append(StringBuilder target, StringOption contents); static int parseInt(StringOption contents); static long parseLong(StringOption contents); static BigDecimal parseDecimal(StringOption contents); static void append(
            CharBuffer source, StringOption destination,
            CharsetEncoder encoder, ByteBuffer buffer); }
StringOptionUtil { public static long parseLong(StringOption contents) { CharBuffer buffer = DECODER_POOL.get().decode(contents.get()); if (buffer.hasRemaining() == false) { throw invalidNumber(contents); } boolean negative = false; char first = buffer.get(0); if (first < '0') { if (first == '+') { buffer.get(); } else if (first == '-') { buffer.get(); negative = true; } } long negativeResult = 0; while (buffer.hasRemaining()) { char c = buffer.get(); int column = Character.digit(c, 10); if (column < 0) { throw invalidNumber(contents); } if (negativeResult < (Long.MIN_VALUE / 10)) { throw invalidNumber(contents); } negativeResult *= 10; if (negativeResult < (Long.MIN_VALUE | column)) { throw invalidNumber(contents); } negativeResult -= column; } if (negative) { return negativeResult; } else { if (negativeResult == Long.MIN_VALUE) { throw invalidNumber(contents); } return -negativeResult; } } private  StringOptionUtil(); static int countCodePoints(StringOption option); static Reader asReader(StringOption option); static void trim(StringOption option); static void append(StringOption target, StringOption contents); static void append(StringOption target, String contents); static void append(StringBuilder target, StringOption contents); static int parseInt(StringOption contents); static long parseLong(StringOption contents); static BigDecimal parseDecimal(StringOption contents); static void append(
            CharBuffer source, StringOption destination,
            CharsetEncoder encoder, ByteBuffer buffer); static final Charset ENCODING; }