/** * Appends the text in this object into the given {@link StringBuilder}. * @param buffer the destination {@link StringBuilder} * @return the appended {@link StringBuilder} * @throws NullPointerException if this object represents {@code null} * @since 0.9.1 */ public StringBuilder appendTo(StringBuilder buffer) { if (nullValue) { throw new NullPointerException(); } StringOptionUtil.append(buffer, this); return buffer; }
CharBuffer buffer = DECODER_POOL.get().decode(contents.get()); if (buffer.hasRemaining() == false) { throw invalidNumber(contents); int column = Character.digit(c, 10); if (column < 0) { throw invalidNumber(contents); throw invalidNumber(contents); throw invalidNumber(contents); } else { if (negativeResult == Long.MIN_VALUE) { throw invalidNumber(contents);
int last = length - 1; for (; start <= last; start++) { if (isTrimTarget(bytes[start]) == false) { break; if (isTrimTarget(bytes[last]) == false) { break;
/** * Parses the given {@link StringOption} which may represent a decimal value. * @param contents the text contents * @return the parsed decimal value * @throws NullPointerException if the {@link StringOption} is/represents {@code null} * @throws IllegalArgumentException if the character sequence is wrong * @since 0.9.1 */ public static BigDecimal parseDecimal(StringOption contents) { CharBuffer buffer = DECODER_POOL.get().decode(contents.get()); if (buffer.hasRemaining() == false) { throw invalidNumber(contents); } int length = buffer.remaining(); if (buffer.hasArray()) { char[] array = buffer.array(); int offsetInArray = buffer.position() + buffer.arrayOffset(); return new BigDecimal(array, offsetInArray, length); } else { char[] cbuf = borrowCharArrayBuf(length); buffer.get(cbuf, 0, length); return new BigDecimal(cbuf, 0, length); } }
private static String dump(StringOption option) { try (Reader reader = StringOptionUtil.asReader(option)) { char[] buf = new char[256]; StringBuffer results = new StringBuffer(); while (true) { int read = reader.read(buf); if (read < 0) { break; } results.append(buf, 0, read); } return results.toString(); } catch (IOException e) { throw new AssertionError(e); } } }
/** * count code points. */ @Test public void countCodePoints() { Consumer<String> tester = s -> assertThat( StringOptionUtil.countCodePoints(new StringOption(s)), is((int) s.codePoints().count())); tester.accept(""); tester.accept("a"); tester.accept("Hello, world!"); tester.accept("\u00c0"); tester.accept(StringOptionTest.HELLO_JP); tester.accept(new StringBuilder() .appendCodePoint(0x1f37a) .appendCodePoint(0x1f363) .toString()); }
/** * parse decimal. */ @Test public void parseDecimal() { Function<Object, BigDecimal> parser = s -> StringOptionUtil.parseDecimal(new StringOption(String.valueOf(s))); assertThat(parser.apply("0"), is(new BigDecimal("0"))); assertThat(parser.apply("1"), is(new BigDecimal("1"))); assertThat(parser.apply("+100"), is(new BigDecimal("100"))); assertThat(parser.apply("-100"), is(new BigDecimal("-100"))); assertThat(parser.apply("+3.14"), is(new BigDecimal("3.14"))); assertThat(parser.apply("-3.14"), is(new BigDecimal("-3.14"))); assertThat(parser.apply(Long.MAX_VALUE), is(new BigDecimal(Long.MAX_VALUE))); assertThat(parser.apply(Long.MIN_VALUE), is(new BigDecimal(Long.MIN_VALUE))); raise(() -> parser.apply("")); raise(() -> parser.apply("Hello, world!")); }
/** * parse long. */ @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))); }
/** * parse int. */ @Test public void parseInt() { Function<Object, Integer> parser = s -> StringOptionUtil.parseInt(new StringOption(String.valueOf(s))); assertThat(parser.apply("0"), is(0)); assertThat(parser.apply("1"), is(1)); assertThat(parser.apply("+100"), is(+100)); assertThat(parser.apply("-100"), is(-100)); assertThat(parser.apply(Integer.MAX_VALUE), is(Integer.MAX_VALUE)); assertThat(parser.apply(Integer.MIN_VALUE), is(Integer.MIN_VALUE)); raise(() -> parser.apply("")); raise(() -> parser.apply("Hello, world!")); raise(() -> parser.apply(Integer.MAX_VALUE + 1L)); raise(() -> parser.apply(Integer.MIN_VALUE - 1L)); }
/** * Appends the text in the second {@link StringOption} into the first one. * This directly modifies the first {@link StringOption}. * @param target the append target * @param contents the text contents to be appended * @throws NullPointerException if the {@link StringOption} is/represents {@code null} */ public static void append(StringOption target, String contents) { Text buffer = StringOption.BUFFER_POOL.get(); buffer.set(contents); append(target, buffer); }
CharBuffer buffer = DECODER_POOL.get().decode(contents.get()); if (buffer.hasRemaining() == false) { throw invalidNumber(contents); int column = Character.digit(c, 10); if (column < 0) { throw invalidNumber(contents); throw invalidNumber(contents); throw invalidNumber(contents); } else { if (negativeResult == Integer.MIN_VALUE) { throw invalidNumber(contents);
@Override protected void doWrite(StringOption property, ValueWriter writer) throws IOException { buffer.setLength(0); StringOptionUtil.append(buffer, property); writer.writeString(buffer); }
/** * Appends the text in the second {@link StringOption} into the first one. * This directly modifies the first {@link StringOption}. * @param target the append target * @param contents the text contents to be appended * @throws NullPointerException if the {@link StringOption} is/represents {@code null} */ public static void append(StringOption target, StringOption contents) { Text text = contents.get(); append(target, text); }
/** * append - string builder. */ @Test public void append_string_builder() { StringBuilder buf = new StringBuilder(); buf.append("<"); StringOptionUtil.append(buf, new StringOption("Hello, world!")); buf.append(">"); assertThat(buf.toString(), is("<Hello, world!>")); }