Skip to content

Deprecated ArangoIterable methods in favour of Java 8 Stream equivalents #382

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 12, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 29 additions & 3 deletions src/main/java/com/arangodb/ArangoIterable.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
package com.arangodb;

import java.util.Collection;
import java.util.stream.Stream;

/**
* @author Mark Vollmary
Expand All @@ -30,43 +31,60 @@ public interface ArangoIterable<T> extends Iterable<T> {
@Override
ArangoIterator<T> iterator();

Stream<T> 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<? super T> 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
<R> ArangoIterable<R> map(Function<? super T, ? extends R> 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<T> filter(Predicate<? super T> 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();

/**
Expand All @@ -75,7 +93,9 @@ public interface ArangoIterable<T> extends Iterable<T> {
* @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<? super T> predicate);

/**
Expand All @@ -84,7 +104,9 @@ public interface ArangoIterable<T> extends Iterable<T> {
* @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<? super T> predicate);

/**
Expand All @@ -93,15 +115,19 @@ public interface ArangoIterable<T> extends Iterable<T> {
* @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<? super T> predicate);

/**
* Iterates over all elements of this {@code ArangoIterable} and adds each to the given target.
*
* @param target the collection to insert into
* @return the filled target
* @deprecated Use {@link #stream()} and {@link Stream#collect} instead.
*/
@Deprecated
<R extends Collection<? super T>> R collectInto(R target);

}
2 changes: 2 additions & 0 deletions src/main/java/com/arangodb/Consumer.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@
/**
* @param <T> the type of the input to the operation
* @author Mark Vollmary
* @deprecated Use {@link java.util.function.Consumer} instead.
*/
@Deprecated
public interface Consumer<T> {

/**
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/com/arangodb/Predicate.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@
/**
* @param <T> the type of the input to the predicate
* @author Mark Vollmary
* @deprecated Use {@link java.util.function.Predicate} instead.
*/
@Deprecated
public interface Predicate<T> {

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<T> implements ArangoIterable<T> {

@Override
public Stream<T> stream() {
return StreamSupport.stream(spliterator(), false);
}

@Override
public <R> ArangoIterable<R> map(final Function<? super T, ? extends R> mapper) {
return new ArangoMappingIterable<>(this, mapper);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
/**
* @author Mark Vollmary
*/
@Deprecated
public class ArangoFilterIterable<T> extends AbstractArangoIterable<T> implements ArangoIterable<T> {

private final ArangoIterable<T> iterable;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
/**
* @author Mark Vollmary
*/
@Deprecated
public class ArangoMappingIterable<R, T> extends AbstractArangoIterable<T> implements ArangoIterable<T> {

private final ArangoIterable<R> iterable;
Expand Down
68 changes: 68 additions & 0 deletions src/test/java/com/arangodb/ArangoCursorTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.*;
Expand Down Expand Up @@ -60,6 +62,15 @@ public void first() {
assertThat(first.getAsLong(), is(0L));
}

@Test
public void firstStream() {
final ArangoCursor<VPackSlice> cursor = db.query("FOR i IN 0..99 RETURN i", VPackSlice.class);
final Optional<VPackSlice> 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() {

Expand All @@ -78,6 +89,13 @@ public void mapFilterCount() {
assertThat(count, is(50L));
}

@Test
public void mapFilterCountStream() {
final ArangoCursor<VPackSlice> 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<VPackSlice> cursor = db.query("FOR i IN 0..99 RETURN i", VPackSlice.class);
Expand Down Expand Up @@ -122,34 +140,70 @@ public void mapFilterCollectIntoSet() {
assertThat(target.size(), is(50));
}

@Test
public void mapFilterCollectIntoSetStream() {
final ArangoCursor<VPackSlice> cursor = db.query("FOR i IN 0..99 RETURN i", VPackSlice.class);
final Set<Long> 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);
final ArangoCursor<VPackSlice> cursor = db.query("FOR i IN 0..99 RETURN i", VPackSlice.class);
cursor.foreach(t -> assertThat(t.getAsLong(), is(i.getAndIncrement())));
}

@Test
public void forEach() {
final AtomicLong i = new AtomicLong(0L);
final ArangoCursor<VPackSlice> 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);
final ArangoCursor<VPackSlice> cursor = db.query("FOR i IN 0..99 RETURN i", VPackSlice.class);
cursor.map(VPackSlice::getAsLong).foreach(t -> assertThat(t, is(i.getAndIncrement())));
}

@Test
public void mapForeachStream() {
final AtomicLong i = new AtomicLong(0L);
final ArangoCursor<VPackSlice> 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);
final ArangoCursor<VPackSlice> cursor = db.query("FOR i IN 0..99 RETURN i", VPackSlice.class);
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<VPackSlice> 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<VPackSlice> cursor = db.query("FOR i IN 0..99 RETURN i", VPackSlice.class);
final boolean match = cursor.anyMatch(t -> t.getAsLong() == 50L);
assertThat(match, is(true));
}

@Test
public void anyMatchStream() {
final ArangoCursor<VPackSlice> 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<VPackSlice> cursor = db.query("FOR i IN 0..99 RETURN i", VPackSlice.class);
Expand All @@ -171,6 +225,13 @@ public void noneMatch() {
assertThat(match, is(true));
}

@Test
public void noneMatchStream() {
final ArangoCursor<VPackSlice> 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<VPackSlice> cursor = db.query("FOR i IN 0..99 RETURN i", VPackSlice.class);
Expand All @@ -192,6 +253,13 @@ public void allMatch() {
assertThat(match, is(true));
}

@Test
public void allMatchStream() {
final ArangoCursor<VPackSlice> 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<VPackSlice> cursor = db.query("FOR i IN 0..99 RETURN i", VPackSlice.class);
Expand Down