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

net.calledtoconstruct.Tuple4 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 Tuple4 implements Tuple {

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

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

    /**
     * A function that returns a new {@link Tuple5} 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 Tuple5}
     */
    public  Tuple5 push(T5 value) {
        return new Tuple5<>(firstValue, secondValue, thirdValue, fourthValue, value);
    }

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

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

    /**
     * A function that returns a new {@link Tuple5} 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 Tuple5}
     */
    public  Tuple5 unshift(T5 value) {
        return new Tuple5<>(value, firstValue, secondValue, thirdValue, fourthValue);
    }

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

    /**
     * A function that returns the fourth value from this instance.
     * 
     * @return A value of type {@code T4}
     */
    public T4 getFourth() {
        return fourthValue;
    }

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

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

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

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

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

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

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

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

    @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(fourthValue.getClass())) {
            final var cast = clazz.cast(fourthValue);
            return Optional.of(cast);
        }
        return Optional.empty();
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy