io.vavr.Tuple3 Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of vavr Show documentation
Show all versions of vavr Show documentation
Vavr is an object-functional library for Java 8+
/* __ __ __ __ __ ___
* \ \ / / \ \ / / __/
* \ \/ / /\ \ \/ / /
* \____/__/ \__\____/__/
*
* Copyright 2014-2019 Vavr, http://vavr.io
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* 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 CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.vavr;
/*-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*\
G E N E R A T O R C R A F T E D
\*-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
import io.vavr.collection.List;
import io.vavr.collection.Seq;
import java.io.Serializable;
import java.util.Comparator;
import java.util.Objects;
import java.util.function.Function;
/**
* A tuple of three elements which can be seen as cartesian product of three components.
*
* @param type of the 1st element
* @param type of the 2nd element
* @param type of the 3rd element
* @author Daniel Dietrich
*/
public final class Tuple3 implements Tuple, Comparable>, Serializable {
private static final long serialVersionUID = 1L;
/**
* The 1st element of this tuple.
*/
public final T1 _1;
/**
* The 2nd element of this tuple.
*/
public final T2 _2;
/**
* The 3rd element of this tuple.
*/
public final T3 _3;
/**
* Constructs a tuple of three elements.
*
* @param t1 the 1st element
* @param t2 the 2nd element
* @param t3 the 3rd element
*/
public Tuple3(T1 t1, T2 t2, T3 t3) {
this._1 = t1;
this._2 = t2;
this._3 = t3;
}
public static Comparator> comparator(Comparator super T1> t1Comp, Comparator super T2> t2Comp, Comparator super T3> t3Comp) {
return (Comparator> & Serializable) (t1, t2) -> {
final int check1 = t1Comp.compare(t1._1, t2._1);
if (check1 != 0) {
return check1;
}
final int check2 = t2Comp.compare(t1._2, t2._2);
if (check2 != 0) {
return check2;
}
final int check3 = t3Comp.compare(t1._3, t2._3);
if (check3 != 0) {
return check3;
}
// all components are equal
return 0;
};
}
@SuppressWarnings("unchecked")
private static , U2 extends Comparable super U2>, U3 extends Comparable super U3>> int compareTo(Tuple3, ?, ?> o1, Tuple3, ?, ?> o2) {
final Tuple3 t1 = (Tuple3) o1;
final Tuple3 t2 = (Tuple3) o2;
final int check1 = t1._1.compareTo(t2._1);
if (check1 != 0) {
return check1;
}
final int check2 = t1._2.compareTo(t2._2);
if (check2 != 0) {
return check2;
}
final int check3 = t1._3.compareTo(t2._3);
if (check3 != 0) {
return check3;
}
// all components are equal
return 0;
}
@Override
public int arity() {
return 3;
}
@Override
public int compareTo(Tuple3 that) {
return Tuple3.compareTo(this, that);
}
/**
* Getter of the 1st element of this tuple.
*
* @return the 1st element of this Tuple.
*/
public T1 _1() {
return _1;
}
/**
* Sets the 1st element of this tuple to the given {@code value}.
*
* @param value the new value
* @return a copy of this tuple with a new value for the 1st element of this Tuple.
*/
public Tuple3 update1(T1 value) {
return new Tuple3<>(value, _2, _3);
}
/**
* Getter of the 2nd element of this tuple.
*
* @return the 2nd element of this Tuple.
*/
public T2 _2() {
return _2;
}
/**
* Sets the 2nd element of this tuple to the given {@code value}.
*
* @param value the new value
* @return a copy of this tuple with a new value for the 2nd element of this Tuple.
*/
public Tuple3 update2(T2 value) {
return new Tuple3<>(_1, value, _3);
}
/**
* Getter of the 3rd element of this tuple.
*
* @return the 3rd element of this Tuple.
*/
public T3 _3() {
return _3;
}
/**
* Sets the 3rd element of this tuple to the given {@code value}.
*
* @param value the new value
* @return a copy of this tuple with a new value for the 3rd element of this Tuple.
*/
public Tuple3 update3(T3 value) {
return new Tuple3<>(_1, _2, value);
}
/**
* Maps the components of this tuple using a mapper function.
*
* @param mapper the mapper function
* @param new type of the 1st component
* @param new type of the 2nd component
* @param new type of the 3rd component
* @return A new Tuple of same arity.
* @throws NullPointerException if {@code mapper} is null
*/
public Tuple3 map(Function3 super T1, ? super T2, ? super T3, Tuple3> mapper) {
Objects.requireNonNull(mapper, "mapper is null");
return mapper.apply(_1, _2, _3);
}
/**
* Maps the components of this tuple using a mapper function for each component.
*
* @param f1 the mapper function of the 1st component
* @param f2 the mapper function of the 2nd component
* @param f3 the mapper function of the 3rd component
* @param new type of the 1st component
* @param new type of the 2nd component
* @param new type of the 3rd component
* @return A new Tuple of same arity.
* @throws NullPointerException if one of the arguments is null
*/
public Tuple3 map(Function super T1, ? extends U1> f1, Function super T2, ? extends U2> f2, Function super T3, ? extends U3> f3) {
Objects.requireNonNull(f1, "f1 is null");
Objects.requireNonNull(f2, "f2 is null");
Objects.requireNonNull(f3, "f3 is null");
return Tuple.of(f1.apply(_1), f2.apply(_2), f3.apply(_3));
}
/**
* Maps the 1st component of this tuple to a new value.
*
* @param new type of the 1st component
* @param mapper A mapping function
* @return a new tuple based on this tuple and substituted 1st component
*/
public Tuple3 map1(Function super T1, ? extends U> mapper) {
Objects.requireNonNull(mapper, "mapper is null");
final U u = mapper.apply(_1);
return Tuple.of(u, _2, _3);
}
/**
* Maps the 2nd component of this tuple to a new value.
*
* @param new type of the 2nd component
* @param mapper A mapping function
* @return a new tuple based on this tuple and substituted 2nd component
*/
public Tuple3 map2(Function super T2, ? extends U> mapper) {
Objects.requireNonNull(mapper, "mapper is null");
final U u = mapper.apply(_2);
return Tuple.of(_1, u, _3);
}
/**
* Maps the 3rd component of this tuple to a new value.
*
* @param new type of the 3rd component
* @param mapper A mapping function
* @return a new tuple based on this tuple and substituted 3rd component
*/
public Tuple3 map3(Function super T3, ? extends U> mapper) {
Objects.requireNonNull(mapper, "mapper is null");
final U u = mapper.apply(_3);
return Tuple.of(_1, _2, u);
}
/**
* Transforms this tuple to an object of type U.
*
* @param f Transformation which creates a new object of type U based on this tuple's contents.
* @param type of the transformation result
* @return An object of type U
* @throws NullPointerException if {@code f} is null
*/
public U apply(Function3 super T1, ? super T2, ? super T3, ? extends U> f) {
Objects.requireNonNull(f, "f is null");
return f.apply(_1, _2, _3);
}
@Override
public Seq> toSeq() {
return List.of(_1, _2, _3);
}
/**
* Append a value to this tuple.
*
* @param type of the value to append
* @param t4 the value to append
* @return a new Tuple with the value appended
*/
public Tuple4 append(T4 t4) {
return Tuple.of(_1, _2, _3, t4);
}
/**
* Concat a tuple's values to this tuple.
*
* @param the type of the 4th value in the tuple
* @param tuple the tuple to concat
* @return a new Tuple with the tuple values appended
* @throws NullPointerException if {@code tuple} is null
*/
public Tuple4 concat(Tuple1 tuple) {
Objects.requireNonNull(tuple, "tuple is null");
return Tuple.of(_1, _2, _3, tuple._1);
}
/**
* Concat a tuple's values to this tuple.
*
* @param the type of the 4th value in the tuple
* @param the type of the 5th value in the tuple
* @param tuple the tuple to concat
* @return a new Tuple with the tuple values appended
* @throws NullPointerException if {@code tuple} is null
*/
public Tuple5 concat(Tuple2 tuple) {
Objects.requireNonNull(tuple, "tuple is null");
return Tuple.of(_1, _2, _3, tuple._1, tuple._2);
}
/**
* Concat a tuple's values to this tuple.
*
* @param the type of the 4th value in the tuple
* @param the type of the 5th value in the tuple
* @param the type of the 6th value in the tuple
* @param tuple the tuple to concat
* @return a new Tuple with the tuple values appended
* @throws NullPointerException if {@code tuple} is null
*/
public Tuple6 concat(Tuple3 tuple) {
Objects.requireNonNull(tuple, "tuple is null");
return Tuple.of(_1, _2, _3, tuple._1, tuple._2, tuple._3);
}
/**
* Concat a tuple's values to this tuple.
*
* @param the type of the 4th value in the tuple
* @param the type of the 5th value in the tuple
* @param the type of the 6th value in the tuple
* @param the type of the 7th value in the tuple
* @param tuple the tuple to concat
* @return a new Tuple with the tuple values appended
* @throws NullPointerException if {@code tuple} is null
*/
public Tuple7 concat(Tuple4 tuple) {
Objects.requireNonNull(tuple, "tuple is null");
return Tuple.of(_1, _2, _3, tuple._1, tuple._2, tuple._3, tuple._4);
}
/**
* Concat a tuple's values to this tuple.
*
* @param the type of the 4th value in the tuple
* @param the type of the 5th value in the tuple
* @param the type of the 6th value in the tuple
* @param the type of the 7th value in the tuple
* @param the type of the 8th value in the tuple
* @param tuple the tuple to concat
* @return a new Tuple with the tuple values appended
* @throws NullPointerException if {@code tuple} is null
*/
public Tuple8 concat(Tuple5 tuple) {
Objects.requireNonNull(tuple, "tuple is null");
return Tuple.of(_1, _2, _3, tuple._1, tuple._2, tuple._3, tuple._4, tuple._5);
}
// -- Object
@Override
public boolean equals(Object o) {
if (o == this) {
return true;
} else if (!(o instanceof Tuple3)) {
return false;
} else {
final Tuple3, ?, ?> that = (Tuple3, ?, ?>) o;
return Objects.equals(this._1, that._1)
&& Objects.equals(this._2, that._2)
&& Objects.equals(this._3, that._3);
}
}
@Override
public int hashCode() {
return Tuple.hash(_1, _2, _3);
}
@Override
public String toString() {
return "(" + _1 + ", " + _2 + ", " + _3 + ")";
}
}