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

xdean.jex.extra.collection.Pair Maven / Gradle / Ivy

The newest version!
package xdean.jex.extra.collection;

import java.util.Objects;
import java.util.Optional;

import javax.annotation.concurrent.Immutable;

/**
 * @author XDean
 *
 * @param 
 * @param 
 */
@Immutable
public class Pair {
  private static final Pair EMPTY = new Pair<>(null, null);

  public static  Pair of(K k, V v) {
    return new Pair<>(k, v);
  }

  @SuppressWarnings("unchecked")
  public static  Pair empty() {
    return (Pair) EMPTY;
  }

  private final K left;
  private final V right;

  public Pair(K k, V v) {
    this.left = k;
    this.right = v;
  }

  public K getLeft() {
    return left;
  }

  public Optional toLeft() {
    return Optional.ofNullable(left);
  }

  public V getRight() {
    return right;
  }

  public Optional toRight() {
    return Optional.ofNullable(right);
  }

  public  Pair left(L left) {
    return Pair.of(left, right);
  }

  public  Pair right(R right) {
    return Pair.of(left, right);
  }

  @Override
  public int hashCode() {
    return Objects.hash(left, right);
  }

  @Override
  public boolean equals(Object obj) {
    if (this == obj) {
      return true;
    } else if (obj == null) {
      return false;
    } else if (!(obj instanceof Pair)) {
      return false;
    }
    Pair other = (Pair) obj;
    return Objects.equals(left, other.left) && Objects.equals(right, other.right);
  }

  @Override
  public String toString() {
    return "Pair [left=" + left + ", right=" + right + "]";
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy