-
Notifications
You must be signed in to change notification settings - Fork 738
Expand file tree
/
Copy pathPoint.java
More file actions
68 lines (54 loc) · 1.76 KB
/
Point.java
File metadata and controls
68 lines (54 loc) · 1.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package nextstep.ladder.domain;
import java.util.Objects;
public class Point {
private static final int MIN_INDEX = 0;
private final int index;
private final boolean left;
private final boolean right;
public Point(int index, boolean left, boolean right) {
validate(index);
validate(left, right);
this.index = index;
this.left = left;
this.right = right;
}
private void validate(int index) {
if (index < MIN_INDEX) {
throw new IllegalArgumentException("인덱스는 " + MIN_INDEX + "보다 작을 수 없습니다.");
}
}
private void validate(boolean left, boolean right) {
if (left && right) {
throw new IllegalArgumentException("좌 우 모두 이동 가능한 Point 생성 불가");
}
}
public static Point createLeftmost(boolean canMoveRight) {
return new Point(MIN_INDEX, false, canMoveRight);
}
public Point createRightmost() {
return new Point(this.index + 1, this.right, false);
}
public Point createNext(boolean canMoveRight) {
return new Point(this.index + 1, this.right, !this.right && canMoveRight);
}
public boolean canMoveLeft() {
return left;
}
public boolean canMoveRight() {
return right;
}
public boolean sameIndex(int index) {
return this.index == index;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Point point = (Point) o;
return index == point.index && left == point.left && right == point.right;
}
@Override
public int hashCode() {
return Objects.hash(index, left, right);
}
}