All Downloads are FREE. Search and download functionalities are using the official Maven repository.

net.dongliu.commons.collection.Triple Maven / Gradle / Ivy

There is a newer version: 6.7.0
Show newest version
package net.dongliu.commons.collection;

import javax.annotation.Nullable;
import java.io.Serializable;
import java.util.StringJoiner;

/**
 * Immutable three-elements tuple
 *
 * @author Liu Dong
 */
public class Triple implements Serializable {
    private static final long serialVersionUID = 7698985848288461531L;
    private final A first;
    private final B second;
    private final C third;

    public Triple(A first, B second, C third) {
        this.first = first;
        this.second = second;
        this.third = third;
    }

    public static  Triple of(A first, B second, C third) {
        return new Triple<>(first, second, third);
    }

    public A first() {
        return first;
    }

    public B second() {
        return second;
    }

    public C third() {
        return third;
    }

    public Triple withFirst(A a) {
        return new Triple<>(a, second, third);
    }

    public Triple withSecond(B b) {
        return new Triple<>(first, b, third);
    }

    public Triple withThird(C c) {
        return new Triple<>(first, second, c);
    }

    @Override
    public boolean equals(@Nullable Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        Triple triple = (Triple) o;

        if (first != null ? !first.equals(triple.first) : triple.first != null) return false;
        if (second != null ? !second.equals(triple.second) : triple.second != null) return false;
        return !(third != null ? !third.equals(triple.third) : triple.third != null);

    }

    @Override
    public int hashCode() {
        int result = first != null ? first.hashCode() : 0;
        result = 31 * result + (second != null ? second.hashCode() : 0);
        result = 31 * result + (third != null ? third.hashCode() : 0);
        return result;
    }

    @Override
    public String toString() {
        return new StringJoiner(", ", "(", ")").add(String.valueOf(first)).add(String.valueOf((second)))
                .add(String.valueOf((third))).toString();
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy