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

net.calledtoconstruct.Tuple3 Maven / Gradle / Ivy

Go to download

A small collection of potentially useful classes for streamlining process flow.

The newest version!
package net.calledtoconstruct;

import java.util.Optional;

public class Tuple3 implements Tuple {

    private final T1 firstValue;
    private final T2 secondValue;
    private final T3 thirdValue;

    public Tuple3(T1 firstValue, T2 secondValue, T3 thirdValue) {
        this.firstValue = firstValue;
        this.secondValue = secondValue;
        this.thirdValue = thirdValue;
    }

    /**
     * A function that returns a new {@link Tuple4} containing 
     * the values of this instance plus the supplied {@code value}.
     * 
     * @param  The type of {@code value} being added.
     * @param value A value to be added.
     * @return An instance of {@link Tuple4}
     */
    public  Tuple4 push(T4 value) {
        return new Tuple4<>(firstValue, secondValue, thirdValue, value);
    }

    /**
     * A function that returns a new instance of {@link Tuple2} containing the
     * first two values from this instance.
     * 
     * @return A {@link Tuple2} containing the first two values from this instance.
     */
    public Tuple2 pop() {
        return new Tuple2<>(firstValue, secondValue);
    }

    /**
     * A function that returns a new instance of {@link Tuple2} containing the
     * last two values from this instance.
     * 
     * @return A {@link Tuple2} containing the last two values from this instance.
     */
    public Tuple2 shift() {
        return new Tuple2<>(secondValue, thirdValue);
    }

    /**
     * A function that returns a new {@link Tuple4} containing 
     * the supplied {@code value} plus the values of this instance.
     * 
     * @param  The type of {@code value} being added.
     * @param value A value to be added.
     * @return An instance of {@link Tuple4}
     */
    public  Tuple4 unshift(T4 value) {
        return new Tuple4<>(value, firstValue, secondValue, thirdValue);
    }

    @Override
    public Optional tryPop() {
        return Optional.of(pop());
    }

    @Override
    public  Optional tryPush(T value) {
        return Optional.of(push(value));
    }

    @Override
    public Optional tryShift() {
        return Optional.of(shift());
    }

    @Override
    public  Optional tryUnshift(T value) {
        return Optional.of(unshift(value));
    }

    /**
     * A function that returns the first value from this instance.
     * 
     * @return A value of type {@code T1}
     */
    public T1 getFirst() {
        return firstValue;
    }

    /**
     * A function that returns the second value from this instance.
     * 
     * @return A value of type {@code T2}
     */
    public T2 getSecond() {
        return secondValue;
    }

    /**
     * A function that returns the third value from this instance.
     * 
     * @return A value of type {@code T3}
     */
    public T3 getThird() {
        return thirdValue;
    }

    public  Tuple4 append(final Tuple1 other) {
        return new Tuple4<>(firstValue, secondValue, thirdValue, other.getFirst());
    }

    public  Tuple5 append(final Tuple2 other) {
        return new Tuple5<>(firstValue, secondValue, thirdValue, other.getFirst(), other.getSecond());
    }

    public  Tuple6 append(final Tuple3 other) {
        return new Tuple6<>(firstValue, secondValue, thirdValue, other.getFirst(), other.getSecond(), other.getThird());
    }

    public  Tuple7 append(final Tuple4 other) {
        return new Tuple7<>(firstValue, secondValue, thirdValue, other.getFirst(), other.getSecond(), other.getThird(), other.getFourth());
    }

    public  Tuple8 append(final Tuple5 other) {
        return new Tuple8<>(firstValue, secondValue, thirdValue, other.getFirst(), other.getSecond(), other.getThird(), other.getFourth(), other.getFifth());
    }

    public  Tuple4 prepend(final Tuple1 other) {
        return new Tuple4<>(other.getFirst(), firstValue, secondValue, thirdValue);
    }

    public  Tuple5 prepend(final Tuple2 other) {
        return new Tuple5<>(other.getFirst(), other.getSecond(), firstValue, secondValue, thirdValue);
    }

    public  Tuple6 prepend(final Tuple3 other) {
        return new Tuple6<>(other.getFirst(), other.getSecond(), other.getThird(), firstValue, secondValue, thirdValue);
    }

    public  Tuple7 prepend(final Tuple4 other) {
        return new Tuple7<>(other.getFirst(), other.getSecond(), other.getThird(), other.getFourth(), firstValue, secondValue, thirdValue);
    }

    public  Tuple8 prepend(final Tuple5 other) {
        return new Tuple8<>(other.getFirst(), other.getSecond(), other.getThird(), other.getFourth(), other.getFifth(), firstValue, secondValue, thirdValue);
    }

    @Override
    public  Optional tryGetFirst(Class clazz) {
        if (clazz.isAssignableFrom(firstValue.getClass())) {
            final var cast = clazz.cast(firstValue);
            return Optional.of(cast);
        }
        return Optional.empty();
    }

    @Override
    public  Optional tryGetLast(Class clazz) {
        if (clazz.isAssignableFrom(thirdValue.getClass())) {
            final var cast = clazz.cast(thirdValue);
            return Optional.of(cast);
        }
        return Optional.empty();
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy