-
Notifications
You must be signed in to change notification settings - Fork 738
Expand file tree
/
Copy pathPosition.java
More file actions
68 lines (54 loc) · 1.85 KB
/
Position.java
File metadata and controls
68 lines (54 loc) · 1.85 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.ladder2.domain.ladder;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
public class Position {
private int value;
private final int max;
private Position(int value, int max) {
if (value < 0 || value >= max) {
throw new IllegalArgumentException("유효하지 않은 위치입니다: v=" + value + ", max=" + max);
}
this.value = value;
this.max = max;
}
public static Position of(int value, int max) {
return new Position(value, max);
}
public static List<Position> listOf(int max, int... values) {
return Arrays.stream(values)
.mapToObj(value -> new Position(value, max))
.collect(Collectors.toList());
}
public static List<Position> range(int start, int end, int max) {
return IntStream.range(start, end)
.mapToObj(value -> new Position(value, max))
.collect(Collectors.toList());
}
public void moveBy(Direction direction) {
int target = value + direction.getValue();
validatePosition(target);
value = target;
}
private void validatePosition(int value) {
if (value < 0 || value >= max) {
throw new IllegalArgumentException("유효하지 않은 위치입니다: v=" + value + ", max=" + max);
}
}
public int value() {
return value;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Position position = (Position) o;
return value == position.value && max == position.max;
}
@Override
public int hashCode() {
return Objects.hash(value, max);
}
}