View Javadoc
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  
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   *
40   * A value object that contains a triple of values.
41   *
42   * @author  Fabrizio Giudici
43   * @since   3.2-ALPHA-12
44   * @it.tidalwave.javadoc.draft
45   *
46   **************************************************************************************************************************************************************/
47  @Getter @Immutable @RequiredArgsConstructor(staticName = "of") @ToString @EqualsAndHashCode
48  public class Triple<A, B, C>
49    {
50      @Nonnull
51      public final A a;
52  
53      @Nonnull
54      public final B b;
55  
56      @Nonnull
57      public final C c;
58  
59      /***********************************************************************************************************************************************************
60       * Creates a {@code Triple} from a {@code Pair} and another value.
61       *
62       * @param   <T>     the former type of the {@code Pair}
63       * @param   <U>     the latter type of the {@code Pair}
64       * @param   <V>     the type of the value
65       * @param   pair    the {@code Pair}
66       * @param   value   the value
67       * @return          the {@code Stream} of {@code Triple}s
68       **********************************************************************************************************************************************************/
69      @Nonnull
70      public static <T, U, V> Triple<T, U, V> of (@Nonnull final Pair<T, U> pair, @Nonnull final V value)
71        {
72          return of(pair.a, pair.b, value);
73        }
74  
75      /***********************************************************************************************************************************************************
76       * Creates a {@link Stream} of {@code Triple}s composed of a given {@code Pair} and another element taken from
77       * {@link Stream}.
78       *
79       * @param   <T>     the former type of the {@code Pair}
80       * @param   <U>     the latter type of the {@code Pair}
81       * @param   <V>     the type of the {@code Stream}
82       * @param   pair    the {@code Pair}
83       * @param   stream  the {@code Stream}
84       * @return          the {@code Stream} of {@code Triple}s
85       **********************************************************************************************************************************************************/
86      @Nonnull
87      public static <T, U, V> Stream<Triple<T, U, V>> tripleStream (@Nonnull final Pair<T, U> pair,
88                                                                    @Nonnull final Stream<? extends V> stream)
89        {
90          return stream.map(value -> of(pair, value));
91        }
92  
93      /***********************************************************************************************************************************************************
94       * Creates a {@link Stream} of {@code Triple}s composed of a given fixed {@code Pair} and an integer in the given
95       * range.
96       *
97       * @param   <T>     the former type of the {@code Pair}
98       * @param   <U>     the latter type of the {@code Pair}
99       * @param   pair    the {@code Pair}
100      * @param   from    the first value of the integer {@code Stream} (included)
101      * @param   to      the last value of the integer {@code Stream} (excluded)
102      * @return          the {@code Stream} of {@code Triple}s
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     @Nonnull
124     public static <T, U> Stream<Triple<T, U, Integer>> tripleRangeClosed (@Nonnull final Pair<T, U> pair,
125                                                                           @Nonnegative final int from,
126                                                                           @Nonnegative final int to)
127       {
128         return tripleStream(pair, IntStream.rangeClosed(from, to).boxed());
129       }
130   }