Skip to content

Package: StreamUtils$1

StreamUtils$1

nameinstructionbranchcomplexitylinemethod
tryAdvance(Consumer)
M: 0 C: 23
100%
M: 0 C: 4
100%
M: 0 C: 3
100%
M: 0 C: 4
100%
M: 0 C: 1
100%
{...}
M: 0 C: 14
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%

Coverage

1: /*
2: * *********************************************************************************************************************
3: *
4: * TheseFoolishThings: Miscellaneous utilities
5: * http://tidalwave.it/projects/thesefoolishthings
6: *
7: * Copyright (C) 2009 - 2024 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.time.Instant;
31: import java.time.LocalDateTime;
32: import java.time.ZoneId;
33: import java.time.ZoneOffset;
34: import java.util.Random;
35: import java.util.Spliterator;
36: import java.util.Spliterators;
37: import java.util.function.BiFunction;
38: import java.util.function.Consumer;
39: import java.util.stream.Stream;
40: import java.util.stream.StreamSupport;
41: import lombok.AccessLevel;
42: import lombok.NoArgsConstructor;
43:
44: /***********************************************************************************************************************
45: *
46: * A collection of operations on {@link Stream}s.
47: *
48: * @author Fabrizio Giudici
49: * @since 3.2-ALPHA-12
50: * @it.tidalwave.javadoc.draft
51: *
52: **********************************************************************************************************************/
53: @NoArgsConstructor(access = AccessLevel.PRIVATE)
54: public final class StreamUtils
55: {
56: /*******************************************************************************************************************
57: *
58: * Zips two streams.
59: *
60: * @param streamA the first {@link Stream}
61: * @param streamB the second {@link Stream}
62: * @param <A> the type of elements of the first {@link Stream}
63: * @param <B> the type of elements of the second {@link Stream}
64: * @return the zipped {@link Stream} of {@link Pair}s
65: * @see #zip(Stream, Stream, BiFunction)
66: * @since 3.2-ALPHA-20
67: *
68: ******************************************************************************************************************/
69: @Nonnull
70: public static <A, B> Stream<Pair<A, B>> zip (@Nonnull final Stream<? extends A> streamA,
71: @Nonnull final Stream<? extends B> streamB)
72: {
73: return zip(streamA, streamB, Pair::of);
74: }
75:
76: /*******************************************************************************************************************
77: *
78: * Zips two streams.
79: *
80: * @param streamA the first {@link Stream}
81: * @param streamB the second {@link Stream}
82: * @param zipper the zipping function
83: * @param <A> the type of elements of the first {@link Stream}
84: * @param <B> the type of elements of the second {@link Stream}
85: * @param <R> the type of elements of the zipped {@link Stream}
86: * @return the zipped {@link Stream}
87: * @see #zip(Stream, Stream)
88: * @since 3.2-ALPHA-12
89: *
90: ******************************************************************************************************************/
91: @Nonnull
92: public static <A, B, R> Stream<R> zip (@Nonnull final Stream<? extends A> streamA,
93: @Nonnull final Stream<? extends B> streamB,
94: @Nonnull final BiFunction<? super A, ? super B, ? extends R> zipper)
95: {
96: final var parallel = streamA.isParallel() || streamB.isParallel();
97: final var sa = streamA.spliterator();
98: final var sb = streamB.spliterator();
99: final var characteristics =
100: sa.characteristics() & sb.characteristics() & (Spliterator.SIZED | Spliterator.ORDERED);
101: final var a = Spliterators.iterator(sa);
102: final var b = Spliterators.iterator(sb);
103: final var estSize = Math.min(sa.estimateSize(), sb.estimateSize());
104: return StreamSupport.stream(new Spliterators.AbstractSpliterator<R>(estSize, characteristics)
105: {
106: @Override
107: public boolean tryAdvance (@Nonnull final Consumer<? super R> action)
108: {
109:• if (a.hasNext() && b.hasNext())
110: {
111: action.accept(zipper.apply(a.next(), b.next()));
112: return true;
113: }
114:
115: return false;
116: }
117: },
118: parallel)
119: .onClose(streamA::close)
120: .onClose(streamB::close);
121: }
122:
123: /*******************************************************************************************************************
124: *
125: * Returns a {@code Stream} of random {@link LocalDateTime}s, in the given range.
126: *
127: * @param seed the random seed
128: * @param from the lower bound of the range (included)
129: * @param to the upper bound of the range (excluded)
130: * @return the stream
131: * @since 3.2-ALPHA-19
132: *
133: ******************************************************************************************************************/
134: @Nonnull
135: public static Stream<LocalDateTime> randomLocalDateTimeStream (final long seed,
136: @Nonnull final LocalDateTime from,
137: @Nonnull final LocalDateTime to)
138: {
139: final var zo = ZoneOffset.UTC;
140: return new Random(seed)
141: .longs(from.toEpochSecond(zo), to.toEpochSecond(zo))
142: .mapToObj(l -> LocalDateTime.ofInstant(Instant.ofEpochSecond(l), ZoneId.of(zo.getId())));
143: }
144: }