Skip to content

Commit

Permalink
common: Add #contains
Browse files Browse the repository at this point in the history
  • Loading branch information
kohlschuetter committed Dec 31, 2023
1 parent 6a03e27 commit 38ce857
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -742,8 +742,18 @@ default int indexOf(int c) {
}

/**
* Returns the index within this StringHolder of the first occurrence of the specified
* CharSequence, or {@code -1} if not found.
* Checks if this {@link StringHolder} contains the given {@link CharSequence}.
*
* @param s The char sequence to look for.
* @return {@code true} if found (also if the sequence is empty).
*/
default boolean contains(CharSequence s) {
return indexOf(s.toString()) >= 0;
}

/**
* Returns the index within this {@link StringHolder} of the first occurrence of the specified
* {@link CharSequence}, or {@code -1} if not found.
*
* @param str The char sequence to look for.
* @return The position, or {@code -1} if not found.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1423,4 +1423,10 @@ public void testIndexOfSelf() throws Exception {
sh = StringHolder.withSupplier(() -> "");
assertEquals(0, sh.indexOf(sh));
}

@Test
public void testContains() throws Exception {
assertTrue(StringHolder.withContent("Foo bar").contains("bar"));
assertFalse(StringHolder.withContent("Foo bar").contains("baz"));
}
}

0 comments on commit 38ce857

Please sign in to comment.