diff --git a/platform/platform-tests/testSrc/com/intellij/util/containers/TreeTraverserTest.java b/platform/platform-tests/testSrc/com/intellij/util/containers/TreeTraverserTest.java index 254b5f838ade..bc13afddbc37 100644 --- a/platform/platform-tests/testSrc/com/intellij/util/containers/TreeTraverserTest.java +++ b/platform/platform-tests/testSrc/com/intellij/util/containers/TreeTraverserTest.java @@ -377,9 +377,10 @@ public class TreeTraverserTest extends TestCase { } public void testJoin() { + assertNull(JBIterable.of().join(", ").reduce((a, b) -> a + b)); assertEquals("", JBIterable.of().join(", ").reduce("", (a, b) -> a + b)); - assertEquals("a", JBIterable.of("a").join(", ").reduce("", (a, b) -> a + b)); - assertEquals("a, b, c", JBIterable.of("a", "b", "c").join(", ").reduce("", (a, b) -> a + b)); + assertEquals("a", JBIterable.of("a").join(", ").reduce((a, b) -> a + b)); + assertEquals("a, b, c", JBIterable.of("a", "b", "c").join(", ").reduce((a, b) -> a + b)); } public void testSplits1() { diff --git a/platform/util/src/com/intellij/util/containers/JBIterable.java b/platform/util/src/com/intellij/util/containers/JBIterable.java index e14cee450995..50d76652129e 100644 --- a/platform/util/src/com/intellij/util/containers/JBIterable.java +++ b/platform/util/src/com/intellij/util/containers/JBIterable.java @@ -100,7 +100,7 @@ public abstract class JBIterable implements Iterable { Multi(Iterable iterable) { super(iterable);} public Iterator iterator() { - return ((Iterable)content).iterator(); + return JBIterator.from(((Iterable)content).iterator()); } } @@ -571,24 +571,6 @@ public abstract class JBIterable implements Iterable { return iterator.hasNext() ? iterator.next() : null; } - /** - * Returns the first element if it is an instance of the specified class, otherwise null. - */ - @Nullable - public final T first(@NotNull Class type) { - E first = first(); - return first != null && type.isInstance(first) ? (T)first : null; - } - - /** - * Returns the first element if it satisfies the condition, otherwise null. - */ - @Nullable - public final E first(@NotNull Condition condition) { - E first = first(); - return first != null && condition.value(first) ? first : null; - } - /** * Returns the first element if it is the only one, otherwise null. */ @@ -639,7 +621,20 @@ public abstract class JBIterable implements Iterable { } /** - * Returns the index of the first matching element. + * Perform calculation over this iterable. + */ + public final E reduce(@NotNull PairFunction function) { + boolean first = true; + E cur = null; + for (E e : this) { + if (first) { cur = e; first = false; } + else cur = function.fun(cur, e); + } + return cur; + } + + /** + * Returns the the first matching element. */ public final E find(@NotNull Condition condition) { return filter(condition).first();