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

com.landawn.abacus.util.Triple Maven / Gradle / Ivy

Go to download

A general programming library in Java/Android. It's easy to learn and simple to use with concise and powerful APIs.

There is a newer version: 5.3.16
Show newest version
/*
 * Copyright (c) 2015, Haiyang Li.
 *
 * 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 com.landawn.abacus.util;

import com.landawn.abacus.annotation.Beta;
import com.landawn.abacus.util.Tuple.Tuple3;
import com.landawn.abacus.util.u.Optional;
import com.landawn.abacus.util.stream.Stream;

/**
 *
 * @author Haiyang Li
 * @param 
 * @param 
 * @param 
 * @since 0.8
 */
public final class Triple implements Mutable {

    public L left;

    public M middle;

    public R right;

    public Triple() {
    }

    Triple(final L l, final M m, final R r) {
        this.left = l;
        this.middle = m;
        this.right = r;
    }

    /**
     *
     * @param 
     * @param 
     * @param 
     * @param l
     * @param m
     * @param r
     * @return
     */
    public static  Triple of(final L l, final M m, final R r) {
        return new Triple<>(l, m, r);
    }

    private static final Triple[] EMPTY_ARRAY = new Triple[0];

    @SuppressWarnings("unchecked")
    public static  Triple[] emptyArray() {
        return (Triple[]) EMPTY_ARRAY;
    }

    /**
     * Gets the left.
     *
     * @return
     */
    public L getLeft() {
        return left;
    }

    /**
     * Sets the left.
     *
     * @param left the new left
     */
    public void setLeft(final L left) {
        this.left = left;
    }

    /**
     * Gets the middle.
     *
     * @return
     */
    public M getMiddle() {
        return middle;
    }

    /**
     * Sets the middle.
     *
     * @param middle the new middle
     */
    public void setMiddle(final M middle) {
        this.middle = middle;
    }

    /**
     * Gets the right.
     *
     * @return
     */
    public R getRight() {
        return right;
    }

    /**
     * Sets the right.
     *
     * @param right the new right
     */
    public void setRight(final R right) {
        this.right = right;
    }

    /**
     *
     * @param left
     * @param middle
     * @param right
     */
    public void set(final L left, final M middle, final R right) {
        this.left = left;
        this.middle = middle;
        this.right = right;
    }

    /**
     * Gets the and set left.
     *
     * @param newLeft
     * @return
     */
    public L getAndSetLeft(L newLeft) {
        final L res = left;
        left = newLeft;
        return res;
    }

    /**
     * Sets the and get left.
     *
     * @param newLeft
     * @return
     */
    public L setAndGetLeft(L newLeft) {
        left = newLeft;
        return left;
    }

    /**
     * Gets the and set middle.
     *
     * @param newMiddle
     * @return
     */
    public M getAndSetMiddle(M newMiddle) {
        final M res = middle;
        middle = newMiddle;
        return res;
    }

    /**
     * Sets the and get middle.
     *
     * @param newMiddle
     * @return
     */
    public M setAndGetMiddle(M newMiddle) {
        middle = newMiddle;
        return middle;
    }

    /**
     * Gets the and set right.
     *
     * @param newRight
     * @return
     */
    public R getAndSetRight(R newRight) {
        final R res = newRight;
        right = newRight;
        return res;
    }

    /**
     * Sets the and get right.
     *
     * @param newRight
     * @return
     */
    public R setAndGetRight(R newRight) {
        right = newRight;
        return right;
    }

    /**
     * Set to the specified newLeft and returns true
     * if predicate returns true. Otherwise returns
     * false without setting the value to new value.
     *
     * @param 
     * @param newLeft
     * @param predicate - the first parameter is current pair, the second
     *        parameter is the newLeft
     * @return
     * @throws E the e
     */
    public  boolean setLeftIf(final L newLeft, Throwables.BiPredicate, ? super L, E> predicate) throws E {
        if (predicate.test(this, newLeft)) {
            this.left = newLeft;
            return true;
        }

        return false;
    }

    /**
     * Set to the specified newMiddle and returns true
     * if predicate returns true. Otherwise returns
     * false without setting the value to new value.
     *
     * @param 
     * @param newMiddle
     * @param predicate - the first parameter is current pair, the second
     *        parameter is the newMiddle
     * @return
     * @throws E the e
     */
    public  boolean setMiddleIf(final M newMiddle, Throwables.BiPredicate, ? super M, E> predicate) throws E {
        if (predicate.test(this, newMiddle)) {
            this.middle = newMiddle;
            return true;
        }

        return false;
    }

    /**
     * Set to the specified newRight and returns true
     * if predicate returns true. Otherwise returns
     * false without setting the value to new value.
     *
     * @param 
     * @param newRight
     * @param predicate - the first parameter is current pair, the second
     *        parameter is the newRight
     * @return
     * @throws E the e
     */
    public  boolean setRightIf(final R newRight, Throwables.BiPredicate, ? super R, E> predicate) throws E {
        if (predicate.test(this, newRight)) {
            this.right = newRight;
            return true;
        }

        return false;
    }

    /**
     * Set to the specified newLeft and newRight and
     * returns true if predicate returns true.
     * Otherwise returns false without setting the left/right to
     * new values.
     *
     * @param 
     * @param newLeft
     * @param newMiddle
     * @param newRight
     * @param predicate - the first parameter is current pair, the second
     *        parameter is the newLeft, the third parameter is the
     *        newMiddle, the fourth parameter is the
     *        newRight
     * @return
     * @throws E the e
     */
    public  boolean setIf(final L newLeft, final M newMiddle, final R newRight,
            Throwables.QuadPredicate, ? super L, ? super M, ? super R, E> predicate) throws E {
        if (predicate.test(this, newLeft, newMiddle, newRight)) {
            this.left = newLeft;
            this.middle = newMiddle;
            this.right = newRight;
            return true;
        }

        return false;
    }

    //    /**
    //     * Swap the left and right value. they must be same type.
    //     */
    //    public void reverse() {
    //        Object tmp = left;
    //        this.left = (L) right;
    //        this.right = (R) tmp;
    //    }

    /**
     * Returns a new instance of Triple<R, M, L>.
     *
     * @return a new instance of Triple<R, M, L>.
     */
    @Beta
    public Triple reverse() {
        return new Triple<>(this.right, this.middle, this.left);
    }

    public Triple copy() {
        return new Triple<>(this.left, this.middle, this.right);
    }

    public Object[] toArray() {
        return new Object[] { left, middle, right };
    }

    /**
     *
     * @param 
     * @param a
     * @return
     */
    public  A[] toArray(A[] a) {
        if (a.length < 3) {
            a = N.copyOf(a, 3);
        }

        a[0] = (A) left;
        a[1] = (A) middle;
        a[2] = (A) right;

        return a;
    }

    /**
     *
     * @param 
     * @param comsumer
     * @throws E the e
     */
    public  void forEach(Throwables.Consumer comsumer) throws E {
        final Throwables.Consumer objComsumer = (Throwables.Consumer) comsumer;

        objComsumer.accept(left);
        objComsumer.accept(middle);
        objComsumer.accept(right);
    }

    /**
     *
     * @param 
     * @param action
     * @throws E the e
     */
    public  void accept(final Throwables.TriConsumer action) throws E {
        action.accept(left, middle, right);
    }

    /**
     *
     * @param 
     * @param action
     * @throws E the e
     */
    public  void accept(final Throwables.Consumer, E> action) throws E {
        action.accept(this);
    }

    /**
     *
     * @param 
     * @param 
     * @param mapper
     * @return
     * @throws E the e
     */
    public  U map(final Throwables.TriFunction mapper) throws E {
        return mapper.apply(left, middle, right);
    }

    /**
     *
     * @param 
     * @param 
     * @param mapper
     * @return
     * @throws E the e
     */
    public  U map(final Throwables.Function, U, E> mapper) throws E {
        return mapper.apply(this);
    }

    /**
     *
     * @param 
     * @param predicate
     * @return
     * @throws E the e
     */
    public  Optional> filter(final Throwables.TriPredicate predicate) throws E {
        return predicate.test(left, middle, right) ? Optional.of(this) : Optional.> empty();
    }

    /**
     *
     * @param 
     * @param predicate
     * @return
     * @throws E the e
     */
    public  Optional> filter(final Throwables.Predicate, E> predicate) throws E {
        return predicate.test(this) ? Optional.of(this) : Optional.> empty();
    }

    public Stream> stream() {
        return Stream.of(this);
    }

    public  Stream stream(final Throwables.Function, Stream, E> func) throws E {
        return func.apply(this);
    }

    public Optional> toOptional() {
        return Optional.of(this);
    }

    public Tuple3 toTuple() {
        return Tuple.of(left, middle, right);
    }

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + N.hashCode(left);
        result = prime * result + N.hashCode(middle);
        return prime * result + N.hashCode(right);
    }

    /**
     *
     * @param obj
     * @return
     */
    @Override
    public boolean equals(final Object obj) {
        if (this == obj) {
            return true;
        }

        if (obj instanceof Triple) {
            final Triple other = (Triple) obj;

            return N.equals(left, other.left) && N.equals(middle, other.middle) && N.equals(right, other.right);
        }

        return false;
    }

    @Override
    public String toString() {
        return "[" + N.toString(left) + ", " + N.toString(middle) + ", " + N.toString(right) + "]";
    }
}