forked from woowacourse-precourse/java-calculator-7
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCustomDelimiterProcessor.java
More file actions
53 lines (38 loc) · 1.39 KB
/
CustomDelimiterProcessor.java
File metadata and controls
53 lines (38 loc) · 1.39 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
package calculator.model.delimiter;
import static calculator.config.CustomDelimiterPattern.*;
import java.util.Optional;
import calculator.validation.InputValidator;
public class CustomDelimiterProcessor {
public String removeCustomDelimiterPattern(String input) {
if (hasCustomDelimiterPattern(input)) {
int endIndex = findEndIndex(input);
return input.substring(endIndex + END.getPatternLength());
}
return input;
}
public Optional<String> extractCustomDelimiter(String input) {
if (!hasCustomDelimiterPattern(input)) {
return Optional.empty();
}
int startIndex = findStartIndex(input);
int endIndex = findEndIndex(input);
return Optional.of(extractCustomDelimiter(input, startIndex, endIndex));
}
private boolean hasCustomDelimiterPattern(String input) {
return input.contains(START.getPattern()) && input.contains(END.getPattern());
}
private int findStartIndex(String input) {
int startIndex = input.indexOf(START.getPattern());
InputValidator.validateStartPattern(startIndex);
return startIndex + START.getPatternLength();
}
private int findEndIndex(String input) {
int endIndex = input.indexOf(END.getPattern());
InputValidator.validateEndPattern(endIndex);
return endIndex;
}
private String extractCustomDelimiter(String input, int start, int end) {
InputValidator.validatePatternPosition(start, end);
return input.substring(start, end);
}
}