Movatterモバイル変換


[0]ホーム

URL:


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

Commit01811e5

Browse files
committed
Add Stream.mapMulti and Stream.mapMultiTo*
1 parenta3690f0 commit01811e5

File tree

5 files changed

+286
-0
lines changed

5 files changed

+286
-0
lines changed

‎stream/src/main/java/com/annimon/stream/Stream.java‎

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
importcom.annimon.stream.internal.Compose;
55
importcom.annimon.stream.internal.Operators;
66
importcom.annimon.stream.internal.Params;
7+
importcom.annimon.stream.internal.SpinedBuffer;
78
importcom.annimon.stream.iterator.IndexedIterator;
89
importcom.annimon.stream.iterator.LazyIterator;
910
importcom.annimon.stream.operator.*;
@@ -1034,6 +1035,123 @@ public DoubleStream flatMapToDouble(@NotNull final Function<? super T, ? extends
10341035
returnnewDoubleStream(params,newObjFlatMapToDouble<T>(iterator,mapper));
10351036
}
10361037

1038+
/**
1039+
* Returns a stream consisting of the results of replacing each element of
1040+
* this stream with zero or more elements,
1041+
* that passed to the provided consumer function.
1042+
*
1043+
* <p>This is an intermediate operation.
1044+
*
1045+
* <p>Example:
1046+
* <pre>
1047+
* stream: [1, 2, 3, 4]
1048+
* mapper: (a, consumer) -&gt; {
1049+
* consumer.accept(a);
1050+
* consumer.accept(-a);
1051+
* }
1052+
* result: [1, -1, 2, -2, 3, -3, 4, -4]
1053+
*
1054+
* stream: [1, 2, 3, 4]
1055+
* mapper: (a, consumer) -&gt; {
1056+
* if (a % 2 == 0)
1057+
* consumer.accept(a * 2);
1058+
* }
1059+
* result: [4, 8]
1060+
* </pre>
1061+
*
1062+
* @param <R> the type of elements in resulting stream
1063+
* @param mapper the mapper function used to apply to each element for producing replacing elements
1064+
* @return the new stream
1065+
* @since 1.2.2
1066+
* @see #flatMap(com.annimon.stream.function.Function)
1067+
*/
1068+
@NotNull
1069+
public <R>Stream<R>mapMulti(@NotNullfinalBiConsumer<?superT, ?superConsumer<R>>mapper) {
1070+
returnflatMap(newFunction<T,Stream<?extendsR>>() {
1071+
@Override
1072+
publicStream<?extendsR>apply(Tt) {
1073+
SpinedBuffer.Of<R>buffer =newSpinedBuffer.Of<R>();
1074+
mapper.accept(t,buffer);
1075+
returnof(buffer.iterator());
1076+
}
1077+
});
1078+
}
1079+
1080+
/**
1081+
* Returns a stream consisting of the results of replacing each element of
1082+
* this stream with zero or more elements,
1083+
* that passed to the provided consumer function.
1084+
*
1085+
* <p>This is an intermediate operation.
1086+
*
1087+
* @param mapper the mapper function used to apply to each element for producing replacing elements
1088+
* @return the new stream
1089+
* @since 1.2.2
1090+
* @see #mapMulti(com.annimon.stream.function.BiConsumer)
1091+
* @see #flatMapToInt(com.annimon.stream.function.Function)
1092+
*/
1093+
@NotNull
1094+
publicIntStreammapMultiToInt(@NotNullfinalBiConsumer<?superT, ?superIntConsumer>mapper) {
1095+
returnflatMapToInt(newFunction<T,IntStream>() {
1096+
@Override
1097+
publicIntStreamapply(Tt) {
1098+
SpinedBuffer.OfIntbuffer =newSpinedBuffer.OfInt();
1099+
mapper.accept(t,buffer);
1100+
returnIntStream.of(buffer.iterator());
1101+
}
1102+
});
1103+
}
1104+
1105+
/**
1106+
* Returns a stream consisting of the results of replacing each element of
1107+
* this stream with zero or more elements,
1108+
* that passed to the provided consumer function.
1109+
*
1110+
* <p>This is an intermediate operation.
1111+
*
1112+
* @param mapper the mapper function used to apply to each element for producing replacing elements
1113+
* @return the new stream
1114+
* @since 1.2.2
1115+
* @see #mapMulti(com.annimon.stream.function.BiConsumer)
1116+
* @see #flatMapToLong(com.annimon.stream.function.Function)
1117+
*/
1118+
@NotNull
1119+
publicLongStreammapMultiToLong(@NotNullfinalBiConsumer<?superT, ?superLongConsumer>mapper) {
1120+
returnflatMapToLong(newFunction<T,LongStream>() {
1121+
@Override
1122+
publicLongStreamapply(Tt) {
1123+
SpinedBuffer.OfLongbuffer =newSpinedBuffer.OfLong();
1124+
mapper.accept(t,buffer);
1125+
returnLongStream.of(buffer.iterator());
1126+
}
1127+
});
1128+
}
1129+
1130+
/**
1131+
* Returns a stream consisting of the results of replacing each element of
1132+
* this stream with zero or more elements,
1133+
* that passed to the provided consumer function.
1134+
*
1135+
* <p>This is an intermediate operation.
1136+
*
1137+
* @param mapper the mapper function used to apply to each element for producing replacing elements
1138+
* @return the new stream
1139+
* @since 1.2.2
1140+
* @see #mapMulti(com.annimon.stream.function.BiConsumer)
1141+
* @see #flatMapToDouble(com.annimon.stream.function.Function)
1142+
*/
1143+
@NotNull
1144+
publicDoubleStreammapMultiToDouble(@NotNullfinalBiConsumer<?superT, ?superDoubleConsumer>mapper) {
1145+
returnflatMapToDouble(newFunction<T,DoubleStream>() {
1146+
@Override
1147+
publicDoubleStreamapply(Tt) {
1148+
SpinedBuffer.OfDoublebuffer =newSpinedBuffer.OfDouble();
1149+
mapper.accept(t,buffer);
1150+
returnDoubleStream.of(buffer.iterator());
1151+
}
1152+
});
1153+
}
1154+
10371155
/**
10381156
* Returns {@code Stream} with indexed elements.
10391157
* Indexing starts from 0 with step 1.
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
packagecom.annimon.stream.streamtests;
2+
3+
importcom.annimon.stream.Stream;
4+
importcom.annimon.stream.function.BiConsumer;
5+
importcom.annimon.stream.function.Consumer;
6+
importcom.annimon.stream.test.hamcrest.StreamMatcher;
7+
importorg.junit.Test;
8+
importstaticcom.annimon.stream.test.hamcrest.StreamMatcher.assertElements;
9+
importstaticorg.hamcrest.Matchers.contains;
10+
11+
publicfinalclassMapMultiTest {
12+
13+
@Test
14+
publicvoidtestMapMulti() {
15+
Stream.rangeClosed(2,4)
16+
.mapMulti(newBiConsumer<Integer,Consumer<String>>() {
17+
@Override
18+
publicvoidaccept(Integeri,Consumer<String>consumer) {
19+
consumer.accept(String.format("%d * 2 = %d",i, (i*2)));
20+
consumer.accept(String.format("%d * 4 = %d",i, (i*4)));
21+
}
22+
})
23+
.custom(assertElements(contains(
24+
"2 * 2 = 4",
25+
"2 * 4 = 8",
26+
"3 * 2 = 6",
27+
"3 * 4 = 12",
28+
"4 * 2 = 8",
29+
"4 * 4 = 16"
30+
)));
31+
}
32+
33+
@Test
34+
publicvoidtestMapMultiEmpty() {
35+
Stream.rangeClosed(2,4)
36+
.mapMulti(newBiConsumer<Integer,Consumer<String>>() {
37+
@Override
38+
publicvoidaccept(Integeri,Consumer<String>consumer) {
39+
}
40+
})
41+
.custom(StreamMatcher.<String>assertIsEmpty());
42+
}
43+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
packagecom.annimon.stream.streamtests;
2+
3+
importcom.annimon.stream.Stream;
4+
importcom.annimon.stream.function.BiConsumer;
5+
importcom.annimon.stream.function.DoubleConsumer;
6+
importorg.junit.Test;
7+
importstaticcom.annimon.stream.test.hamcrest.DoubleStreamMatcher.assertElements;
8+
importstaticcom.annimon.stream.test.hamcrest.DoubleStreamMatcher.assertIsEmpty;
9+
importstaticorg.hamcrest.Matchers.array;
10+
importstaticorg.hamcrest.Matchers.closeTo;
11+
12+
publicfinalclassMapMultiToDoubleTest {
13+
14+
@SuppressWarnings("unchecked")
15+
@Test
16+
publicvoidtestMapMultiToDouble() {
17+
Stream.of(2,4)
18+
.mapMultiToDouble(newBiConsumer<Integer,DoubleConsumer>() {
19+
@Override
20+
publicvoidaccept(Integervalue,DoubleConsumerconsumer) {
21+
consumer.accept(value /10d);
22+
consumer.accept(value /20d);
23+
}
24+
})
25+
.custom(assertElements(array(
26+
closeTo(0.2,0.0001),
27+
closeTo(0.1,0.0001),
28+
closeTo(0.4,0.0001),
29+
closeTo(0.2,0.0001)
30+
)));
31+
}
32+
33+
@Test
34+
publicvoidtestMapMultiToDoubleEmpty() {
35+
Stream.rangeClosed(2,4)
36+
.mapMultiToDouble(newBiConsumer<Integer,DoubleConsumer>() {
37+
@Override
38+
publicvoidaccept(Integervalue,DoubleConsumerconsumer) {
39+
}
40+
})
41+
.custom(assertIsEmpty());
42+
}
43+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
packagecom.annimon.stream.streamtests;
2+
3+
importcom.annimon.stream.Stream;
4+
importcom.annimon.stream.function.BiConsumer;
5+
importcom.annimon.stream.function.IntConsumer;
6+
importorg.junit.Test;
7+
importstaticcom.annimon.stream.test.hamcrest.IntStreamMatcher.assertElements;
8+
importstaticcom.annimon.stream.test.hamcrest.IntStreamMatcher.assertIsEmpty;
9+
importstaticorg.hamcrest.Matchers.arrayContaining;
10+
11+
publicfinalclassMapMultiToIntTest {
12+
13+
@Test
14+
publicvoidtestMapMultiToInt() {
15+
Stream.rangeClosed(2,4)
16+
.mapMultiToInt(newBiConsumer<Integer,IntConsumer>() {
17+
@Override
18+
publicvoidaccept(Integervalue,IntConsumerconsumer) {
19+
for (inti =0;i <value;i++) {
20+
consumer.accept(value);
21+
}
22+
}
23+
})
24+
.custom(assertElements(arrayContaining(
25+
2,2,
26+
3,3,3,
27+
4,4,4,4
28+
)));
29+
}
30+
31+
@Test
32+
publicvoidtestMapMultiToIntEmpty() {
33+
Stream.rangeClosed(2,4)
34+
.mapMultiToInt(newBiConsumer<Integer,IntConsumer>() {
35+
@Override
36+
publicvoidaccept(Integervalue,IntConsumerconsumer) {
37+
}
38+
})
39+
.custom(assertIsEmpty());
40+
}
41+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
packagecom.annimon.stream.streamtests;
2+
3+
importcom.annimon.stream.Stream;
4+
importcom.annimon.stream.function.BiConsumer;
5+
importcom.annimon.stream.function.LongConsumer;
6+
importorg.junit.Test;
7+
importstaticcom.annimon.stream.test.hamcrest.LongStreamMatcher.assertElements;
8+
importstaticcom.annimon.stream.test.hamcrest.LongStreamMatcher.assertIsEmpty;
9+
importstaticorg.hamcrest.Matchers.arrayContaining;
10+
11+
publicfinalclassMapMultiToLongTest {
12+
13+
@Test
14+
publicvoidtestMapMultiToLong() {
15+
Stream.rangeClosed(2L,4L)
16+
.mapMultiToLong(newBiConsumer<Long,LongConsumer>() {
17+
@Override
18+
publicvoidaccept(Longvalue,LongConsumerconsumer) {
19+
for (longi =0;i <value;i++) {
20+
consumer.accept(value);
21+
}
22+
}
23+
})
24+
.custom(assertElements(arrayContaining(
25+
2L,2L,
26+
3L,3L,3L,
27+
4L,4L,4L,4L
28+
)));
29+
}
30+
31+
@Test
32+
publicvoidtestMapMultiToLongEmpty() {
33+
Stream.rangeClosed(2L,4L)
34+
.mapMultiToLong(newBiConsumer<Long,LongConsumer>() {
35+
@Override
36+
publicvoidaccept(Longvalue,LongConsumerconsumer) {
37+
}
38+
})
39+
.custom(assertIsEmpty());
40+
}
41+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp