-
Notifications
You must be signed in to change notification settings - Fork 151
Expand file tree
/
Copy pathExpression.java
More file actions
47 lines (36 loc) · 1.16 KB
/
Expression.java
File metadata and controls
47 lines (36 loc) · 1.16 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
package co.programmers.domain;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import co.programmers.exception.ExceptionMessage;
public class Expression {
private static final Pattern EXPRESSION_FORMAT = Pattern.compile("(\\d+\\s[\\+\\-\\*\\/]\\s)+\\d+");
private static final String DELIMITER = " ";
private String expression;
public Expression(String expression) {
this.expression = expression;
if (expression == null || expression.isEmpty()) {
throw new IllegalArgumentException(ExceptionMessage.EMPTY_INPUT);
}
if (!validate()) {
throw new ArithmeticException(ExceptionMessage.INVALID_EXPRESSION);
}
}
private boolean validate() {
Matcher matcher = EXPRESSION_FORMAT.matcher(expression);
return matcher.matches();
}
public List<String> split() {
return new ArrayList<>(List.of(expression.split(DELIMITER)));
}
public List<String> split(String delimiter) {
return new ArrayList<>(List.of(expression.split(delimiter)));
}
public void eliminateWhiteSpace() {
expression = expression.replaceAll("\\s", "");
}
public String getExpression() {
return expression;
}
}