/** * Returns whether or not this text starts with the given prefix. * @param prefix the prefix * @return {@code true} if this has the given prefix or the prefix is empty, otherwise {@code false} * @throws NullPointerException if this or the prefix is {@code null} * @since 0.9.1 */ public boolean startsWith(String prefix) { if (isNull() || prefix == null) { throw new NullPointerException(); } if (prefix.isEmpty()) { return true; } Text buffer = BUFFER_POOL.get(); buffer.set(prefix); return startsWith(entity, buffer); }
/** * Returns whether or not this text starts with the given prefix. * @param prefix the prefix * @return {@code true} if this has the given prefix or the prefix is empty, otherwise {@code false} * @throws NullPointerException if this or the prefix is {@code null} * @since 0.9.1 */ public boolean startsWith(StringOption prefix) { if (isNull() || prefix == null || prefix.isNull()) { throw new NullPointerException(); } if (prefix.isEmpty()) { return true; } return startsWith(entity, prefix.entity); }
/** * startsWith. */ @Test public void startsWith() { npe(() -> new StringOption().startsWith("a")); npe(() -> new StringOption().startsWith(new StringOption("a"))); npe(() -> new StringOption("a").startsWith(new StringOption())); BiPredicate<String, String> tester = (string, prefix) -> { StringOption opt = new StringOption(string); boolean b0 = opt.startsWith(prefix); boolean b1 = opt.startsWith(new StringOption(prefix)); assertThat(b0, is(b1)); return b0; }; assertThat(tester.test("Hello", "Hello"), is(true)); assertThat(tester.test("Hello", ""), is(true)); assertThat(tester.test("Hello, world!", "a"), is(false)); assertThat(tester.test("Hello, world!", "Hello"), is(true)); assertThat(tester.test("Hello, world!", "world!"), is(false)); assertThat(tester.test("Hello", "Hello, world!"), is(false)); assertThat(tester.test("Hello, world!", "Hello!"), is(false)); assertThat(tester.test("Hello, world!", "world?"), is(false)); assertThat(tester.test("Hello, world!", "Hello, world?"), is(false)); }