--- /dev/null
+/*******************************************************************************
+ * Copyright (c) 2016 EfficiOS Inc., Alexandre Montplaisir
+ *
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *******************************************************************************/
+
+package org.eclipse.tracecompass.common.core.tests;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
+
+import java.util.Iterator;
+import java.util.List;
+
+import org.eclipse.tracecompass.common.core.StreamUtils;
+import org.junit.Test;
+
+import com.google.common.collect.ImmutableList;
+
+/**
+ * Tests for {@link StreamUtils} methods.
+ *
+ * @author Alexandre Montplaisir
+ */
+public class StreamUtilsTest {
+
+ /**
+ * Test {@link StreamUtils#getStream(Iterator)}.
+ */
+ @Test
+ public void testGetStreamIterator() {
+ List<Integer> list = ImmutableList.of(1, 2, 3, 4, 5);
+
+ /* Test short-circuiting terminal operation */
+ Iterator<Integer> iter = list.iterator();
+ boolean test = StreamUtils.getStream(iter)
+ .anyMatch(value -> value == 3);
+ assertTrue(test);
+
+ /*
+ * Test that the short-circuiting operation stopped its iteration where
+ * it needed to.
+ */
+ assertTrue(iter.hasNext());
+ assertEquals(4, iter.next().intValue());
+
+ /* Test fully-consuming terminal operation */
+ iter = list.iterator();
+ int sum = StreamUtils.getStream(iter)
+ .mapToInt(Integer::intValue)
+ .sum();
+ assertEquals(15, sum);
+ assertFalse(iter.hasNext());
+ }
+}
Bundle-ManifestVersion: 2
Bundle-Name: %Bundle-Name
Bundle-Vendor: %Bundle-Vendor
-Bundle-Version: 2.1.0.qualifier
+Bundle-Version: 2.2.0.qualifier
Bundle-Localization: plugin
Bundle-SymbolicName: org.eclipse.tracecompass.common.core;singleton:=true
Bundle-Activator: org.eclipse.tracecompass.internal.common.core.Activator
import static org.eclipse.tracecompass.common.core.NonNullUtils.checkNotNull;
+import java.util.Iterator;
+import java.util.Spliterator;
+import java.util.Spliterators;
import java.util.function.Function;
import java.util.stream.Stream;
import java.util.stream.StreamSupport;
return StreamSupport.stream(iterable.spliterator(), false);
}
+ /**
+ * Get a {@link Stream} from a generic {@link Iterator}.
+ *
+ * Depending on the type of terminal operation used on the stream, the
+ * iterator may or may not have some elements remaining. Be wary if you
+ * re-use the same iterator afterwards.
+ *
+ * @param iterator
+ * The iterator to wrap
+ * @return A stream containing the iterator's elements
+ * @since 2.2
+ */
+ public static <T> Stream<T> getStream(Iterator<T> iterator) {
+ Spliterator<T> spliterator = Spliterators.spliteratorUnknownSize(iterator, 0);
+ return StreamSupport.stream(spliterator, false);
+ }
+
/**
* Generic utility class to "flatten" a data structure using the
* {@link Stream} API.