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

com.msiops.footing.tuple.Pair Maven / Gradle / Ivy

The newest version!
/**
 * Licensed under the Apache License, Version 2.0 (the "License") under
 * one or more contributor license agreements. See the NOTICE file
 * distributed with this work for information regarding copyright
 * ownership. 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 com.msiops.footing.tuple;

import java.util.Objects;
import java.util.function.Function;

import com.msiops.footing.functional.Fun2;

public final class Pair {

    public final T1 _1;

    public final T2 _2;

    Pair(final T1 t1, final T2 t2) {
        super();
        this._1 = Objects.requireNonNull(t1);
        this._2 = Objects.requireNonNull(t2);
    }

    @Override
    public boolean equals(final Object obj) {

        final boolean rval;
        if (obj == this) {
            rval = true;
        } else if (obj instanceof Pair) {
            rval = this._1.equals(((Pair) obj)._1)
                    && this._2.equals(((Pair) obj)._2);
        } else {
            rval = false;
        }
        return rval;

    }

    @Override
    public int hashCode() {

        return Objects.hash(this._1, this._2);

    }

    public  T slice(final Function, T> sel) {
        return sel.apply(this);
    }

    public  Pair slice(final Function, T> sel1,
            final Function, U> sel2) {
        return new Pair<>(sel1.apply(this), sel2.apply(this));
    }

    public  Triplet slice(
            final Function, T> sel1,
            final Function, U> sel2,
            final Function, V> sel3) {
        return new Triplet<>(sel1.apply(this), sel2.apply(this),
                sel3.apply(this));
    }

    public  Tuple4 slice(
            final Function, T> sel1,
            final Function, U> sel2,
            final Function, V> sel3,
            final Function, W> sel4) {
        return new Tuple4<>(sel1.apply(this), sel2.apply(this),
                sel3.apply(this), sel4.apply(this));
    }

    public  Tuple5 slice(
            final Function, T> sel1,
            final Function, U> sel2,
            final Function, V> sel3,
            final Function, W> sel4,
            final Function, X> sel5) {
        return new Tuple5<>(sel1.apply(this), sel2.apply(this),
                sel3.apply(this), sel4.apply(this), sel5.apply(this));
    }

    public  R spread(final Fun2 f) {
        return f.apply(this._1, this._2);
    }

    @Override
    public String toString() {
        return new StringBuilder().append('(').append(this._1).append(',')
                .append(this._2).append(')').toString();
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy