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

no.digipost.tuple.XTuple Maven / Gradle / Ivy

The newest version!
/*
 * Copyright (C) Posten Norge AS
 *
 * 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 no.digipost.tuple;

import no.digipost.function.DecaFunction;
import no.digipost.function.HexaFunction;
import no.digipost.function.NonaFunction;
import no.digipost.function.OctoFunction;
import no.digipost.function.PentaFunction;
import no.digipost.function.QuadFunction;
import no.digipost.function.SeptiFunction;
import no.digipost.function.TriFunction;

import java.io.Serializable;
import java.util.Objects;
import java.util.StringJoiner;
import java.util.function.BiFunction;
import java.util.function.Function;

import static java.util.function.Function.identity;

final class XTuple implements Tuple,
                                                                       Triple,
                                                                       Quadruple,
                                                                       Pentuple,
                                                                       Hextuple,
                                                                       Septuple,
                                                                       Octuple,
                                                                       Nonuple,
                                                                       Decuple,
                                                                       Serializable {

    private static final long serialVersionUID = 1L;

    static final Serializable TERMINATOR = new Serializable() {
        private static final long serialVersionUID = 1L;

        private Object readResolve() {
            return TERMINATOR;
        }

        @Override
        public String toString() {
            return "[" + XTuple.class.getSimpleName() + "-terminator element. Seeing this text indicates a bug in the Digg library.";
        };
    };



    private final T1 _1;
    private final T2 _2;
    private final T3 _3;
    private final T4 _4;
    private final T5 _5;
    private final T6 _6;
    private final T7 _7;
    private final T8 _8;
    private final T9 _9;
    private final T10 _10;


    XTuple(T1 _1, T2 _2, T3 _3, T4 _4, T5 _5, T6 _6, T7 _7, T8 _8, T9 _9, T10 _10) {
        this._1 = _1;
        this._2 = _2;
        this._3 = _3;
        this._4 = _4;
        this._5 = _5;
        this._6 = _6;
        this._7 = _7;
        this._8 = _8;
        this._9 = _9;
        this._10 = _10;
    }

    @Override
    public T1 first() {
        return _1;
    }

    @Override
    public T2 second() {
        return _2;
    }

    @Override
    public T3 third() {
        return _3;
    }

    @Override
    public T4 fourth() {
        return _4;
    }

    @Override
    public T5 fifth() {
        return _5;
    }

    @Override
    public T6 sixth() {
        return _6;
    }

    @Override
    public T7 seventh() {
        return _7;
    }

    @Override
    public T8 eighth() {
        return _8;
    }

    @Override
    public T9 ninth() {
        return _9;
    }

    @Override
    public T10 tenth() {
        return _10;
    }


    @Override
    public  XTuple mapFirst(Function mapper) {
        return map(mapper, identity());
    }

    @Override
    public  XTuple mapSecond(Function mapper) {
        return map(identity(), mapper);
    }

    @Override
    public  XTuple mapThird(Function mapper) {
        return map(identity(), identity(), mapper);
    }

    @Override
    public  XTuple mapFourth(Function mapper) {
        return map(identity(), identity(), identity(), mapper);
    }

    @Override
    public  XTuple mapFifth(Function mapper) {
        return map(identity(), identity(), identity(), identity(), mapper);
    }

    @Override
    public  XTuple mapSixth(Function mapper) {
        return map(identity(), identity(), identity(), identity(), identity(), mapper);
    }

    @Override
    public  XTuple mapSeventh(Function mapper) {
        return map(identity(), identity(), identity(), identity(), identity(), identity(), mapper);
    }

    @Override
    public  XTuple mapEighth(Function mapper) {
        return map(identity(), identity(), identity(), identity(), identity(), identity(), identity(), mapper);
    }

    @Override
    public  XTuple mapNinth(Function mapper) {
        return map(identity(), identity(), identity(), identity(), identity(), identity(), identity(), identity(), mapper);
    }

    @Override
    public  XTuple mapTenth(Function mapper) {
        return map(identity(), identity(), identity(), identity(), identity(), identity(), identity(), identity(), identity(), mapper);
    }


    @Override
    public  XTuple map(Function firstMapper,
                                                                        Function secondMapper) {
        return map(firstMapper, secondMapper, identity());
    }

    @Override
    public  XTuple map(Function firstMapper,
                                                                            Function secondMapper,
                                                                            Function thirdMapper) {
        return map(firstMapper, secondMapper, thirdMapper, identity());
    }

    @Override
    public  XTuple map(Function firstMapper,
                                                                                Function secondMapper,
                                                                                Function thirdMapper,
                                                                                Function fourthMapper) {
        return map(firstMapper, secondMapper, thirdMapper, fourthMapper, identity());
    }

    @Override
    public  XTuple map(Function firstMapper,
                                                                                    Function secondMapper,
                                                                                    Function thirdMapper,
                                                                                    Function fourthMapper,
                                                                                    Function fifthMapper) {
        return map(firstMapper, secondMapper, thirdMapper, fourthMapper, fifthMapper, identity());
    }

    @Override
    public  XTuple map(Function firstMapper,
                                                                                        Function secondMapper,
                                                                                        Function thirdMapper,
                                                                                        Function fourthMapper,
                                                                                        Function fifthMapper,
                                                                                        Function sixthMapper) {
        return map(firstMapper, secondMapper, thirdMapper, fourthMapper, fifthMapper, sixthMapper, identity());
    }

    @Override
    public  XTuple map(Function firstMapper,
                                                                                            Function secondMapper,
                                                                                            Function thirdMapper,
                                                                                            Function fourthMapper,
                                                                                            Function fifthMapper,
                                                                                            Function sixthMapper,
                                                                                            Function seventhMapper) {
        return map(firstMapper, secondMapper, thirdMapper, fourthMapper, fifthMapper, sixthMapper, seventhMapper, identity());
    }

    @Override
    public  XTuple map(Function firstMapper,
                                                                                                Function secondMapper,
                                                                                                Function thirdMapper,
                                                                                                Function fourthMapper,
                                                                                                Function fifthMapper,
                                                                                                Function sixthMapper,
                                                                                                Function seventhMapper,
                                                                                                Function eightMapper) {
        return map(firstMapper, secondMapper, thirdMapper, fourthMapper, fifthMapper, sixthMapper, seventhMapper, eightMapper, identity());
    }

    @Override
    public  XTuple map(Function firstMapper,
                                                                                                    Function secondMapper,
                                                                                                    Function thirdMapper,
                                                                                                    Function fourthMapper,
                                                                                                    Function fifthMapper,
                                                                                                    Function sixthMapper,
                                                                                                    Function seventhMapper,
                                                                                                    Function eightMapper,
                                                                                                    Function ninthMapper) {
        return map(firstMapper, secondMapper, thirdMapper, fourthMapper, fifthMapper, sixthMapper, seventhMapper, eightMapper, ninthMapper, identity());
    }

    @Override
    public  XTuple map(Function firstMapper,
                                                                                                         Function secondMapper,
                                                                                                         Function thirdMapper,
                                                                                                         Function fourthMapper,
                                                                                                         Function fifthMapper,
                                                                                                         Function sixthMapper,
                                                                                                         Function seventhMapper,
                                                                                                         Function eightMapper,
                                                                                                         Function ninthMapper,
                                                                                                         Function tenthMapper) {
        return new XTuple<>(firstMapper.apply(_1),
                            secondMapper.apply(_2),
                            thirdMapper.apply(_3),
                            fourthMapper.apply(_4),
                            fifthMapper.apply(_5),
                            sixthMapper.apply(_6),
                            seventhMapper.apply(_7),
                            eightMapper.apply(_8),
                            ninthMapper.apply(_9),
                            tenthMapper.apply(_10));
    }


    @Override
    public Decuple asDecuple() {
        return this;
    }

    @Override
    public Nonuple asNonuple() {
        return this;
    }

    @Override
    public Octuple asOctuple() {
        return this;
    }

    @Override
    public Septuple asSeptuple() {
        return this;
    }

    @Override
    public Hextuple asHextuple() {
        return this;
    }

    @Override
    public Pentuple asPentuple() {
        return this;
    }

    @Override
    public Quadruple asQuadruple() {
        return this;
    }

    @Override
    public Triple asTriple() {
        return this;
    }

    @Override
    public Tuple asTuple() {
        return this;
    }

    @Override
    public  R to(BiFunction convertor) {
        return convertor.apply(_1, _2);
    }

    @Override
    public  R to(TriFunction convertor) {
        return convertor.apply(_1, _2, _3);
    }

    @Override
    public  R to(QuadFunction convertor) {
        return convertor.apply(_1, _2, _3, _4);
    }

    @Override
    public  R to(PentaFunction convertor) {
        return convertor.apply(_1, _2, _3, _4, _5);
    }

    @Override
    public  R to(HexaFunction convertor) {
        return convertor.apply(_1, _2, _3, _4, _5, _6);
    }

    @Override
    public  R to(SeptiFunction convertor) {
        return convertor.apply(_1, _2, _3, _4, _5, _6, _7);
    }

    @Override
    public  R to(OctoFunction convertor) {
        return convertor.apply(_1, _2, _3, _4, _5, _6, _7, _8);
    }

    @Override
    public  R to(NonaFunction convertor) {
        return convertor.apply(_1, _2, _3, _4, _5, _6, _7, _8, _9);
    }

    @Override
    public  R to(DecaFunction convertor) {
        return convertor.apply(_1, _2, _3, _4, _5, _6, _7, _8, _9, _10);
    }


    @Override
    public boolean equals(Object obj) {
        if (obj instanceof XTuple) {
            XTuple that = (XTuple) obj;
            return Objects.equals(this._1, that._1) &&
                   Objects.equals(this._2, that._2) &&
                   Objects.equals(this._3, that._3) &&
                   Objects.equals(this._4, that._4) &&
                   Objects.equals(this._5, that._5) &&
                   Objects.equals(this._6, that._6) &&
                   Objects.equals(this._7, that._7) &&
                   Objects.equals(this._8, that._8) &&
                   Objects.equals(this._9, that._9) &&
                   Objects.equals(this._10, that._10);
        }
        return false;
    }

    @Override
    public int hashCode() {
        return Objects.hash(_1, _2, _3, _4, _5, _6, _7, _8, _9, _10);
    }

    @Override
    public String toString() {
        StringJoiner joiner = new StringJoiner(", ", "[", "]");
        for (Object element : new Object[] {_1, _2, _3, _4, _5, _6, _7, _8, _9, _10}) {
            if (element == TERMINATOR) break;
            joiner.add(String.valueOf(element));
        }
        return joiner.toString();
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy