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

com.shapesecurity.functional.Pair Maven / Gradle / Ivy

/*
 * Copyright 2014 Shape Security, Inc.
 *
 * 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 com.shapesecurity.functional;

import com.shapesecurity.functional.data.HashCodeBuilder;

import org.jetbrains.annotations.NotNull;

public final class Pair {
    public final A left;
    public final B right;

    /**
     * Constructor method to utilize type inference.
     * @param left first component
     * @param right second component
     * @param  type of the first component
     * @param  type of the second component
     * @return the pair
     */
    @NotNull
    public static  Pair of(A left, B right) {
        return new Pair<>(left, right);
    }

    public Pair(A left, B right) {
        super();
        this.left = left;
        this.right = right;
    }

    public A left() {
        return this.left;
    }

    public B right() {
        return this.right;
    }

    @NotNull
    public Pair swap() {
        return new Pair<>(this.right, this.left);
    }

    @NotNull
    public  Pair mapLeft(@NotNull F f) {
        return new Pair<>(f.apply(this.left), this.right);
    }

    @NotNull
    public  Pair mapRight(@NotNull F f) {
        return new Pair<>(this.left, f.apply(this.right));
    }

    @SuppressWarnings("unchecked")
    @Override
    public boolean equals(Object obj) {
        return obj == this || obj instanceof Pair &&
                ((Pair) obj).left.equals(this.left) &&
                ((Pair) obj).right.equals(this.right);
    }

    @Override
    public int hashCode() {
        int hash = HashCodeBuilder.put(HashCodeBuilder.init(), "Pair");
        hash = HashCodeBuilder.put(hash, this.left);
        return HashCodeBuilder.put(hash, this.right);
    }
}





© 2015 - 2025 Weber Informatics LLC | Privacy Policy