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

no.digipost.tuple.Tuple 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 java.util.Map;
import java.util.function.BiFunction;
import java.util.function.Function;

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

/**
 * A tuple is a simple composition of two arbitrary values (objects). A tuple
 * captures no semantics of the two values, and they are only referred to as
 * "the first" and "the second" value.
 *
 * @see ViewableAsTuple
 *
 * @param  The type of the first value
 * @param  The type of the second value
 */
public interface Tuple extends ViewableAsTuple {

    static  Tuple ofMapEntry(Map.Entry mapEntry) {
        return of(mapEntry.getKey(), mapEntry.getValue());
    }

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


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


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


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


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


    /**
     * Create a new tuple by applying a function to each element, and putting the
     * results to corresponding positions in the new tuple.
     *
     * @param firstMapper the function to apply to the first element
     * @param secondMapper the function to apply to the second element
     * @return the new tuple
     */
     Tuple map(Function firstMapper, Function secondMapper);


    /**
     * @return a new tuple with the same elements in swapped positions.
     */
    default Tuple swap() {
        return Tuple.of(second(), first());
    }


    /**
     * @return this tuple instance.
     */
    @Override
    Tuple asTuple();


    /**
     * Convert this tuple 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(BiFunction convertor);

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy