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

com.github.romanqed.jutils.util.Pair Maven / Gradle / Ivy

Go to download

A set of simple utilities for implementing missing functionality in different versions of Java.

There is a newer version: 4.0.0
Show newest version
package com.github.romanqed.jutils.util;

import java.util.Objects;

public class Pair {
    protected final K key;
    protected final V value;

    public Pair(K key, V value) {
        this.key = key;
        this.value = value;
    }

    public K getKey() {
        return key;
    }

    public V getValue() {
        return value;
    }

    @Override
    public String toString() {
        return key + "=" + value;
    }

    @Override
    public int hashCode() {
        return Checks.requireNonNullElse(key.hashCode(), 0) * 13 +
                Checks.requireNonNullElse(value.hashCode(), 0);
    }

    @Override
    @SuppressWarnings("unchecked")
    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }
        if (!(o instanceof Pair)) {
            return false;
        }
        Pair pair;
        try {
            pair = (Pair) o;
        } catch (Exception e) {
            return false;
        }
        return Objects.equals(key, pair.key) && Objects.equals(value, pair.value);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy