StreamUtils.java

  1. /*
  2.  * *************************************************************************************************************************************************************
  3.  *
  4.  * TheseFoolishThings: Miscellaneous utilities
  5.  * http://tidalwave.it/projects/thesefoolishthings
  6.  *
  7.  * Copyright (C) 2009 - 2025 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 the License.
  12.  * 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 an "AS IS" BASIS, WITHOUT WARRANTIES OR
  17.  * CONDITIONS OF ANY KIND, either express or implied.  See the License for the specific language governing permissions and limitations under the License.
  18.  *
  19.  * *************************************************************************************************************************************************************
  20.  *
  21.  * git clone https://bitbucket.org/tidalwave/thesefoolishthings-src
  22.  * git clone https://github.com/tidalwave-it/thesefoolishthings-src
  23.  *
  24.  * *************************************************************************************************************************************************************
  25.  */
  26. package it.tidalwave.util;

  27. import jakarta.annotation.Nonnull;
  28. import java.time.Instant;
  29. import java.time.LocalDateTime;
  30. import java.time.ZoneId;
  31. import java.time.ZoneOffset;
  32. import java.util.Random;
  33. import java.util.Spliterator;
  34. import java.util.Spliterators;
  35. import java.util.function.BiFunction;
  36. import java.util.function.Consumer;
  37. import java.util.stream.Stream;
  38. import java.util.stream.StreamSupport;
  39. import lombok.AccessLevel;
  40. import lombok.NoArgsConstructor;

  41. /***************************************************************************************************************************************************************
  42.  *
  43.  * A collection of operations on {@link Stream}s.
  44.  *
  45.  * @author  Fabrizio Giudici
  46.  * @since   3.2-ALPHA-12
  47.  * @it.tidalwave.javadoc.draft
  48.  *
  49.  **************************************************************************************************************************************************************/
  50. @NoArgsConstructor(access = AccessLevel.PRIVATE)
  51. public final class StreamUtils
  52.   {
  53.     /***********************************************************************************************************************************************************
  54.      * Zips two streams.
  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} of {@link Pair}s
  61.      * @see     #zip(Stream, Stream, BiFunction)
  62.      * @since   3.2-ALPHA-20
  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.      * Zips two streams.
  72.      *
  73.      * @param   streamA     the first {@link Stream}
  74.      * @param   streamB     the second {@link Stream}
  75.      * @param   zipper      the zipping function
  76.      * @param   <A>         the type of elements of the first {@link Stream}
  77.      * @param   <B>         the type of elements of the second {@link Stream}
  78.      * @param   <R>         the type of elements of the zipped {@link Stream}
  79.      * @return              the zipped {@link Stream}
  80.      * @see     #zip(Stream, Stream)
  81.      * @since   3.2-ALPHA-12
  82.      **********************************************************************************************************************************************************/
  83.     @Nonnull
  84.     public static <A, B, R> Stream<R> zip (@Nonnull final Stream<? extends A> streamA,
  85.                                            @Nonnull final Stream<? extends B> streamB,
  86.                                            @Nonnull final BiFunction<? super A, ? super B, ? extends R> zipper)
  87.       {
  88.         final var parallel = streamA.isParallel() || streamB.isParallel();
  89.         final var sa = streamA.spliterator();
  90.         final var sb = streamB.spliterator();
  91.         final var characteristics =
  92.                 sa.characteristics() & sb.characteristics() & (Spliterator.SIZED | Spliterator.ORDERED);
  93.         final var a = Spliterators.iterator(sa);
  94.         final var b = Spliterators.iterator(sb);
  95.         final var estSize = Math.min(sa.estimateSize(), sb.estimateSize());
  96.         return StreamSupport.stream(new Spliterators.AbstractSpliterator<R>(estSize, characteristics)
  97.             {
  98.               @Override
  99.               public boolean tryAdvance (@Nonnull final Consumer<? super R> action)
  100.                 {
  101.                   if (a.hasNext() && b.hasNext())
  102.                     {
  103.                       action.accept(zipper.apply(a.next(), b.next()));
  104.                       return true;
  105.                     }

  106.                   return false;
  107.                 }
  108.             },
  109.           parallel)
  110.           .onClose(streamA::close)
  111.           .onClose(streamB::close);
  112.       }

  113.     /***********************************************************************************************************************************************************
  114.      * Returns a {@code Stream} of random {@link LocalDateTime}s, in the given range. The used random generator is not strong enough for cryptographic purposes.
  115.      * @param   seed      the random seed
  116.      * @param   from      the lower bound of the range (included)
  117.      * @param   to        the upper bound of the range (excluded)
  118.      * @return            the stream
  119.      * @since   3.2-ALPHA-19
  120.      **********************************************************************************************************************************************************/
  121.     @Nonnull
  122.     public static Stream<LocalDateTime> randomLocalDateTimeStream (final long seed,
  123.                                                                    @Nonnull final LocalDateTime from,
  124.                                                                    @Nonnull final LocalDateTime to)
  125.       {
  126.         final var zo = ZoneOffset.UTC;
  127.         return new Random(seed)
  128.                 .longs(from.toEpochSecond(zo), to.toEpochSecond(zo))
  129.                 .mapToObj(l -> LocalDateTime.ofInstant(Instant.ofEpochSecond(l), ZoneId.of(zo.getId())));
  130.       }
  131.   }