Skip to content

Commit 9cde35c

Browse files
authored
deprecated ArangoIterable methods in favour of Java 8 Stream equivalents (#382)
1 parent bf16b41 commit 9cde35c

File tree

7 files changed

+110
-3
lines changed

7 files changed

+110
-3
lines changed

src/main/java/com/arangodb/ArangoIterable.java

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
package com.arangodb;
2222

2323
import java.util.Collection;
24+
import java.util.stream.Stream;
2425

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

34+
Stream<T> stream();
35+
3336
/**
3437
* Performs the given action for each element of the {@code ArangoIterable}
3538
*
36-
* @param action a action to perform on the elements
39+
* @param action
40+
* a action to perform on the elements
41+
* @deprecated Use {@link #forEach(java.util.function.Consumer)} instead.
3742
*/
43+
@Deprecated
3844
void foreach(Consumer<? super T> action);
3945

4046
/**
4147
* Returns a {@code ArangoIterable} consisting of the results of applying the given function to the elements of this
4248
* {@code ArangoIterable}.
4349
*
44-
* @param mapper a function to apply to each element
50+
* @param mapper
51+
* a function to apply to each element
4552
* @return the new {@code ArangoIterable}
53+
*
54+
* @deprecated Use {@link #stream()} and {@link Stream#map(java.util.function.Function)} instead.
4655
*/
56+
@Deprecated
4757
<R> ArangoIterable<R> map(Function<? super T, ? extends R> mapper);
4858

4959
/**
5060
* Returns a {@code ArangoIterable} consisting of the elements of this {@code ArangoIterable} that match the given
5161
* predicate.
5262
*
53-
* @param predicate a predicate to apply to each element to determine if it should be included
63+
* @param predicate
64+
* a predicate to apply to each element to determine if it should be included
5465
* @return the new {@code ArangoIterable}
66+
*
67+
* @deprecated Use {@link #stream()} and {@link Stream#filter(java.util.function.Predicate)} instead.
5568
*/
69+
@Deprecated
5670
ArangoIterable<T> filter(Predicate<? super T> predicate);
5771

5872
/**
5973
* Returns the first element or {@code null} if no element exists.
6074
*
6175
* @return first element or {@code null}
76+
* @deprecated Use {@link #stream()} and {@link Stream#findFirst()} instead.
6277
*/
78+
@Deprecated
6379
T first();
6480

6581
/**
6682
* Returns the count of elements of this {@code ArangoIterable}.
6783
*
6884
* @return the count of elements
85+
* @deprecated Use {@link #stream()} and {@link Stream#count()} instead.
6986
*/
87+
@Deprecated
7088
long count();
7189

7290
/**
@@ -75,7 +93,9 @@ public interface ArangoIterable<T> extends Iterable<T> {
7593
* @param predicate a predicate to apply to elements of this {@code ArangoIterable}
7694
* @return {@code true} if any elements of the {@code ArangoIterable} match the provided predicate, otherwise
7795
* {@code false}
96+
* @deprecated Use {@link #stream()} and {@link Stream#anyMatch(java.util.function.Predicate)} instead.
7897
*/
98+
@Deprecated
7999
boolean anyMatch(Predicate<? super T> predicate);
80100

81101
/**
@@ -84,7 +104,9 @@ public interface ArangoIterable<T> extends Iterable<T> {
84104
* @param predicate a predicate to apply to elements of this {@code ArangoIterable}
85105
* @return {@code true} if all elements of the {@code ArangoIterable} match the provided predicate, otherwise
86106
* {@code false}
107+
* @deprecated Use {@link #stream()} and {@link Stream#allMatch(java.util.function.Predicate)} instead.
87108
*/
109+
@Deprecated
88110
boolean allMatch(Predicate<? super T> predicate);
89111

90112
/**
@@ -93,15 +115,19 @@ public interface ArangoIterable<T> extends Iterable<T> {
93115
* @param predicate a predicate to apply to elements of this {@code ArangoIterable}
94116
* @return {@code true} if no elements of the {@code ArangoIterable} match the provided predicate, otherwise
95117
* {@code false}
118+
* @deprecated Use {@link #stream()} and {@link Stream#noneMatch(java.util.function.Predicate)} instead.
96119
*/
120+
@Deprecated
97121
boolean noneMatch(Predicate<? super T> predicate);
98122

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

107133
}

src/main/java/com/arangodb/Consumer.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,9 @@
2323
/**
2424
* @param <T> the type of the input to the operation
2525
* @author Mark Vollmary
26+
* @deprecated Use {@link java.util.function.Consumer} instead.
2627
*/
28+
@Deprecated
2729
public interface Consumer<T> {
2830

2931
/**

src/main/java/com/arangodb/Predicate.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,9 @@
2323
/**
2424
* @param <T> the type of the input to the predicate
2525
* @author Mark Vollmary
26+
* @deprecated Use {@link java.util.function.Predicate} instead.
2627
*/
28+
@Deprecated
2729
public interface Predicate<T> {
2830

2931
/**

src/main/java/com/arangodb/internal/cursor/AbstractArangoIterable.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,19 @@
2727

2828
import java.util.Collection;
2929
import java.util.Iterator;
30+
import java.util.stream.Stream;
31+
import java.util.stream.StreamSupport;
3032

3133
/**
3234
* @author Mark Vollmary
3335
*/
3436
public abstract class AbstractArangoIterable<T> implements ArangoIterable<T> {
3537

38+
@Override
39+
public Stream<T> stream() {
40+
return StreamSupport.stream(spliterator(), false);
41+
}
42+
3643
@Override
3744
public <R> ArangoIterable<R> map(final Function<? super T, ? extends R> mapper) {
3845
return new ArangoMappingIterable<>(this, mapper);

src/main/java/com/arangodb/internal/cursor/ArangoFilterIterable.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
/**
2929
* @author Mark Vollmary
3030
*/
31+
@Deprecated
3132
public class ArangoFilterIterable<T> extends AbstractArangoIterable<T> implements ArangoIterable<T> {
3233

3334
private final ArangoIterable<T> iterable;

src/main/java/com/arangodb/internal/cursor/ArangoMappingIterable.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
/**
2929
* @author Mark Vollmary
3030
*/
31+
@Deprecated
3132
public class ArangoMappingIterable<R, T> extends AbstractArangoIterable<T> implements ArangoIterable<T> {
3233

3334
private final ArangoIterable<R> iterable;

src/test/java/com/arangodb/ArangoCursorTest.java

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,10 @@
3030
import java.util.ArrayList;
3131
import java.util.HashSet;
3232
import java.util.List;
33+
import java.util.Optional;
3334
import java.util.Set;
3435
import java.util.concurrent.atomic.AtomicLong;
36+
import java.util.stream.Collectors;
3537

3638
import static org.hamcrest.MatcherAssert.assertThat;
3739
import static org.hamcrest.Matchers.*;
@@ -60,6 +62,15 @@ public void first() {
6062
assertThat(first.getAsLong(), is(0L));
6163
}
6264

65+
@Test
66+
public void firstStream() {
67+
final ArangoCursor<VPackSlice> cursor = db.query("FOR i IN 0..99 RETURN i", VPackSlice.class);
68+
final Optional<VPackSlice> first = cursor.stream().findFirst();
69+
assertThat(first.isPresent(), is(true));
70+
assertThat(first.get().isInteger(), is(true));
71+
assertThat(first.get().getAsLong(), is(0L));
72+
}
73+
6374
@Test
6475
public void next() {
6576

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

92+
@Test
93+
public void mapFilterCountStream() {
94+
final ArangoCursor<VPackSlice> cursor = db.query("FOR i IN 0..99 RETURN i", VPackSlice.class);
95+
final long count = cursor.stream().map(VPackSlice::getAsLong).filter(t -> t < 50).count();
96+
assertThat(count, is(50L));
97+
}
98+
8199
@Test
82100
public void mapMapFilterCount() {
83101
final ArangoCursor<VPackSlice> cursor = db.query("FOR i IN 0..99 RETURN i", VPackSlice.class);
@@ -122,34 +140,70 @@ public void mapFilterCollectIntoSet() {
122140
assertThat(target.size(), is(50));
123141
}
124142

143+
@Test
144+
public void mapFilterCollectIntoSetStream() {
145+
final ArangoCursor<VPackSlice> cursor = db.query("FOR i IN 0..99 RETURN i", VPackSlice.class);
146+
final Set<Long> target = cursor.stream().map(VPackSlice::getAsLong).filter(t -> t < 50).collect(Collectors.toSet());
147+
assertThat(target, is(not(nullValue())));
148+
assertThat(target.size(), is(50));
149+
}
150+
125151
@Test
126152
public void foreach() {
127153
final AtomicLong i = new AtomicLong(0L);
128154
final ArangoCursor<VPackSlice> cursor = db.query("FOR i IN 0..99 RETURN i", VPackSlice.class);
129155
cursor.foreach(t -> assertThat(t.getAsLong(), is(i.getAndIncrement())));
130156
}
131157

158+
@Test
159+
public void forEach() {
160+
final AtomicLong i = new AtomicLong(0L);
161+
final ArangoCursor<VPackSlice> cursor = db.query("FOR i IN 0..99 RETURN i", VPackSlice.class);
162+
cursor.forEach(t -> assertThat(t.getAsLong(), is(i.getAndIncrement())));
163+
}
164+
132165
@Test
133166
public void mapForeach() {
134167
final AtomicLong i = new AtomicLong(0L);
135168
final ArangoCursor<VPackSlice> cursor = db.query("FOR i IN 0..99 RETURN i", VPackSlice.class);
136169
cursor.map(VPackSlice::getAsLong).foreach(t -> assertThat(t, is(i.getAndIncrement())));
137170
}
138171

172+
@Test
173+
public void mapForeachStream() {
174+
final AtomicLong i = new AtomicLong(0L);
175+
final ArangoCursor<VPackSlice> cursor = db.query("FOR i IN 0..99 RETURN i", VPackSlice.class);
176+
cursor.stream().map(VPackSlice::getAsLong).forEach(t -> assertThat(t, is(i.getAndIncrement())));
177+
}
178+
139179
@Test
140180
public void mapFilterForeach() {
141181
final AtomicLong i = new AtomicLong(0L);
142182
final ArangoCursor<VPackSlice> cursor = db.query("FOR i IN 0..99 RETURN i", VPackSlice.class);
143183
cursor.map(VPackSlice::getAsLong).filter(t -> t < 50).foreach(t -> assertThat(t, is(i.getAndIncrement())));
144184
}
145185

186+
@Test
187+
public void mapFilterForEachStream() {
188+
final AtomicLong i = new AtomicLong(0L);
189+
final ArangoCursor<VPackSlice> cursor = db.query("FOR i IN 0..99 RETURN i", VPackSlice.class);
190+
cursor.stream().map(VPackSlice::getAsLong).filter(t -> t < 50).forEach(t -> assertThat(t, is(i.getAndIncrement())));
191+
}
192+
146193
@Test
147194
public void anyMatch() {
148195
final ArangoCursor<VPackSlice> cursor = db.query("FOR i IN 0..99 RETURN i", VPackSlice.class);
149196
final boolean match = cursor.anyMatch(t -> t.getAsLong() == 50L);
150197
assertThat(match, is(true));
151198
}
152199

200+
@Test
201+
public void anyMatchStream() {
202+
final ArangoCursor<VPackSlice> cursor = db.query("FOR i IN 0..99 RETURN i", VPackSlice.class);
203+
final boolean match = cursor.stream().anyMatch(t -> t.getAsLong() == 50L);
204+
assertThat(match, is(true));
205+
}
206+
153207
@Test
154208
public void mapAnyMatch() {
155209
final ArangoCursor<VPackSlice> cursor = db.query("FOR i IN 0..99 RETURN i", VPackSlice.class);
@@ -171,6 +225,13 @@ public void noneMatch() {
171225
assertThat(match, is(true));
172226
}
173227

228+
@Test
229+
public void noneMatchStream() {
230+
final ArangoCursor<VPackSlice> cursor = db.query("FOR i IN 0..99 RETURN i", VPackSlice.class);
231+
final boolean match = cursor.stream().noneMatch(t -> t.getAsLong() == 100L);
232+
assertThat(match, is(true));
233+
}
234+
174235
@Test
175236
public void mapNoneMatch() {
176237
final ArangoCursor<VPackSlice> cursor = db.query("FOR i IN 0..99 RETURN i", VPackSlice.class);
@@ -192,6 +253,13 @@ public void allMatch() {
192253
assertThat(match, is(true));
193254
}
194255

256+
@Test
257+
public void allMatchStream() {
258+
final ArangoCursor<VPackSlice> cursor = db.query("FOR i IN 0..99 RETURN i", VPackSlice.class);
259+
final boolean match = cursor.stream().allMatch(t -> t.getAsLong() < 100L);
260+
assertThat(match, is(true));
261+
}
262+
195263
@Test
196264
public void mapAllMatch() {
197265
final ArangoCursor<VPackSlice> cursor = db.query("FOR i IN 0..99 RETURN i", VPackSlice.class);

0 commit comments

Comments
 (0)