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

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

There is a newer version: 0.36
Show 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.TriFunction;

import java.util.function.Function;

import static no.digipost.tuple.XTuple.TERMINATOR;

/**
 * A triple is a simple composition of three arbitrary values (objects). A triple
 * captures no semantics of the three values, and they are only referred to as
 * "the first", "the second", and "the third" value.
 *
 * @param  The type of the first value
 * @param  The type of the second value
 * @param  The type of the third value
 */
public interface Triple extends ViewableAsTriple {

    static  Triple of(T1 first, T2 second, T3 third) {
        return new XTuple<>(first, second, third, TERMINATOR, null, null, null, null, null, null);
    }

    static  Triple flatten(Tuple, T3> nestedTuple) {
        return Triple.of(nestedTuple.first().first(),
                         nestedTuple.first().second(),
                         nestedTuple.second());
    }

    /**
     * @return the first value
     */
    T1 first();

    /**
     * @return the second value
     */
    T2 second();

    /**
     * @return the third value
     */
    T3 third();


    /**
     * Create a new triple by applying a function to the first element, and putting the
     * result as the first element of the new triple.
     *
     * @param mapper the function to apply to the first element
     * @return the new triple
     */
     Triple mapFirst(Function mapper);


    /**
     * Create a new triple by applying a function to the second element, and putting the
     * result as the second element of the new triple.
     *
     * @param mapper the function to apply to the second element
     * @return the new triple
     */
     Triple mapSecond(Function mapper);


    /**
     * Create a new triple by applying a function to the third element, and putting the
     * result as the third element of the new triple.
     *
     * @param mapper the function to apply to the third element
     * @return the new triple
     */
     Triple mapThird(Function mapper);


    /**
     * Create a new triple by applying a function to each element, and putting the
     * results into a new triple.
     *
     * @param firstMapper  the function to apply to the first element
     * @param secondMapper the function to apply to the second element
     * @param thirdMapper  the function to apply to the third element
     * @return the new triple
     */
     Triple map(Function firstMapper,
                                        Function secondMapper,
                                        Function thirdMapper);


    /**
     * @return this triple instance
     */
    @Override
    Triple asTriple();


    /**
     * Convert this triple to an instance of an arbitrary type.
     *
     * @param  The type of the resulting instance
     * @param convertor the function used to convert the contained
     *                  values to a resulting compound instance.
     * @return the result from the given function
     */
     R to(TriFunction convertor);

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy