package cs3500.connectn.model; import java.util.Comparator; import java.util.Objects; /** * A 2-D integer grid position. */ public final class Position implements Comparable { /** * Constructs a new {@code Position} with the given column and row. * * @param x the column * @param y the row * @return the new {@code Position} */ public static Position posn(int x, int y) { return new Position(x, y); } private Position(int x, int y) { this.x = x; this.y = y; } public int x() { return x; } private final int x; public int y() { return y; } private final int y; @Override public String toString() { return String.format("(%d, %d)", x, y); } @Override public boolean equals(Object o) { if (this == o) return true; if (!(o instanceof Position)) return false; Position other = (Position) o; return this.x == other.x && this.y == other.y; } @Override public int hashCode() { return Objects.hash(x, y); } /** * Compares to another {@code Position} using lexicographic order. * *

* {@inheritDoc} */ @Override public int compareTo(Position other) { return COMPARATOR.compare(this, other); } private static final Comparator COMPARATOR = Comparator .comparingInt(Position::y) .thenComparingInt(Position::x); }