diff --git a/src/main/java/com/arangodb/ArangoIterable.java b/src/main/java/com/arangodb/ArangoIterable.java index 0323fa7a5..40ab262bc 100644 --- a/src/main/java/com/arangodb/ArangoIterable.java +++ b/src/main/java/com/arangodb/ArangoIterable.java @@ -21,6 +21,7 @@ package com.arangodb; import java.util.Collection; +import java.util.stream.Stream; /** * @author Mark Vollmary @@ -30,43 +31,60 @@ public interface ArangoIterable extends Iterable { @Override ArangoIterator iterator(); + Stream stream(); + /** * Performs the given action for each element of the {@code ArangoIterable} * - * @param action a action to perform on the elements + * @param action + * a action to perform on the elements + * @deprecated Use {@link #forEach(java.util.function.Consumer)} instead. */ + @Deprecated void foreach(Consumer action); /** * Returns a {@code ArangoIterable} consisting of the results of applying the given function to the elements of this * {@code ArangoIterable}. * - * @param mapper a function to apply to each element + * @param mapper + * a function to apply to each element * @return the new {@code ArangoIterable} + * + * @deprecated Use {@link #stream()} and {@link Stream#map(java.util.function.Function)} instead. */ + @Deprecated ArangoIterable map(Function mapper); /** * Returns a {@code ArangoIterable} consisting of the elements of this {@code ArangoIterable} that match the given * predicate. * - * @param predicate a predicate to apply to each element to determine if it should be included + * @param predicate + * a predicate to apply to each element to determine if it should be included * @return the new {@code ArangoIterable} + * + * @deprecated Use {@link #stream()} and {@link Stream#filter(java.util.function.Predicate)} instead. */ + @Deprecated ArangoIterable filter(Predicate predicate); /** * Returns the first element or {@code null} if no element exists. * * @return first element or {@code null} + * @deprecated Use {@link #stream()} and {@link Stream#findFirst()} instead. */ + @Deprecated T first(); /** * Returns the count of elements of this {@code ArangoIterable}. * * @return the count of elements + * @deprecated Use {@link #stream()} and {@link Stream#count()} instead. */ + @Deprecated long count(); /** @@ -75,7 +93,9 @@ public interface ArangoIterable extends Iterable { * @param predicate a predicate to apply to elements of this {@code ArangoIterable} * @return {@code true} if any elements of the {@code ArangoIterable} match the provided predicate, otherwise * {@code false} + * @deprecated Use {@link #stream()} and {@link Stream#anyMatch(java.util.function.Predicate)} instead. */ + @Deprecated boolean anyMatch(Predicate predicate); /** @@ -84,7 +104,9 @@ public interface ArangoIterable extends Iterable { * @param predicate a predicate to apply to elements of this {@code ArangoIterable} * @return {@code true} if all elements of the {@code ArangoIterable} match the provided predicate, otherwise * {@code false} + * @deprecated Use {@link #stream()} and {@link Stream#allMatch(java.util.function.Predicate)} instead. */ + @Deprecated boolean allMatch(Predicate predicate); /** @@ -93,7 +115,9 @@ public interface ArangoIterable extends Iterable { * @param predicate a predicate to apply to elements of this {@code ArangoIterable} * @return {@code true} if no elements of the {@code ArangoIterable} match the provided predicate, otherwise * {@code false} + * @deprecated Use {@link #stream()} and {@link Stream#noneMatch(java.util.function.Predicate)} instead. */ + @Deprecated boolean noneMatch(Predicate predicate); /** @@ -101,7 +125,9 @@ public interface ArangoIterable extends Iterable { * * @param target the collection to insert into * @return the filled target + * @deprecated Use {@link #stream()} and {@link Stream#collect} instead. */ + @Deprecated > R collectInto(R target); } diff --git a/src/main/java/com/arangodb/Consumer.java b/src/main/java/com/arangodb/Consumer.java index b4015bf2c..3edbe3c4e 100644 --- a/src/main/java/com/arangodb/Consumer.java +++ b/src/main/java/com/arangodb/Consumer.java @@ -23,7 +23,9 @@ /** * @param the type of the input to the operation * @author Mark Vollmary + * @deprecated Use {@link java.util.function.Consumer} instead. */ +@Deprecated public interface Consumer { /** diff --git a/src/main/java/com/arangodb/Predicate.java b/src/main/java/com/arangodb/Predicate.java index 5f0326618..4c7257862 100644 --- a/src/main/java/com/arangodb/Predicate.java +++ b/src/main/java/com/arangodb/Predicate.java @@ -23,7 +23,9 @@ /** * @param the type of the input to the predicate * @author Mark Vollmary + * @deprecated Use {@link java.util.function.Predicate} instead. */ +@Deprecated public interface Predicate { /** diff --git a/src/main/java/com/arangodb/internal/cursor/AbstractArangoIterable.java b/src/main/java/com/arangodb/internal/cursor/AbstractArangoIterable.java index e0c0c0565..93ede0a1b 100644 --- a/src/main/java/com/arangodb/internal/cursor/AbstractArangoIterable.java +++ b/src/main/java/com/arangodb/internal/cursor/AbstractArangoIterable.java @@ -27,12 +27,19 @@ import java.util.Collection; import java.util.Iterator; +import java.util.stream.Stream; +import java.util.stream.StreamSupport; /** * @author Mark Vollmary */ public abstract class AbstractArangoIterable implements ArangoIterable { + @Override + public Stream stream() { + return StreamSupport.stream(spliterator(), false); + } + @Override public ArangoIterable map(final Function mapper) { return new ArangoMappingIterable<>(this, mapper); diff --git a/src/main/java/com/arangodb/internal/cursor/ArangoFilterIterable.java b/src/main/java/com/arangodb/internal/cursor/ArangoFilterIterable.java index 9d26affe7..bb0ff1054 100644 --- a/src/main/java/com/arangodb/internal/cursor/ArangoFilterIterable.java +++ b/src/main/java/com/arangodb/internal/cursor/ArangoFilterIterable.java @@ -28,6 +28,7 @@ /** * @author Mark Vollmary */ +@Deprecated public class ArangoFilterIterable extends AbstractArangoIterable implements ArangoIterable { private final ArangoIterable iterable; diff --git a/src/main/java/com/arangodb/internal/cursor/ArangoMappingIterable.java b/src/main/java/com/arangodb/internal/cursor/ArangoMappingIterable.java index 0b0141576..c1b818c2b 100644 --- a/src/main/java/com/arangodb/internal/cursor/ArangoMappingIterable.java +++ b/src/main/java/com/arangodb/internal/cursor/ArangoMappingIterable.java @@ -28,6 +28,7 @@ /** * @author Mark Vollmary */ +@Deprecated public class ArangoMappingIterable extends AbstractArangoIterable implements ArangoIterable { private final ArangoIterable iterable; diff --git a/src/test/java/com/arangodb/ArangoCursorTest.java b/src/test/java/com/arangodb/ArangoCursorTest.java index d1ddc3ac6..ef7e4439f 100644 --- a/src/test/java/com/arangodb/ArangoCursorTest.java +++ b/src/test/java/com/arangodb/ArangoCursorTest.java @@ -30,8 +30,10 @@ import java.util.ArrayList; import java.util.HashSet; import java.util.List; +import java.util.Optional; import java.util.Set; import java.util.concurrent.atomic.AtomicLong; +import java.util.stream.Collectors; import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.*; @@ -60,6 +62,15 @@ public void first() { assertThat(first.getAsLong(), is(0L)); } + @Test + public void firstStream() { + final ArangoCursor cursor = db.query("FOR i IN 0..99 RETURN i", VPackSlice.class); + final Optional first = cursor.stream().findFirst(); + assertThat(first.isPresent(), is(true)); + assertThat(first.get().isInteger(), is(true)); + assertThat(first.get().getAsLong(), is(0L)); + } + @Test public void next() { @@ -78,6 +89,13 @@ public void mapFilterCount() { assertThat(count, is(50L)); } + @Test + public void mapFilterCountStream() { + final ArangoCursor cursor = db.query("FOR i IN 0..99 RETURN i", VPackSlice.class); + final long count = cursor.stream().map(VPackSlice::getAsLong).filter(t -> t < 50).count(); + assertThat(count, is(50L)); + } + @Test public void mapMapFilterCount() { final ArangoCursor cursor = db.query("FOR i IN 0..99 RETURN i", VPackSlice.class); @@ -122,6 +140,14 @@ public void mapFilterCollectIntoSet() { assertThat(target.size(), is(50)); } + @Test + public void mapFilterCollectIntoSetStream() { + final ArangoCursor cursor = db.query("FOR i IN 0..99 RETURN i", VPackSlice.class); + final Set target = cursor.stream().map(VPackSlice::getAsLong).filter(t -> t < 50).collect(Collectors.toSet()); + assertThat(target, is(not(nullValue()))); + assertThat(target.size(), is(50)); + } + @Test public void foreach() { final AtomicLong i = new AtomicLong(0L); @@ -129,6 +155,13 @@ public void foreach() { cursor.foreach(t -> assertThat(t.getAsLong(), is(i.getAndIncrement()))); } + @Test + public void forEach() { + final AtomicLong i = new AtomicLong(0L); + final ArangoCursor cursor = db.query("FOR i IN 0..99 RETURN i", VPackSlice.class); + cursor.forEach(t -> assertThat(t.getAsLong(), is(i.getAndIncrement()))); + } + @Test public void mapForeach() { final AtomicLong i = new AtomicLong(0L); @@ -136,6 +169,13 @@ public void mapForeach() { cursor.map(VPackSlice::getAsLong).foreach(t -> assertThat(t, is(i.getAndIncrement()))); } + @Test + public void mapForeachStream() { + final AtomicLong i = new AtomicLong(0L); + final ArangoCursor cursor = db.query("FOR i IN 0..99 RETURN i", VPackSlice.class); + cursor.stream().map(VPackSlice::getAsLong).forEach(t -> assertThat(t, is(i.getAndIncrement()))); + } + @Test public void mapFilterForeach() { final AtomicLong i = new AtomicLong(0L); @@ -143,6 +183,13 @@ public void mapFilterForeach() { cursor.map(VPackSlice::getAsLong).filter(t -> t < 50).foreach(t -> assertThat(t, is(i.getAndIncrement()))); } + @Test + public void mapFilterForEachStream() { + final AtomicLong i = new AtomicLong(0L); + final ArangoCursor cursor = db.query("FOR i IN 0..99 RETURN i", VPackSlice.class); + cursor.stream().map(VPackSlice::getAsLong).filter(t -> t < 50).forEach(t -> assertThat(t, is(i.getAndIncrement()))); + } + @Test public void anyMatch() { final ArangoCursor cursor = db.query("FOR i IN 0..99 RETURN i", VPackSlice.class); @@ -150,6 +197,13 @@ public void anyMatch() { assertThat(match, is(true)); } + @Test + public void anyMatchStream() { + final ArangoCursor cursor = db.query("FOR i IN 0..99 RETURN i", VPackSlice.class); + final boolean match = cursor.stream().anyMatch(t -> t.getAsLong() == 50L); + assertThat(match, is(true)); + } + @Test public void mapAnyMatch() { final ArangoCursor cursor = db.query("FOR i IN 0..99 RETURN i", VPackSlice.class); @@ -171,6 +225,13 @@ public void noneMatch() { assertThat(match, is(true)); } + @Test + public void noneMatchStream() { + final ArangoCursor cursor = db.query("FOR i IN 0..99 RETURN i", VPackSlice.class); + final boolean match = cursor.stream().noneMatch(t -> t.getAsLong() == 100L); + assertThat(match, is(true)); + } + @Test public void mapNoneMatch() { final ArangoCursor cursor = db.query("FOR i IN 0..99 RETURN i", VPackSlice.class); @@ -192,6 +253,13 @@ public void allMatch() { assertThat(match, is(true)); } + @Test + public void allMatchStream() { + final ArangoCursor cursor = db.query("FOR i IN 0..99 RETURN i", VPackSlice.class); + final boolean match = cursor.stream().allMatch(t -> t.getAsLong() < 100L); + assertThat(match, is(true)); + } + @Test public void mapAllMatch() { final ArangoCursor cursor = db.query("FOR i IN 0..99 RETURN i", VPackSlice.class);