Skip to content

Commit 18f5eb8

Browse files
committed
interpreter pattern format tab fixed
1 parent 56811a2 commit 18f5eb8

File tree

11 files changed

+104
-106
lines changed

11 files changed

+104
-106
lines changed

interpreter/example/Main.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
public class Main {
22

3-
public static void main() {
4-
//user starts the game
5-
Expression rgbToHex = new RgbToHex();
6-
Expression hexToRgb = new HexToRgb();
3+
public static void main() {
4+
//user starts the game
5+
Expression rgbToHex = new RgbToHex();
6+
Expression hexToRgb = new HexToRgb();
77

8-
//user solves the tasks
9-
rgbToHex.interpret("rgb(50, 100, 200)"); //interpreted as #3264C8
10-
hexToRgb.interpret("#123ABC"); //interpreted as rgb(18,58,188)
11-
}
8+
//user solves the tasks
9+
rgbToHex.interpret("rgb(50, 100, 200)"); //interpreted as #3264C8
10+
hexToRgb.interpret("#123ABC"); //interpreted as rgb(18,58,188)
11+
}
1212
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
interface Expression {
22

3-
String interpret(String number);
3+
String interpret(String number);
44
}
Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
public class HexToInt implements Expression {
22

3-
@Override
4-
public String interpret(String number) {
5-
Pattern pattern = Pattern.compile("([0-9a-f]{2}", Pattern.CASE_INSENSITIVE);
6-
Matcher matcher = pattern.matcher(number);
7-
if (matcher.matches()) {
8-
return Integer.parseInt(number, 16);
9-
}
10-
else
11-
return "Invalid input. Should be [0-9a-f]{2}";
12-
}
3+
@Override
4+
public String interpret(String number) {
5+
Pattern pattern = Pattern.compile("([0-9a-f]{2}", Pattern.CASE_INSENSITIVE);
6+
Matcher matcher = pattern.matcher(number);
7+
if (matcher.matches()) {
8+
return Integer.parseInt(number, 16);
9+
}
10+
else
11+
return "Invalid input. Should be [0-9a-f]{2}";
12+
}
1313
}
Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
11
public class HexToRgb implements Expression {
22

3-
@Override
4-
public String interpret(String number) {
5-
Pattern pattern = Pattern.compile("#([0-9a-f]{6}", Pattern.CASE_INSENSITIVE);
6-
Matcher matcher = pattern.matcher(number);
7-
if (matcher.matches()) {
8-
Expression hexToInt = new HexToInt();
9-
String red = hexToInt.interpret(number.substring(1,3));
10-
String green = hexToInt.interpret(number.substring(3,5));
11-
String blue = hexToInt.interpret(number.substring(5,7));
12-
if(!red.contains("Invalid") && !green.contains("Invalid") && !blue.contains("Invalid"))
13-
return "#" + red + green + blue;
14-
else
15-
return "Invalid input. Should be #rrggbb";
16-
}
17-
else
18-
return "Invalid input. Should be #rrggbb";
19-
}
3+
@Override
4+
public String interpret(String number) {
5+
Pattern pattern = Pattern.compile("#([0-9a-f]{6}", Pattern.CASE_INSENSITIVE);
6+
Matcher matcher = pattern.matcher(number);
7+
if (matcher.matches()) {
8+
Expression hexToInt = new HexToInt();
9+
String red = hexToInt.interpret(number.substring(1,3));
10+
String green = hexToInt.interpret(number.substring(3,5));
11+
String blue = hexToInt.interpret(number.substring(5,7));
12+
if(!red.contains("Invalid") && !green.contains("Invalid") && !blue.contains("Invalid"))
13+
return "#" + red + green + blue;
14+
else
15+
return "Invalid input. Should be #rrggbb";
16+
}
17+
else
18+
return "Invalid input. Should be #rrggbb";
19+
}
2020
}
Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
public class IntToHex implements Expression {
22

3-
@Override
4-
public String interpret(String number) {
5-
Pattern pattern = Pattern.compile("([0-9]{1,3}");
6-
Matcher matcher = pattern.matcher(number);
7-
if (matcher.matches() && Integer.parseInt(number) <= 255) {
8-
return Integer.toHexString(number);
9-
else
10-
return "Invalid input. Should be a number between 0-255";
11-
}
3+
@Override
4+
public String interpret(String number) {
5+
Pattern pattern = Pattern.compile("([0-9]{1,3}");
6+
Matcher matcher = pattern.matcher(number);
7+
if (matcher.matches() && Integer.parseInt(number) <= 255) {
8+
return Integer.toHexString(number);
9+
else
10+
return "Invalid input. Should be a number between 0-255";
11+
}
1212
}
Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
11
public class RgbToHex implements Expression {
22

3-
@Override
4-
public String interpret(String number) {
5-
Pattern pattern = Pattern.compile("rgb *\\( *([0-9]+), *([0-9]+), *([0-9]+) *\\)");
6-
Matcher matcher = pattern.matcher(number);
7-
if (matcher.matches()) {
8-
Expression intToHex = new IntToHex();
9-
String red = intToHex.interpret(matcher.group(1));
10-
String green = intToHex.interpret(matcher.group(2));
11-
String blue = intToHex.interpret(matcher.group(3));
12-
if(!red.contains("Invalid") && !green.contains("Invalid") && !blue.contains("Invalid"))
13-
return "rgb(" + red + green + blue + ")";
14-
else
15-
return "Invalid input. Should be rgb(int, int, int)";
16-
}
17-
else
18-
return "Invalid input. Should be rgb(int, int, int)";
19-
}
3+
@Override
4+
public String interpret(String number) {
5+
Pattern pattern = Pattern.compile("rgb *\\( *([0-9]+), *([0-9]+), *([0-9]+) *\\)");
6+
Matcher matcher = pattern.matcher(number);
7+
if (matcher.matches()) {
8+
Expression intToHex = new IntToHex();
9+
String red = intToHex.interpret(matcher.group(1));
10+
String green = intToHex.interpret(matcher.group(2));
11+
String blue = intToHex.interpret(matcher.group(3));
12+
if(!red.contains("Invalid") && !green.contains("Invalid") && !blue.contains("Invalid"))
13+
return "rgb(" + red + green + blue + ")";
14+
else
15+
return "Invalid input. Should be rgb(int, int, int)";
16+
}
17+
else
18+
return "Invalid input. Should be rgb(int, int, int)";
19+
}
2020
}

interpreter/pattern/Main.java

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,21 @@
11
public class Main {
22

33
public static void main() {
4-
//create concrete rule based on brothers expression rule
5-
Expression areBrothers = getBrothersExpression("Jack", "Johnnie");
4+
//create concrete rule based on brothers expression rule
5+
Expression areBrothers = getBrothersExpression("Jack", "Johnnie");
66

7-
//check if input implements the rule
8-
areBrothers.interpret("Jack is Johnnie's brother"); //true
9-
areBrothers.interpret("Jack has one brother. His name is John"); //false
10-
}
7+
//check if input implements the rule
8+
areBrothers.interpret("Jack is Johnnie's brother"); //true
9+
areBrothers.interpret("Jack has one brother. His name is John"); //false
10+
}
1111

12-
//brothers expression rule
13-
public static Expression getBrothersExpression(String name1, String name2) {
14-
Expression person1 = new TerminalExpression(new Context(name1));
15-
Expression person2 = new TerminalExpression(new Context(name2));
16-
Expression brother = new TerminalExpression(new Context("brother"));
12+
//brothers expression rule
13+
public static Expression getBrothersExpression(String name1, String name2) {
14+
Expression person1 = new TerminalExpression(new Context(name1));
15+
Expression person2 = new TerminalExpression(new Context(name2));
16+
Expression brother = new TerminalExpression(new Context("brother"));
1717

18-
Expression names = new NonTerminalExpression(person1, person2);
19-
return new NonTerminalExpression(names, brothers);
20-
}
18+
Expression names = new NonTerminalExpression(person1, person2);
19+
return new NonTerminalExpression(names, brothers);
20+
}
2121
}
Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,14 @@
11
public class Context {
22

3-
//some fields
4-
private String text;
3+
//some fields
4+
private String text;
55

6-
//constructor and methods
7-
public Context(String text) {
8-
this.text = text;
9-
}
6+
//constructor and methods
7+
public Context(String text) {
8+
this.text = text;
9+
}
1010

11-
public String getText() {
12-
return text;
13-
}
14-
15-
//other methods
11+
public String getText() {
12+
return text;
13+
}
1614
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
interface Expression {
22

3-
boolean interpret(Context context);
3+
boolean interpret(Context context);
44
}
Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
public class NonTerminalExpression implements Expression {
22

3-
private TerminalExpression expression1;
4-
private TerminalExpression expression2;
3+
private TerminalExpression expression1;
4+
private TerminalExpression expression2;
55

6-
public NonTerminalExpression(TerminalExpression expr1, TerminalExpression expr2) {
7-
this.expression1 = expr1;
8-
this.expression2 = expr2;
9-
}
6+
public NonTerminalExpression(TerminalExpression expr1, TerminalExpression expr2) {
7+
this.expression1 = expr1;
8+
this.expression2 = expr2;
9+
}
1010

11-
@Override
12-
public boolean interpret(Context context) {
13-
//do something specific for NonTerminalExpression e.g. AndExpression
14-
return expression1.interpret(context) && expression2.interpret(context);
15-
}
11+
@Override
12+
public boolean interpret(Context context) {
13+
//do something specific for NonTerminalExpression e.g. AndExpression
14+
return expression1.interpret(context) && expression2.interpret(context);
15+
}
1616
}

0 commit comments

Comments
 (0)