Triple.java

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

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

  50.     @Nonnull
  51.     public final B b;

  52.     @Nonnull
  53.     public final C c;

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

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

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

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