-
Notifications
You must be signed in to change notification settings - Fork 708
/
Copy pathBridgeLines.java
64 lines (50 loc) · 2.03 KB
/
BridgeLines.java
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
package ladder.domain;
import ladder.exception.BridgeLinesDifferenceHeightException;
import ladder.exception.ContinuousBridgeLinesException;
import ladder.exception.InvalidBridgeLinesSizeException;
import java.util.List;
import java.util.stream.IntStream;
public class BridgeLines {
private static final int MINIMUM_BRIDGE_LINES_SIZE = 2;
private final List<BridgeLine> bridgeLines;
public BridgeLines(final List<BridgeLine> bridgeLines) {
validateBridgeLinesOrThrow(bridgeLines);
this.bridgeLines = bridgeLines;
}
public int size() {
return this.bridgeLines.size();
}
public List<BridgeLine> getBridgeLines() {
return bridgeLines;
}
private void validateBridgeLinesOrThrow(final List<BridgeLine> bridgeLines) {
validateBridgeLinesSize(bridgeLines);
validateBridgeLinesHeightOrThrow(bridgeLines);
validateContinuousBridgeLinesOrThrow(bridgeLines);
}
private void validateBridgeLinesSize(final List<BridgeLine> bridgeLines) {
if (bridgeLines.size() < MINIMUM_BRIDGE_LINES_SIZE) {
throw new InvalidBridgeLinesSizeException();
}
}
private void validateBridgeLinesHeightOrThrow(final List<BridgeLine> bridgeLines) {
if (bridgeLines.stream()
.map(BridgeLine::getHeight)
.distinct()
.count() != 1) {
throw new BridgeLinesDifferenceHeightException();
}
}
private void validateContinuousBridgeLinesOrThrow(final List<BridgeLine> bridgeLines) {
IntStream.range(0, bridgeLines.size() - 1)
.filter(index -> {
final BridgeLine current = bridgeLines.get(index);
final BridgeLine next = bridgeLines.get(index + 1);
return current.hasSameHeightConnection(next);
})
.findFirst()
.ifPresent(ignore -> {
throw new ContinuousBridgeLinesException();
});
}
}