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

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

There is a newer version: 1.8.1
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 java.util.Collection;
import java.util.Iterator;
import java.util.List;

import com.landawn.abacus.util.Tuple.Tuple3;
import com.landawn.abacus.util.function.BiPredicate;
import com.landawn.abacus.util.function.Consumer;
import com.landawn.abacus.util.function.Function;
import com.landawn.abacus.util.function.Predicate;
import com.landawn.abacus.util.function.QuadPredicate;
import com.landawn.abacus.util.function.TriConsumer;
import com.landawn.abacus.util.function.TriFunction;
import com.landawn.abacus.util.function.TriPredicate;
import com.landawn.abacus.util.stream.Stream;

/**
 * 
 * @since 0.8
 * 
 * @author Haiyang Li
 *
 * @param 
 * @param 
 * @param 
 */
public final class Triple {
    public volatile L left;
    public volatile M middle;
    public volatile R right;

    public Triple() {
    }

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

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

    public static  Triple from(T[] a) {
        if (N.isNullOrEmpty(a)) {
            return new Triple<>(null, null, null);
        } else if (a.length == 1) {
            return new Triple<>(a[0], null, null);
        } else if (a.length == 2) {
            return new Triple<>(a[0], a[1], null);
        } else {
            return new Triple<>(a[0], a[1], a[2]);
        }
    }

    public static  Triple from(Collection c) {
        if (N.isNullOrEmpty(c)) {
            return new Triple<>(null, null, null);
        }

        final List list = c instanceof List ? (List) c : null;

        if (c.size() == 1) {
            if (list != null) {
                return new Triple(list.get(0), null, null);
            } else {
                return new Triple(c.iterator().next(), null, null);
            }
        } else if (c.size() == 2) {
            if (list != null) {
                return new Triple(list.get(0), list.get(1), null);
            } else {
                final Iterator iter = c.iterator();
                return new Triple(iter.next(), iter.next(), null);
            }
        } else {
            if (list != null) {
                return new Triple(list.get(0), list.get(1), list.get(2));
            } else {
                final Iterator iter = c.iterator();
                return new Triple(iter.next(), iter.next(), iter.next());
            }
        }
    }

    public L left() {
        return left;
    }

    public M middle() {
        return middle;
    }

    public R right() {
        return right;
    }

    public L getLeft() {
        return left;
    }

    public Triple setLeft(final L left) {
        this.left = left;

        return this;
    }

    public M getMiddle() {
        return middle;
    }

    public Triple setMiddle(final M middle) {
        this.middle = middle;

        return this;
    }

    public R getRight() {
        return right;
    }

    public Triple setRight(final R right) {
        this.right = right;

        return this;
    }

    public Triple set(final L left, final M middle, final R right) {
        this.left = left;
        this.middle = middle;
        this.right = right;

        return this;
    }

    public L getAndSetLeft(L newLeft) {
        final L res = left;
        left = newLeft;
        return res;
    }

    public L setAndGetLeft(L newLeft) {
        left = newLeft;
        return left;
    }

    public M getAndSetMiddle(M newMiddle) {
        final M res = middle;
        middle = newMiddle;
        return res;
    }

    public M setAndGetMiddle(M newMiddle) {
        middle = newMiddle;
        return middle;
    }

    public R getAndSetRight(R newRight) {
        final R res = newRight;
        right = newRight;
        return res;
    }

    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 newLeft
     * @param predicate - the first parameter is current pair, the second
     *        parameter is the newLeft
     * @return
     */
    public boolean setLeftIf(final L newLeft, BiPredicate, ? super L> predicate) {
        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 newMiddle
     * @param predicate - the first parameter is current pair, the second
     *        parameter is the newMiddle
     * @return
     */
    public boolean setMiddleIf(final M newMiddle, BiPredicate, ? super M> predicate) {
        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 newRight
     * @param predicate - the first parameter is current pair, the second
     *        parameter is the newRight
     * @return
     */
    public boolean setRightIf(final R newRight, BiPredicate, ? super R> predicate) {
        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 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
     */
    public boolean setIf(final L newLeft, final M newMiddle, final R newRight,
            QuadPredicate, ? super L, ? super M, ? super R> predicate) {
        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;
    //    }

    /**
     * 
     * @return a new instance of Triple<R, M, L>.
     */
    public Triple reversed() {
        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 };
    }

    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;
    }

    public void forEach(Consumer comsumer) {
        final Consumer objComsumer = (Consumer) comsumer;

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

    public void accept(final TriConsumer action) {
        action.accept(left, middle, right);
    }

    public void accept(final Consumer> action) {
        action.accept(this);
    }

    public  U map(final TriFunction mapper) {
        return mapper.apply(left, middle, right);
    }

    public  U map(final Function, U> mapper) {
        return mapper.apply(this);
    }

    public Optional> filter(final TriPredicate predicate) {
        return predicate.test(left, middle, right) ? Optional.of(this) : Optional.> empty();
    }

    public Optional> filter(final Predicate> predicate) {
        return predicate.test(this) ? Optional.of(this) : Optional.> empty();
    }

    public Stream> stream() {
        return Stream.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);
        result = prime * result + N.hashCode(right);
        return result;
    }

    @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) + "]";
    }
}