Skip to content

Commit c6cc4ea

Browse files
committed
Add U.containsWith(iterable, element).
1 parent afc3f13 commit c6cc4ea

File tree

4 files changed

+40
-0
lines changed

4 files changed

+40
-0
lines changed

src/main/java/com/github/underscore/U.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -652,6 +652,19 @@ public boolean contains(final T elem) {
652652
return contains(iterable, elem);
653653
}
654654

655+
public static <E> boolean containsWith(final Iterable<E> iterable, final E elem) {
656+
return some(iterable, new Predicate<E>() {
657+
@Override
658+
public boolean test(E e) {
659+
return elem == null ? e == null : String.valueOf(e).contains(String.valueOf(elem));
660+
}
661+
});
662+
}
663+
664+
public boolean containsWith(final T elem) {
665+
return containsWith(iterable, elem);
666+
}
667+
655668
public static <E> boolean contains(final Iterable<E> iterable, final E elem, final int fromIndex) {
656669
final List<E> list = newArrayList(iterable);
657670
return contains(list.subList(fromIndex, list.size()), elem);
@@ -2949,6 +2962,10 @@ public Chain<Boolean> contains(final T elem) {
29492962
return new Chain<Boolean>(U.contains(list, elem));
29502963
}
29512964

2965+
public Chain<Boolean> containsWith(final T elem) {
2966+
return new Chain<Boolean>(U.containsWith(list, elem));
2967+
}
2968+
29522969
public Chain<T> invoke(final String methodName, final List<Object> args) {
29532970
return new Chain<T>(U.invoke(list, methodName, args));
29542971
}

src/main/java/com/github/underscore/lodash/U.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -316,6 +316,10 @@ public Chain<Boolean> contains(final T elem) {
316316
return new Chain<Boolean>(U.contains(value(), elem));
317317
}
318318

319+
public Chain<Boolean> containsWith(final T elem) {
320+
return new Chain<Boolean>(U.containsWith(value(), elem));
321+
}
322+
319323
public Chain<T> invoke(final String methodName, final List<Object> args) {
320324
return new Chain<T>(U.invoke(value(), methodName, args));
321325
}

src/test/java/com/github/underscore/CollectionsTest.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1036,6 +1036,24 @@ public void contains() {
10361036
assertTrue(result4);
10371037
}
10381038

1039+
/*
1040+
_.containsWith(["abc", "bcd", "cde"], "bc");
1041+
=> true
1042+
*/
1043+
@Test
1044+
public void containsWith() {
1045+
final boolean result = U.containsWith(asList(1, 2, 3), 3);
1046+
assertTrue(result);
1047+
final boolean resultObj = new U<Integer>(asList(1, 2, 3)).containsWith(3);
1048+
assertTrue(resultObj);
1049+
final boolean resultChain = U.chain(asList(1, 2, 3)).containsWith(3).item();
1050+
assertTrue(resultChain);
1051+
final boolean result2 = U.containsWith(asList(1, 2, null), null);
1052+
assertTrue(result2);
1053+
final boolean result3 = U.containsWith(asList("abc", "bcd", "cde"), "bc");
1054+
assertTrue(result3);
1055+
}
1056+
10391057
@Test
10401058
public void containsAtLeast() {
10411059
final boolean result = U.containsAtLeast(asList(1, 2, 2), 2, 2);

src/test/java/com/github/underscore/lodash/LodashTest.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -934,6 +934,7 @@ public void accept(String str) {
934934
U.chain(new String[] {""}).count(new Predicate<String>() {
935935
public boolean test(String str) { return true; } });
936936
U.chain(new String[] {""}).contains("");
937+
U.chain(new String[] {""}).containsWith("");
937938
U.chain(new String[] {""}).invoke("toString", Collections.emptyList());
938939
U.chain(new String[] {""}).invoke("toString");
939940
U.chain(new String[] {""}).pluck("toString");

0 commit comments

Comments
 (0)