StreamUtils.java

  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. import javax.annotation.Nonnull;
  29. import java.time.Instant;
  30. import java.time.LocalDateTime;
  31. import java.time.ZoneId;
  32. import java.time.ZoneOffset;
  33. import java.util.Random;
  34. import java.util.Spliterator;
  35. import java.util.Spliterators;
  36. import java.util.function.BiFunction;
  37. import java.util.function.Consumer;
  38. import java.util.stream.Stream;
  39. import java.util.stream.StreamSupport;
  40. import lombok.AccessLevel;
  41. import lombok.NoArgsConstructor;

  42. /***********************************************************************************************************************
  43.  *
  44.  * A collection of operations on {@link Stream}s.
  45.  *
  46.  * @author  Fabrizio Giudici
  47.  * @since   3.2-ALPHA-12
  48.  * @it.tidalwave.javadoc.draft
  49.  *
  50.  **********************************************************************************************************************/
  51. @NoArgsConstructor(access = AccessLevel.PRIVATE)
  52. public final class StreamUtils
  53.   {
  54.     /*******************************************************************************************************************
  55.      *
  56.      * Zips two streams.
  57.      *
  58.      * @param   streamA     the first {@link Stream}
  59.      * @param   streamB     the second {@link Stream}
  60.      * @param   zipper      the zipping function
  61.      * @param   <A>         the type of elements of the first {@link Stream}
  62.      * @param   <B>         the type of elements of the second {@link Stream}
  63.      * @param   <R>         the type of elements of the zipped {@link Stream}
  64.      * @return              the zipped {@link Stream}
  65.      * @since   3.2-ALPHA-12
  66.      *
  67.      ******************************************************************************************************************/
  68.     @Nonnull
  69.     public static <A, B, R> Stream<R> zip (@Nonnull final Stream<? extends A> streamA,
  70.                                            @Nonnull final Stream<? extends B> streamB,
  71.                                            @Nonnull final BiFunction<? super A, ? super B, ? extends R> zipper)
  72.       {
  73.         final var parallel = streamA.isParallel() || streamB.isParallel();
  74.         final var sa = streamA.spliterator();
  75.         final var sb = streamB.spliterator();
  76.         final var characteristics =
  77.                 sa.characteristics() & sb.characteristics() & (Spliterator.SIZED | Spliterator.ORDERED);
  78.         final var a = Spliterators.iterator(sa);
  79.         final var b = Spliterators.iterator(sb);
  80.         final var estSize = Math.min(sa.estimateSize(), sb.estimateSize());
  81.         return StreamSupport.stream(new Spliterators.AbstractSpliterator<R>(estSize, characteristics)
  82.             {
  83.               @Override
  84.               public boolean tryAdvance (@Nonnull final Consumer<? super R> action)
  85.                 {
  86.                   if (a.hasNext() && b.hasNext())
  87.                     {
  88.                       action.accept(zipper.apply(a.next(), b.next()));
  89.                       return true;
  90.                     }

  91.                   return false;
  92.                 }
  93.             },
  94.           parallel)
  95.           .onClose(streamA::close)
  96.           .onClose(streamB::close);
  97.       }

  98.     /*******************************************************************************************************************
  99.      *
  100.      * Returns a {@code Stream} of random {@link LocalDateTime}s, in the given range.
  101.      *
  102.      * @param   seed      the random seed
  103.      * @param   from      the lower bound of the range (included)
  104.      * @param   to        the upper bound of the range (excluded)
  105.      * @return            the stream
  106.      * @since   3.2-ALPHA-19
  107.      *
  108.      ******************************************************************************************************************/
  109.     @Nonnull
  110.     public static Stream<LocalDateTime> randomLocalDateTimeStream (final long seed,
  111.                                                                    @Nonnull final LocalDateTime from,
  112.                                                                    @Nonnull final LocalDateTime to)
  113.       {
  114.         final var zo = ZoneOffset.UTC;
  115.         return new Random(seed)
  116.                 .longs(from.toEpochSecond(zo), to.toEpochSecond(zo))
  117.                 .mapToObj(l -> LocalDateTime.ofInstant(Instant.ofEpochSecond(l), ZoneId.of(zo.getId())));
  118.       }
  119.   }