/** * Create a {@link Tuple2} with the given objects. * * @param t1 The first value in the tuple. Not null. * @param t2 The second value in the tuple. Not null. * @param <T1> The type of the first value. * @param <T2> The type of the second value. * @return The new {@link Tuple2}. */ public static <T1, T2> Tuple2<T1, T2> of(T1 t1, T2 t2) { return new Tuple2<>(t1, t2); }
/** * Map the left-hand part (T1) of this {@link Tuple2} into a different value and type, * keeping the right-hand part (T2). * * @param mapper the mapping {@link Function} for the left-hand part * @param <R> the new type for the left-hand part * @return a new {@link Tuple2} with a different left (T1) value */ public <R> Tuple2<R, T2> mapT1(Function<T1, R> mapper) { return new Tuple2<>(mapper.apply(t1), t2); }
/** * Map the right-hand part (T2) of this {@link Tuple2} into a different value and type, * keeping the left-hand part (T1). * * @param mapper the mapping {@link Function} for the right-hand part * @param <R> the new type for the right-hand part * @return a new {@link Tuple2} with a different right (T2) value */ public <R> Tuple2<T1, R> mapT2(Function<T2, R> mapper) { return new Tuple2<>(t1, mapper.apply(t2)); }
@Test public void equals() { Tuple2<Integer, Integer> otherFull = new Tuple2<>(1, 2); assertThat(full) .isEqualTo(otherFull); }
@Test public void invertedContentNotEquals() { Tuple2<Integer, Integer> otherFull = new Tuple2<>(2, 1); assertThat(full) .isNotEqualTo(otherFull); }
@Test public void fnAnyDelegate() { Integer[] source = new Integer[] { 1, 2, 3, 4, 5, 6, 7, 8 }; Function<Tuple2, Tuple2<Object, Object>> invert = t2 -> new Tuple2<>(t2.getT2(), t2.getT1()); Tuple2<Object, Object> tuple = Tuples.fnAny(invert).apply(source); assertThat(tuple.getT1()).isEqualTo(2); assertThat(tuple.getT2()).isEqualTo(1); assertThat(tuple) .isExactlyInstanceOf(Tuple2.class) .containsExactly(2, 1); }
@Test public void nullT1Rejected() { assertThatExceptionOfType(NullPointerException.class) .isThrownBy(() -> new Tuple2<>(null, 2)) .withMessage("t1"); }
@Test public void nullT2Rejected() { assertThatExceptionOfType(NullPointerException.class) .isThrownBy(() -> new Tuple2<>(1, null)) .withMessage("t2"); } @Test
/** * Create a {@link Tuple2} with the given objects. * * @param t1 The first value in the tuple. Not null. * @param t2 The second value in the tuple. Not null. * @param <T1> The type of the first value. * @param <T2> The type of the second value. * @return The new {@link Tuple2}. */ public static <T1, T2> Tuple2<T1, T2> of(T1 t1, T2 t2) { return new Tuple2<>(t1, t2); }
/** * Map the left-hand part (T1) of this {@link Tuple2} into a different value and type, * keeping the right-hand part (T2). * * @param mapper the mapping {@link Function} for the left-hand part * @param <R> the new type for the left-hand part * @return a new {@link Tuple2} with a different left (T1) value */ public <R> Tuple2<R, T2> mapT1(Function<T1, R> mapper) { return new Tuple2<>(mapper.apply(t1), t2); }
/** * Map the right-hand part (T2) of this {@link Tuple2} into a different value and type, * keeping the left-hand part (T1). * * @param mapper the mapping {@link Function} for the right-hand part * @param <R> the new type for the right-hand part * @return a new {@link Tuple2} with a different right (T2) value */ public <R> Tuple2<T1, R> mapT2(Function<T2, R> mapper) { return new Tuple2<>(t1, mapper.apply(t2)); }