Triple.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.Nonnegative;
  29. import javax.annotation.Nonnull;
  30. import javax.annotation.concurrent.Immutable;
  31. import java.util.stream.IntStream;
  32. import java.util.stream.Stream;
  33. import lombok.EqualsAndHashCode;
  34. import lombok.Getter;
  35. import lombok.RequiredArgsConstructor;
  36. import lombok.ToString;

  37. /***********************************************************************************************************************
  38.  *
  39.  * A value object that contains a triple of values.
  40.  *
  41.  * @author  Fabrizio Giudici
  42.  * @since   3.2-ALPHA-12
  43.  * @it.tidalwave.javadoc.draft
  44.  *
  45.  **********************************************************************************************************************/
  46. @Immutable @RequiredArgsConstructor(staticName = "of") @ToString @EqualsAndHashCode
  47. public class Triple<A, B, C>
  48.   {
  49.     @Getter @Nonnull
  50.     public final A a;

  51.     @Getter @Nonnull
  52.     public final B b;

  53.     @Getter @Nonnull
  54.     public final C c;

  55.     /*******************************************************************************************************************
  56.      *
  57.      * Creates a {@code Triple} from a {@code Pair} and another value.
  58.      *
  59.      * @param   <T>     the former type of the {@code Pair}
  60.      * @param   <U>     the latter type of the {@code Pair}
  61.      * @param   <V>     the type of the value
  62.      * @param   pair    the {@code Pair}
  63.      * @param   value   the value
  64.      * @return          the {@code Stream} of {@code Triple}s
  65.      *
  66.      ******************************************************************************************************************/
  67.     @Nonnull
  68.     public static <T, U, V> Triple<T, U, V> of (@Nonnull final Pair<T, U> pair, @Nonnull final V value)
  69.       {
  70.         return of(pair.a, pair.b, value);
  71.       }

  72.     /*******************************************************************************************************************
  73.      *
  74.      * Creates a {@link Stream} of {@code Triple}s composed of a given {@code Pair} and another element taken from
  75.      * {@link Stream}.
  76.      *
  77.      * @param   <T>     the former type of the {@code Pair}
  78.      * @param   <U>     the latter type of the {@code Pair}
  79.      * @param   <V>     the type of the {@code Stream}
  80.      * @param   pair    the {@code Pair}
  81.      * @param   stream  the {@code Stream}
  82.      * @return          the {@code Stream} of {@code Triple}s
  83.      *
  84.      ******************************************************************************************************************/
  85.     @Nonnull
  86.     public static <T, U, V> Stream<Triple<T, U, V>> tripleStream (@Nonnull final Pair<T, U> pair,
  87.                                                                   @Nonnull final Stream<? extends V> stream)
  88.       {
  89.         return stream.map(value -> Triple.of(pair, value));
  90.       }

  91.     /*******************************************************************************************************************
  92.      *
  93.      * Creates a {@link Stream} of {@code Triple}s composed of a given fixed {@code Pair} and an integer in the given
  94.      * range.
  95.      *
  96.      * @param   <T>     the former type of the {@code Pair}
  97.      * @param   <U>     the latter type of the {@code Pair}
  98.      * @param   pair    the {@code Pair}
  99.      * @param   from    the first value of the integer {@code Stream} (included)
  100.      * @param   to      the last value of the integer {@code Stream} (excluded)
  101.      * @return          the {@code Stream} of {@code Triple}s
  102.      *
  103.      ******************************************************************************************************************/
  104.     @Nonnull
  105.     public static <T, U> Stream<Triple<T, U, Integer>> tripleRange (@Nonnull final Pair<T, U> pair,
  106.                                                                     @Nonnegative final int from,
  107.                                                                     @Nonnegative final int to)
  108.       {
  109.         return tripleStream(pair, IntStream.range(from, to).boxed());
  110.       }

  111.     /*******************************************************************************************************************
  112.      *
  113.      * Creates a {@link Stream} of {@code Triple}s composed of a given fixed {@code Pair} and an integer in the given
  114.      * range.
  115.      *
  116.      * @param   <T>     the former type of the {@code Pair}
  117.      * @param   <U>     the latter type of the {@code Pair}
  118.      * @param   pair    the {@code Pair}
  119.      * @param   from    the first value of the integer {@code Stream} (included)
  120.      * @param   to      the last value of the integer {@code Stream} (included)
  121.      * @return          the {@code Stream} of {@code Triple}s
  122.      *
  123.      ******************************************************************************************************************/
  124.     @Nonnull
  125.     public static <T, U> Stream<Triple<T, U, Integer>> tripleRangeClosed (@Nonnull final Pair<T, U> pair,
  126.                                                                           @Nonnegative final int from,
  127.                                                                           @Nonnegative final int to)
  128.       {
  129.         return tripleStream(pair, IntStream.rangeClosed(from, to).boxed());
  130.       }
  131.   }