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

com.yahoo.collections.Pair Maven / Gradle / Ivy

Go to download

Library for use in Java components of Vespa. Shared code which do not fit anywhere else.

There is a newer version: 8.441.21
Show newest version
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.collections;

import java.util.Objects;

/**
 * An immutable pair of objects. This implements equals and hashCode by delegating to the
 * pair objects.
 *
 * @author bratseth
 */
public class Pair {

    /** The first member for the pair. May be null. */
    private final F first;
    /** The second member for the pair. May be null. */
    private final S second;

    /** Creates a pair. Each member may be set to null. */
    public Pair(final F first, final S second) {
        this.first = first;
        this.second = second;
    }

    /** Returns the first member. This may be null. */
    public F getFirst() { return first; }

    /** Returns the second member. This may be null. */
    public S getSecond() { return second; }

    @Override
    public int hashCode() {
        return ( first != null ? first.hashCode() : 0 ) +
               ( second != null ? 17*second.hashCode() : 0) ;
    }

    @Override
    public boolean equals(Object o) {
        if (o == this) return true;
        if (!(o instanceof Pair)) return false;

        @SuppressWarnings("rawtypes")
        Pair other = (Pair) o;
        return Objects.equals(this.first, other.first) && Objects.equals(this.second, other.second);
    }

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

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy