StreamOperations.java

  1. /*
  2.  * *********************************************************************************************************************
  3.  *
  4.  * TheseFoolishThings: Miscellaneous utilities
  5.  * http://tidalwave.it/projects/thesefoolishthings
  6.  *
  7.  * Copyright (C) 2009 - 2021 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.util.Iterator;
  30. import java.util.Spliterator;
  31. import java.util.Spliterators;
  32. import java.util.function.BiFunction;
  33. import java.util.function.Consumer;
  34. import java.util.stream.Stream;
  35. import java.util.stream.StreamSupport;
  36. import lombok.AccessLevel;
  37. import lombok.NoArgsConstructor;

  38. /***********************************************************************************************************************
  39.  *
  40.  * A collection of operations on {@link Stream}s.
  41.  *
  42.  * @author  Fabrizio Giudici
  43.  * @since   3.2-ALPHA-12
  44.  * @it.tidalwave.javadoc.draft
  45.  *
  46.  **********************************************************************************************************************/
  47. @NoArgsConstructor(access = AccessLevel.PRIVATE)
  48. public final class StreamOperations
  49.   {
  50.     /*******************************************************************************************************************
  51.      *
  52.      * Zips two streams into a stream of {@link Pair}s.
  53.      *
  54.      * @param   streamA     the first {@link Stream}
  55.      * @param   streamB     the second {@link Stream}
  56.      * @param   <A>         the type of elements of the first {@link Stream}
  57.      * @param   <B>         the type of elements of the second {@link Stream}
  58.      * @return              the zipped {@link Stream}
  59.      * @since   3.2-ALPHA-12
  60.      *
  61.      ******************************************************************************************************************/
  62.     @Nonnull
  63.     public static <A, B> Stream<Pair<A, B>> zip (@Nonnull final Stream<? extends A> streamA,
  64.                                                  @Nonnull final Stream<? extends B> streamB)
  65.       {
  66.         return zip(streamA, streamB, Pair::of);
  67.       }

  68.     /*******************************************************************************************************************
  69.      *
  70.      * Zips two streams.
  71.      *
  72.      * @param   streamA     the first {@link Stream}
  73.      * @param   streamB     the second {@link Stream}
  74.      * @param   zipper      the zipping function
  75.      * @param   <A>         the type of elements of the first {@link Stream}
  76.      * @param   <B>         the type of elements of the second {@link Stream}
  77.      * @param   <R>         the type of elements of the zipped {@link Stream}
  78.      * @return              the zipped {@link Stream}
  79.      * @since   3.2-ALPHA-12
  80.      *
  81.      ******************************************************************************************************************/
  82.     @Nonnull
  83.     public static <A, B, R> Stream<R> zip (@Nonnull final Stream<A> streamA,
  84.                                            @Nonnull final Stream<B> streamB,
  85.                                            @Nonnull final BiFunction<? super A, ? super B, ? extends R> zipper)
  86.       {
  87.         final boolean parallel = streamA.isParallel() || streamB.isParallel();
  88.         final Spliterator<A> sa = streamA.spliterator();
  89.         final Spliterator<B> sb = streamB.spliterator();
  90.         final int characteristics =
  91.                 sa.characteristics() & sb.characteristics() & (Spliterator.SIZED | Spliterator.ORDERED);
  92.         final Iterator<A> a = Spliterators.iterator(sa);
  93.         final Iterator<B> b = Spliterators.iterator(sb);
  94.         final long estSize = Math.min(sa.estimateSize(), sb.estimateSize());
  95.         return StreamSupport.stream(new Spliterators.AbstractSpliterator<R>(estSize, characteristics)
  96.             {
  97.               @Override
  98.               public boolean tryAdvance (@Nonnull final Consumer<? super R> action)
  99.                 {
  100.                   if (a.hasNext() && b.hasNext())
  101.                     {
  102.                       action.accept(zipper.apply(a.next(), b.next()));
  103.                       return true;
  104.                     }

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