1 package io.github.robertograham.rleparser.helper;
2
3 import io.github.robertograham.rleparser.domain.Coordinate;
4 import io.github.robertograham.rleparser.domain.StatusRun;
5 import io.github.robertograham.rleparser.domain.enumeration.Status;
6
7 import java.util.Set;
8 import java.util.regex.Matcher;
9 import java.util.regex.Pattern;
10 import java.util.stream.Collectors;
11 import java.util.stream.IntStream;
12
13 public class StatusRunHelper {
14
15 private static final Pattern STATUS_RUN_PATTERN = Pattern.compile("(\\d*)([a-z$])");
16
17 public static StatusRun readStatusRun(String encodedStatusRun, Coordinate origin) {
18 Matcher matcher = STATUS_RUN_PATTERN.matcher(encodedStatusRun);
19
20 if (matcher.find())
21 return new StatusRun(
22 matcher.group(1).isEmpty() ?
23 1
24 : Integer.parseInt(matcher.group(1)),
25 Status.fromCode(matcher.group(2)),
26 origin
27 );
28
29 throw new IllegalArgumentException("Encoded run length status did not match (\\d*)([a-z$])");
30 }
31
32 public static Set<Coordinate> readCoordinates(StatusRun statusRun) {
33 return IntStream.range(statusRun.getOrigin().getX(), statusRun.getOrigin().getX() + statusRun.getLength())
34 .mapToObj(statusRun.getOrigin()::withX)
35 .collect(Collectors.toSet());
36 }
37 }