Skip to content

Commit 4c06dcc

Browse files
committed
Initiated prefixes, source, and suffixes at the start of the program. Added updatePattern method for code readability. Added constructor so updatePattern gets called at instantiation.
1 parent faed06d commit 4c06dcc

File tree

1 file changed

+19
-15
lines changed

1 file changed

+19
-15
lines changed

src/main/java/VerbalExpression.java

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,33 @@
11
import java.util.regex.Pattern;
22

33
class VerbalExpression {
4-
private String prefixes, source, suffixes, pattern = "";
4+
private String prefixes = "", source = "", suffixes = "", pattern = "";
55
private int modifiers = Pattern.MULTILINE;
66

7+
public VerbalExpression () {
8+
this.updatePattern();
9+
}
10+
711
private String sanitize(String value) {
812
if(value == null)
913
return value;
1014
return Pattern.quote(value);
1115
}
1216

1317
public VerbalExpression add(String value) {
14-
this.source = this.source != null ? this.source + value : value;
15-
if (this.source != null) {
16-
Pattern p = Pattern.compile(this.prefixes + this.source + this.suffixes, this.modifiers);
17-
this.pattern = p.pattern();
18-
}
18+
this.source += value;
19+
return this.updatePattern();
20+
}
21+
22+
public VerbalExpression updatePattern() {
23+
Pattern p = Pattern.compile(this.prefixes + this.source + this.suffixes, this.modifiers);
24+
this.pattern = p.pattern();
1925
return this;
2026
}
2127

2228
public VerbalExpression startOfLine(boolean enable) {
2329
this.prefixes = enable ? "^" : "";
24-
this.add("");
30+
this.updatePattern();
2531
return this;
2632
}
2733

@@ -31,7 +37,7 @@ public VerbalExpression startOfLine() {
3137

3238
public VerbalExpression endOfLine(boolean enable) {
3339
this.suffixes = enable ? "$" : "";
34-
this.add("");
40+
this.updatePattern();
3541
return this;
3642
}
3743

@@ -79,7 +85,7 @@ public VerbalExpression somethingBut(String value) {
7985
}
8086

8187
public VerbalExpression replace(String source, String value) {
82-
this.add("");
88+
this.updatePattern();
8389
this.source.replaceAll(pattern,value);
8490
return this;
8591
}
@@ -159,7 +165,7 @@ public VerbalExpression addModifier(char modifier) {
159165
break;
160166
}
161167

162-
this.add("");
168+
this.updatePattern();
163169
return this;
164170
}
165171

@@ -190,14 +196,14 @@ public VerbalExpression removeModifier(char modifier) {
190196
break;
191197
}
192198

193-
this.add("");
199+
this.updatePattern();
194200
return this;
195201
}
196202

197203
public VerbalExpression withAnyCase(boolean enable) {
198204
if (enable) this.addModifier( 'i' );
199205
else this.removeModifier( 'i' );
200-
this.add("");
206+
this.updatePattern();
201207
return this;
202208
}
203209

@@ -208,7 +214,7 @@ public VerbalExpression withAnyCase() {
208214
public VerbalExpression searchOneLine(boolean enable) {
209215
if (enable) this.removeModifier( 'm' );
210216
else this.addModifier( 'm' );
211-
this.add("");
217+
this.updatePattern();
212218
return this;
213219
}
214220

@@ -235,12 +241,10 @@ public VerbalExpression or(String value) {
235241
}
236242

237243
public boolean test(String toTest) {
238-
this.add("");
239244
return Pattern.matches(this.pattern, toTest);
240245
}
241246

242247
public String toString() {
243-
this.add("");
244248
return this.pattern.toString();
245249
}
246250
}

0 commit comments

Comments
 (0)