Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit b856c56

Browse files
committed
Custom operator example
1 parent 1b90c2b commit b856c56

File tree

4 files changed

+75
-1
lines changed

4 files changed

+75
-1
lines changed
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package com.annimon.java8streamexample;
2+
3+
import com.annimon.stream.Stream;
4+
import com.annimon.stream.function.Function;
5+
import java.util.Iterator;
6+
7+
public final class CustomOperators {
8+
9+
public static <T> Index<T> index() {
10+
return index(0);
11+
}
12+
13+
public static <T> Index<T> index(int from) {
14+
return new Index<>(from);
15+
}
16+
17+
private static class Index<T> implements Function<Stream<T>, Stream<IntPair<T>>> {
18+
19+
private final int startFrom;
20+
21+
public Index(int startFrom) {
22+
this.startFrom = startFrom;
23+
}
24+
25+
@Override
26+
public Stream<IntPair<T>> apply(Stream<T> stream) {
27+
final Iterator<? extends T> iterator = stream.iterator();
28+
return Stream.of(new Iterator<IntPair<T>>() {
29+
30+
private int index = startFrom;
31+
32+
@Override
33+
public boolean hasNext() {
34+
return iterator.hasNext();
35+
}
36+
37+
@Override
38+
public IntPair<T> next() {
39+
return new IntPair<>(index++, iterator.next());
40+
}
41+
42+
@Override
43+
public void remove() {
44+
throw new UnsupportedOperationException();
45+
}
46+
});
47+
}
48+
}
49+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.annimon.java8streamexample;
2+
3+
public class IntPair<T> {
4+
5+
private final int index;
6+
private final T object;
7+
8+
public IntPair(int index, T object) {
9+
this.index = index;
10+
this.object = object;
11+
}
12+
13+
public int getIndex() {
14+
return index;
15+
}
16+
17+
public T getObject() {
18+
return object;
19+
}
20+
}

‎app/src/main/java/com/annimon/java8streamexample/MainActivity.java‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
import com.annimon.stream.IntStream;
1212
import com.annimon.stream.RandomCompat;
1313
import com.annimon.stream.Stream;
14-
import java.util.Random;
1514

1615
public final class MainActivity extends ActionBarActivity {
1716

@@ -119,6 +118,10 @@ private void action(String action) {
119118
.mapToObj(i -> String.format("%d. %s", i+1, mAdapter.getItem(i).getWord()))
120119
.map(str -> new Word(str, ""));
121120
break;
121+
case "add index custom op":
122+
stream = stream.custom(CustomOperators.index(1))
123+
.map(p -> new Word(String.format("%d. %s", p.getIndex(), p.getObject()), ""));
124+
break;
122125
case "skip %N":
123126
stream = stream.skip(filterValue);
124127
break;

‎app/src/main/res/values/strings.xml‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
<item>Filter N symbols</item>
1717
<item>Is contains "ok"?</item>
1818
<item>Add index</item>
19+
<item>Add index with custom operator</item>
1920
<item>Skip first N rows</item>
2021
<item>Limit N rows</item>
2122
<item>Group by first character</item>
@@ -32,6 +33,7 @@
3233
<item>filter %N length</item>
3334
<item>contains ok</item>
3435
<item>add index</item>
36+
<item>add index custom op</item>
3537
<item>skip %N</item>
3638
<item>limit %N</item>
3739
<item>group</item>

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /