3
3
import static java .lang .String .valueOf ;
4
4
5
5
import java .util .ArrayList ;
6
+ import java .util .HashMap ;
6
7
import java .util .List ;
8
+ import java .util .Map ;
7
9
import java .util .regex .Matcher ;
8
10
import java .util .regex .Pattern ;
9
11
@@ -18,6 +20,16 @@ public static class Builder {
18
20
private StringBuilder suffixes = new StringBuilder ();
19
21
private int modifiers = Pattern .MULTILINE ;
20
22
23
+ private static final Map <Character , Integer > SYMBOL_MAP = new HashMap <Character , Integer >() {{
24
+ put ('d' , Pattern .UNIX_LINES );
25
+ put ('i' , Pattern .CASE_INSENSITIVE );
26
+ put ('x' , Pattern .COMMENTS );
27
+ put ('m' , Pattern .MULTILINE );
28
+ put ('s' , Pattern .DOTALL );
29
+ put ('u' , Pattern .UNICODE_CASE );
30
+ put ('U' , Pattern .UNICODE_CHARACTER_CLASS );
31
+ }};
32
+
21
33
/**
22
34
* Package private. Use {@link #regex()} to build a new one
23
35
*
@@ -172,7 +184,7 @@ public Builder find(final String value) {
172
184
public Builder maybe (final String pValue ) {
173
185
return this .then (pValue ).add ("?" );
174
186
}
175
-
187
+
176
188
/**
177
189
* Add a regex to the expression that might appear once (or not)
178
190
* Example:
@@ -367,60 +379,16 @@ public Builder range(final String... pArgs) {
367
379
}
368
380
369
381
public Builder addModifier (final char pModifier ) {
370
- switch (pModifier ) {
371
- case 'd' :
372
- modifiers |= Pattern .UNIX_LINES ;
373
- break ;
374
- case 'i' :
375
- modifiers |= Pattern .CASE_INSENSITIVE ;
376
- break ;
377
- case 'x' :
378
- modifiers |= Pattern .COMMENTS ;
379
- break ;
380
- case 'm' :
381
- modifiers |= Pattern .MULTILINE ;
382
- break ;
383
- case 's' :
384
- modifiers |= Pattern .DOTALL ;
385
- break ;
386
- case 'u' :
387
- modifiers |= Pattern .UNICODE_CASE ;
388
- break ;
389
- case 'U' :
390
- modifiers |= Pattern .UNICODE_CHARACTER_CLASS ;
391
- break ;
392
- default :
393
- break ;
382
+ if (SYMBOL_MAP .containsKey (pModifier )) {
383
+ modifiers |= SYMBOL_MAP .get (pModifier );
394
384
}
395
385
396
386
return this ;
397
387
}
398
388
399
389
public Builder removeModifier (final char pModifier ) {
400
- switch (pModifier ) {
401
- case 'd' :
402
- modifiers &= ~Pattern .UNIX_LINES ;
403
- break ;
404
- case 'i' :
405
- modifiers &= ~Pattern .CASE_INSENSITIVE ;
406
- break ;
407
- case 'x' :
408
- modifiers &= ~Pattern .COMMENTS ;
409
- break ;
410
- case 'm' :
411
- modifiers &= ~Pattern .MULTILINE ;
412
- break ;
413
- case 's' :
414
- modifiers &= ~Pattern .DOTALL ;
415
- break ;
416
- case 'u' :
417
- modifiers &= ~Pattern .UNICODE_CASE ;
418
- break ;
419
- case 'U' :
420
- modifiers &= ~Pattern .UNICODE_CHARACTER_CLASS ;
421
- break ;
422
- default :
423
- break ;
390
+ if (SYMBOL_MAP .containsKey (pModifier )) {
391
+ modifiers &= ~SYMBOL_MAP .get (pModifier );
424
392
}
425
393
426
394
return this ;
@@ -578,7 +546,7 @@ public Builder or(final String pValue) {
578
546
}
579
547
return this ;
580
548
}
581
-
549
+
582
550
/**
583
551
* Adds an alternative expression to be matched
584
552
* based on an array of values
@@ -750,7 +718,7 @@ public String getText(final String toTest, final int group) {
750
718
751
719
/**
752
720
* Extract exact group from string and add it to list
753
- *
721
+ *
754
722
* Example:
755
723
* String text = "SampleHelloWorldString";
756
724
* VerbalExpression regex = regex().capt().oneOf("Hello", "World").endCapt().maybe("String").build();
0 commit comments