javaslang.Tuple1 Maven / Gradle / Ivy
/* / \____ _ _ ____ ______ / \ ____ __ _______
* / / \/ \ / \/ \ / /\__\/ // \/ \ // /\__\ JΛVΛSLΛNG
* _/ / /\ \ \/ / /\ \\__\\ \ // /\ \ /\\/ \ /__\ \ Copyright 2014-2016 Javaslang, http://javaslang.io
* /___/\_/ \_/\____/\_/ \_/\__\/__/\__\_/ \_// \__/\_____/ Licensed under the Apache License, Version 2.0
*/
package javaslang;
/*-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*\
G E N E R A T O R C R A F T E D
\*-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
import java.io.Serializable;
import java.util.Comparator;
import java.util.Objects;
import java.util.function.Function;
import javaslang.collection.List;
import javaslang.collection.Seq;
/**
* A tuple of one element which can be seen as cartesian product of one component.
*
* @param type of the 1st element
* @author Daniel Dietrich
* @since 1.1.0
*/
public final class Tuple1 implements Tuple, Comparable>, Serializable {
private static final long serialVersionUID = 1L;
/**
* The 1st element of this tuple.
*/
public final T1 _1;
/**
* Constructs a tuple of one element.
*
* @param t1 the 1st element
*/
public Tuple1(T1 t1) {
this._1 = t1;
}
public static Comparator> comparator(Comparator super T1> t1Comp) {
return (Comparator> & Serializable) (t1, t2) -> {
final int check1 = t1Comp.compare(t1._1, t2._1);
if (check1 != 0) {
return check1;
}
// all components are equal
return 0;
};
}
@SuppressWarnings("unchecked")
private static > int compareTo(Tuple1> o1, Tuple1> o2) {
final Tuple1 t1 = (Tuple1) o1;
final Tuple1 t2 = (Tuple1) o2;
final int check1 = t1._1.compareTo(t2._1);
if (check1 != 0) {
return check1;
}
// all components are equal
return 0;
}
@Override
public int arity() {
return 1;
}
@Override
public int compareTo(Tuple1 that) {
return Tuple1.compareTo(this, that);
}
/**
* Getter of the 1st element of this tuple.
*
* @return the 1st element of this Tuple.
*/
public T1 _1() {
return _1;
}
/**
* Maps the components of this tuple using a mapper function.
*
* @param mapper the mapper function
* @param new type of the 1st component
* @return A new Tuple of same arity.
* @throws NullPointerException if {@code mapper} is null
*/
public Tuple1 map(Function super T1, ? extends U1> mapper) {
Objects.requireNonNull(mapper, "mapper is null");
return Tuple.of(mapper.apply(_1));
}
/**
* 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 transform(Function super T1, ? extends U> f) {
Objects.requireNonNull(f, "f is null");
return f.apply(_1);
}
@Override
public Seq> toSeq() {
return List.of(_1);
}
// -- Object
@Override
public boolean equals(Object o) {
if (o == this) {
return true;
} else if (!(o instanceof Tuple1)) {
return false;
} else {
final Tuple1> that = (Tuple1>) o;
return Objects.equals(this._1, that._1);
}
}
// if _1 == null, hashCode() returns Objects.hash(new T1[] { null }) = 31 instead of 0 = Objects.hash(null)
@Override
public int hashCode() {
return Objects.hash(_1);
}
@Override
public String toString() {
return String.format("(%s)", _1);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy