-
Notifications
You must be signed in to change notification settings - Fork 738
Expand file tree
/
Copy pathLadderLine.java
More file actions
46 lines (37 loc) · 1.14 KB
/
LadderLine.java
File metadata and controls
46 lines (37 loc) · 1.14 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
package nextstep.ladder.domain;
import java.util.Iterator;
import java.util.List;
public class LadderLine {
private final List<Boolean> lines;
public LadderLine(List<Boolean> lines) {
if (lines == null || lines.isEmpty()) {
throw new IllegalArgumentException("lines cannot be null or empty");
}
validateLines(lines);
this.lines = lines;
}
public int size() {
return lines.size();
}
private void validateLines(List<Boolean> lines) {
Iterator<Boolean> iterator = lines.iterator();
boolean prev = iterator.next();
while (iterator.hasNext()) {
boolean cur = iterator.next();
if (prev && cur) {
throw new IllegalArgumentException("Ladder lines cannot be connected continuously.");
}
prev = cur;
}
}
@Override
public String toString() {
final StringBuilder sb = new StringBuilder();
for (Boolean line : lines) {
sb.append("|");
sb.append(line ? "--------": " ");
}
sb.append("|");
return sb.toString();
}
}