Skip to content

Commit 9a91a2f

Browse files
committed
refactor: LineBuilder를 Line 내부로 이동
1 parent 9ad91e8 commit 9a91a2f

File tree

6 files changed

+69
-119
lines changed

6 files changed

+69
-119
lines changed

src/main/java/nextstep/ladder/domain/Line.java

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
package nextstep.ladder.domain;
22

3+
import java.util.ArrayList;
34
import java.util.List;
45

56
public class Line {
67
private static final int MIN_POINT_SIZE = 2;
78

89
private final List<Point> points;
910

10-
Line(List<Point> points) {
11+
private Line(List<Point> points) {
1112
validate(points);
1213
this.points = points;
1314
}
@@ -61,4 +62,35 @@ public Point getPoint(int index) {
6162
.findFirst()
6263
.orElseThrow(() -> new IllegalArgumentException("포인트가 없습니다. index: " + index));
6364
}
65+
66+
static class LineBuilder {
67+
private final List<Point> points;
68+
69+
public LineBuilder() {
70+
this.points = new ArrayList<>();
71+
}
72+
73+
public LineBuilder point(boolean canMoveRight) {
74+
if (points.isEmpty()) {
75+
points.add(Point.createLeftmost(canMoveRight));
76+
return this;
77+
}
78+
points.add(lastPoint().createNext(canMoveRight));
79+
return this;
80+
}
81+
82+
public Line build() {
83+
if (points.isEmpty()) {
84+
throw new IllegalStateException("점이 없습니다.");
85+
}
86+
87+
points.add(lastPoint().createRightmost());
88+
89+
return new Line(points);
90+
}
91+
92+
private Point lastPoint() {
93+
return points.get(points.size() - 1);
94+
}
95+
}
6496
}

src/main/java/nextstep/ladder/domain/LineBuilder.java

Lines changed: 0 additions & 40 deletions
This file was deleted.

src/main/java/nextstep/ladder/domain/Lines.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
package nextstep.ladder.domain;
22

33
import java.util.List;
4+
import java.util.Random;
45
import java.util.stream.IntStream;
56

67
import static java.util.stream.Collectors.collectingAndThen;
78
import static java.util.stream.Collectors.toList;
89

910
public class Lines {
11+
private static final Random RANDOM = new Random();
1012

1113
private final List<Line> lines;
1214

@@ -23,10 +25,19 @@ private void validate(List<Line> lines) {
2325

2426
public static Lines of(int numberOfPlayers, int height) {
2527
return IntStream.range(0, height)
26-
.mapToObj(i -> LineBuilder.buildWithRandomPoints(numberOfPlayers))
28+
.mapToObj(i -> getBuildWithRandomPoints(numberOfPlayers))
2729
.collect(collectingAndThen(toList(), Lines::new));
2830
}
2931

32+
private static Line getBuildWithRandomPoints(int numberOfPlayers) {
33+
Line.LineBuilder builder = new Line.LineBuilder();
34+
builder.point(RANDOM.nextBoolean());
35+
for (int i = 0; i < numberOfPlayers - 2; i++) {
36+
builder.point(RANDOM.nextBoolean());
37+
}
38+
return builder.build();
39+
}
40+
3041
public int getHeight() {
3142
return lines.size();
3243
}

src/main/java/nextstep/ladder/domain/Point.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package nextstep.ladder.domain;
22

3+
import java.util.Objects;
4+
35
public class Point {
46
private static final int MIN_INDEX = 0;
57

@@ -50,4 +52,17 @@ public boolean canMoveRight() {
5052
public boolean sameIndex(int index) {
5153
return this.index == index;
5254
}
55+
56+
@Override
57+
public boolean equals(Object o) {
58+
if (this == o) return true;
59+
if (o == null || getClass() != o.getClass()) return false;
60+
Point point = (Point) o;
61+
return index == point.index && left == point.left && right == point.right;
62+
}
63+
64+
@Override
65+
public int hashCode() {
66+
return Objects.hash(index, left, right);
67+
}
5368
}

src/test/java/nextstep/ladder/domain/LineBuilderTest.java

Lines changed: 0 additions & 16 deletions
This file was deleted.

src/test/java/nextstep/ladder/domain/LineTest.java

Lines changed: 9 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33
import org.junit.jupiter.api.DisplayName;
44
import org.junit.jupiter.api.Test;
55

6-
import java.util.List;
7-
86
import static org.assertj.core.api.Assertions.assertThat;
97
import static org.assertj.core.api.Assertions.assertThatThrownBy;
108

@@ -13,77 +11,27 @@ class LineTest {
1311
@DisplayName("포인트를 받아 라인을 생성한다")
1412
@Test
1513
void create() {
16-
Point leftmostPoint = Point.createLeftmost(true);
17-
List<Point> points = List.of(
18-
leftmostPoint,
19-
leftmostPoint.createRightmost()
20-
);
21-
Line line = new Line(points);
14+
Line line = new Line.LineBuilder()
15+
.point(true)
16+
.build();
2217

2318
assertThat(line.width()).isEqualTo(2);
2419
}
2520

26-
@DisplayName("포인트가 null이면 예외가 발생한다")
27-
@Test
28-
void nullPoints() {
29-
assertThatThrownBy(() -> new Line(null))
30-
.isInstanceOf(IllegalArgumentException.class);
31-
}
32-
3321
@DisplayName("포인트가 비어있으면 예외가 발생한다")
3422
@Test
3523
void emptyPoints() {
36-
assertThatThrownBy(() -> new Line(List.of()))
37-
.isInstanceOf(IllegalArgumentException.class);
38-
}
39-
40-
@DisplayName("시작 포인트가 왼쪽으로 이동 가능하면 예외가 발생한다")
41-
@Test
42-
void firstPointCannotMoveLeft() {
43-
List<Point> points = List.of(
44-
new Point(0, true, false),
45-
new Point(1, false, false)
46-
);
47-
48-
assertThatThrownBy(() -> new Line(points))
49-
.isInstanceOf(IllegalArgumentException.class);
50-
}
51-
52-
@DisplayName("마지막 포인트가 오른쪽으로 이동 가능하면 예외가 발생한다")
53-
@Test
54-
void lastPointCannotMoveRight() {
55-
List<Point> points = List.of(
56-
new Point(0, false, false),
57-
new Point(1, false, true)
58-
);
59-
60-
assertThatThrownBy(() -> new Line(points))
61-
.isInstanceOf(IllegalArgumentException.class);
62-
}
63-
64-
@DisplayName("이동 가능한 포인트간에 서로 이동 가능하지 않으면 예외가 발생한다")
65-
@Test
66-
void hasMovableConnectionToNext() {
67-
List<Point> points = List.of(
68-
new Point(0, false, true),
69-
new Point(1, false, false)
70-
);
71-
72-
assertThatThrownBy(() -> new Line(points))
73-
.isInstanceOf(IllegalArgumentException.class);
24+
assertThatThrownBy(() -> new Line.LineBuilder().build())
25+
.isInstanceOf(IllegalStateException.class);
7426
}
7527

7628
@DisplayName("인덱스를 받아 포인트를 반환한다")
7729
@Test
7830
void getPoint() {
79-
Point leftmostPoint = Point.createLeftmost(true);
80-
List<Point> points = List.of(
81-
leftmostPoint,
82-
leftmostPoint.createRightmost()
83-
);
84-
Line line = new Line(points);
31+
Line line = new Line.LineBuilder()
32+
.point(true)
33+
.build();
8534

86-
assertThat(line.getPoint(0)).isEqualTo(leftmostPoint);
35+
assertThat(line.getPoint(0)).isEqualTo(Point.createLeftmost(true));
8736
}
88-
8937
}

0 commit comments

Comments
 (0)