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

org.checkerframework.javacutil.Pair Maven / Gradle / Ivy

Go to download

The Checker Framework enhances Java's type system to make it more powerful and useful. This lets software developers detect and prevent errors in their Java programs. The Checker Framework includes compiler plug-ins ("checkers") that find bugs or verify their absence. It also permits you to write your own compiler plug-ins.

There is a newer version: 3.42.0-eisop4
Show newest version
package org.checkerframework.javacutil;

import org.checkerframework.checker.nullness.qual.Nullable;
import org.checkerframework.dataflow.qual.Pure;
import org.checkerframework.dataflow.qual.SideEffectFree;
import org.plumelib.util.UtilPlume;

import java.util.Objects;

// The class type variables are called V1 and V2 so that T1 and T2 can be used for method type
// variables.
/**
 * Immutable pair class.
 *
 * @param  the type of the first element of the pair
 * @param  the type of the second element of the pair
 * @deprecated use org.plumelib.util.IPair
 */
@Deprecated // 2023-06-02
// TODO: as class is immutable, use @Covariant annotation.
public class Pair {
    /** The first element of the pair. */
    public final V1 first;

    /** The second element of the pair. */
    public final V2 second;

    private Pair(V1 first, V2 second) {
        this.first = first;
        this.second = second;
    }

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

    // The typical way to make a copy is to first call super.clone() and then modify it.
    // That implementation strategy does not work for Pair because its fields are final, so the
    // clone
    // and deepCopy() methods use of() instead.

    /**
     * Returns a copy of this in which each element is a clone of the corresponding element of this.
     * {@code clone()} may or may not itself make a deep copy of the elements.
     *
     * @param  the type of the first element of the pair
     * @param  the type of the second element of the pair
     * @param orig a pair
     * @return a copy of {@code orig}, with all elements cloned
     */
    // This method is static so that the pair element types can be constrained to be Cloneable.
    @SuppressWarnings("nullness") // generics problem with deepCopy()
    public static  Pair cloneElements(
            Pair orig) {

        T1 oldFirst = orig.first;
        T1 newFirst = oldFirst == null ? oldFirst : UtilPlume.clone(oldFirst);
        T2 oldSecond = orig.second;
        T2 newSecond = oldSecond == null ? oldSecond : UtilPlume.clone(oldSecond);
        return of(newFirst, newSecond);
    }

    /**
     * Returns a deep copy of this: each element is a deep copy (according to the {@code
     * DeepCopyable} interface) of the corresponding element of this.
     *
     * @param  the type of the first element of the pair
     * @param  the type of the second element of the pair
     * @param orig a pair
     * @return a deep copy of {@code orig}
     */
    @SuppressWarnings("nullness") // generics problem with deepCopy()
    // This method is static so that the pair element types can be constrained to be DeepCopyable.
    public static , T2 extends DeepCopyable> Pair deepCopy(
            Pair orig) {
        return of(
                DeepCopyable.deepCopyOrNull(orig.first), DeepCopyable.deepCopyOrNull(orig.second));
    }

    /**
     * Returns a copy, where the {@code first} element is deep: the {@code first} element is a deep
     * copy (according to the {@code DeepCopyable} interface), and the {@code second} element is
     * identical to the argument.
     *
     * @param  the type of the first element of the pair
     * @param  the type of the second element of the pair
     * @param orig a pair
     * @return a copy of {@code orig}, where the first element is a deep copy
     */
    @SuppressWarnings("nullness") // generics problem with deepCopy()
    public static , T2> Pair deepCopyFirst(Pair orig) {
        return of(DeepCopyable.deepCopyOrNull(orig.first), orig.second);
    }

    /**
     * Returns a copy, where the {@code second} element is deep: the {@code first} element is
     * identical to the argument, and the {@code second} element is a deep copy (according to the
     * {@code DeepCopyable} interface).
     *
     * @param  the type of the first element of the pair
     * @param  the type of the second element of the pair
     * @param orig a pair
     * @return a copy of {@code orig}, where the second element is a deep copy
     */
    @SuppressWarnings("nullness") // generics problem with deepCopy()
    public static > Pair deepCopySecond(Pair orig) {
        return of(orig.first, DeepCopyable.deepCopyOrNull(orig.second));
    }

    @Override
    @Pure
    public boolean equals(@Nullable Object obj) {
        if (this == obj) {
            return true;
        }
        if (!(obj instanceof Pair)) {
            return false;
        }
        // generics are not checked at run time!
        @SuppressWarnings("unchecked")
        Pair other = (Pair) obj;
        return Objects.equals(this.first, other.first) && Objects.equals(this.second, other.second);
    }

    /** The cached hash code. -1 means it needs to be computed. */
    private int hashCode = -1;

    @Pure
    @Override
    public int hashCode() {
        if (hashCode == -1) {
            hashCode = Objects.hash(first, second);
        }
        return hashCode;
    }

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




© 2015 - 2024 Weber Informatics LLC | Privacy Policy