Skip to contentMethod: tryAdvance(Consumer)
1: /*
2: * *********************************************************************************************************************
3: *
4: * TheseFoolishThings: Miscellaneous utilities
5: * http://tidalwave.it/projects/thesefoolishthings
6: *
7: * Copyright (C) 2009 - 2023 by Tidalwave s.a.s. (http://tidalwave.it)
8: *
9: * *********************************************************************************************************************
10: *
11: * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
12: * the License. You may obtain a copy of the License at
13: *
14: * http://www.apache.org/licenses/LICENSE-2.0
15: *
16: * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
17: * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
18: * specific language governing permissions and limitations under the License.
19: *
20: * *********************************************************************************************************************
21: *
22: * git clone https://bitbucket.org/tidalwave/thesefoolishthings-src
23: * git clone https://github.com/tidalwave-it/thesefoolishthings-src
24: *
25: * *********************************************************************************************************************
26: */
27: package it.tidalwave.util;
28:
29: import javax.annotation.Nonnull;
30: import java.util.Iterator;
31: import java.util.Spliterator;
32: import java.util.Spliterators;
33: import java.util.function.BiFunction;
34: import java.util.function.Consumer;
35: import java.util.stream.Stream;
36: import java.util.stream.StreamSupport;
37: import lombok.AccessLevel;
38: import lombok.NoArgsConstructor;
39:
40: /***********************************************************************************************************************
41: *
42: * A collection of operations on {@link Stream}s.
43: *
44: * @author Fabrizio Giudici
45: * @since 3.2-ALPHA-12
46: * @it.tidalwave.javadoc.draft
47: *
48: **********************************************************************************************************************/
49: @NoArgsConstructor(access = AccessLevel.PRIVATE)
50: public final class StreamOperations
51: {
52: /*******************************************************************************************************************
53: *
54: * Zips two streams into a stream of {@link Pair}s.
55: *
56: * @param streamA the first {@link Stream}
57: * @param streamB the second {@link Stream}
58: * @param <A> the type of elements of the first {@link Stream}
59: * @param <B> the type of elements of the second {@link Stream}
60: * @return the zipped {@link Stream}
61: * @since 3.2-ALPHA-12
62: *
63: ******************************************************************************************************************/
64: @Nonnull
65: public static <A, B> Stream<Pair<A, B>> zip (@Nonnull final Stream<? extends A> streamA,
66: @Nonnull final Stream<? extends B> streamB)
67: {
68: return zip(streamA, streamB, Pair::of);
69: }
70:
71: /*******************************************************************************************************************
72: *
73: * Zips two streams.
74: *
75: * @param streamA the first {@link Stream}
76: * @param streamB the second {@link Stream}
77: * @param zipper the zipping function
78: * @param <A> the type of elements of the first {@link Stream}
79: * @param <B> the type of elements of the second {@link Stream}
80: * @param <R> the type of elements of the zipped {@link Stream}
81: * @return the zipped {@link Stream}
82: * @since 3.2-ALPHA-12
83: *
84: ******************************************************************************************************************/
85: @Nonnull
86: public static <A, B, R> Stream<R> zip (@Nonnull final Stream<A> streamA,
87: @Nonnull final Stream<B> streamB,
88: @Nonnull final BiFunction<? super A, ? super B, ? extends R> zipper)
89: {
90: final boolean parallel = streamA.isParallel() || streamB.isParallel();
91: final Spliterator<A> sa = streamA.spliterator();
92: final Spliterator<B> sb = streamB.spliterator();
93: final int characteristics =
94: sa.characteristics() & sb.characteristics() & (Spliterator.SIZED | Spliterator.ORDERED);
95: final Iterator<A> a = Spliterators.iterator(sa);
96: final Iterator<B> b = Spliterators.iterator(sb);
97: final long estSize = Math.min(sa.estimateSize(), sb.estimateSize());
98: return StreamSupport.stream(new Spliterators.AbstractSpliterator<R>(estSize, characteristics)
99: {
100: @Override
101: public boolean tryAdvance (@Nonnull final Consumer<? super R> action)
102: {
103:• if (a.hasNext() && b.hasNext())
104: {
105: action.accept(zipper.apply(a.next(), b.next()));
106: return true;
107: }
108:
109: return false;
110: }
111: },
112: parallel)
113: .onClose(streamA::close)
114: .onClose(streamB::close);
115: }
116: }