1 package io.github.robertograham.rleparser.domain;
2
3 import java.util.Objects;
4
5 public class Coordinate {
6
7 private final int x;
8 private final int y;
9
10 public Coordinate(int x, int y) {
11 this.x = x;
12 this.y = y;
13 }
14
15 public int getX() {
16 return x;
17 }
18
19 public int getY() {
20 return y;
21 }
22
23 public Coordinate withX(int x) {
24 return this.x == x ? this : new Coordinate(x, y);
25 }
26
27 public Coordinate withY(int y) {
28 return this.y == y ? this : new Coordinate(x, y);
29 }
30
31 public Coordinate plusToX(int amount) {
32 return amount == 0 ? this : withX(x + amount);
33 }
34
35 public Coordinate plusToY(int amount) {
36 return amount == 0 ? this : withY(y + amount);
37 }
38
39 @Override
40 public String toString() {
41 return "Coordinate{" +
42 "x=" + x +
43 ", y=" + y +
44 '}';
45 }
46
47 @Override
48 public boolean equals(Object object) {
49 if (this == object)
50 return true;
51
52 if (!(object instanceof Coordinate))
53 return false;
54
55 Coordinate coordinate = (Coordinate) object;
56
57 return x == coordinate.x &&
58 y == coordinate.y;
59 }
60
61 @Override
62 public int hashCode() {
63 return Objects.hash(x, y);
64 }
65 }