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

panda.std.Pair Maven / Gradle / Ivy

The newest version!
/*
 * Copyright (c) 2021 dzikoysk
 *  
 * 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 panda.std;

import java.util.Objects;

public class Pair {

    protected final A first;
    protected final B second;

    public Pair(A first, B second) {
        this.first = first;
        this.second = second;
    }

    public static  Pair of(A first, B second) {
        return new Pair<>(first, second);
    }

    public  Triple add(C third) {
        return Triple.of(first, second, third);
    }

    public  Pair withFirst(R newValue) {
        return new Pair<>(newValue, second);
    }

    public  Pair withSecond(R newValue) {
        return new Pair<>(first, newValue);
    }

    public A getFirst() {
        return first;
    }

    public B getSecond() {
        return second;
    }

    @Override
    public boolean equals(Object to) {
        if (this == to) {
            return true;
        }

        if (to == null || getClass() != to.getClass()) {
            return false;
        }

        Pair pair = (Pair) to;
        return Objects.equals(first, pair.first) && Objects.equals(second, pair.second);
    }

    @Override
    public int hashCode() {
        return Objects.hash(first, second);
    }

    @Override
    public String toString() {
        return "['" + first + "', '" + second + "']";
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy