File tree Expand file tree Collapse file tree 3 files changed +50
-3
lines changed
main/java/pl/mperor/lab/java/design/pattern/structural/decorator/lambda/expression
test/java/pl/mperor/lab/java/design/pattern/structural/decorator/lambda/expression Expand file tree Collapse file tree 3 files changed +50
-3
lines changed Original file line number Diff line number Diff line change 1- package pl .mperor .lab .java .design .pattern .structural .decorator ;
1+ package pl .mperor .lab .java .design .pattern .structural .decorator . lambda . expression ;
22
33import java .util .Arrays ;
44import java .util .function .UnaryOperator ;
Original file line number Diff line number Diff line change 1+ package pl .mperor .lab .java .design .pattern .structural .decorator .lambda .expression ;
2+
3+ import java .util .function .Predicate ;
4+
5+ public class TextValidator {
6+
7+ private TextValidator () {
8+ }
9+
10+ public static Predicate <String > notEmpty () {
11+ return Predicate .not (String ::isEmpty );
12+ }
13+
14+ public static Predicate <String > lengthBetween (int min , int max ) {
15+ return minLength (min ).and (maxLength (max ));
16+ }
17+
18+ public static Predicate <String > minLength (int min ) {
19+ return text -> text .length () >= min ;
20+ }
21+
22+ public static Predicate <String > maxLength (int max ) {
23+ return text -> text .length () >= max ;
24+ }
25+
26+ public static Predicate <String > numeric () {
27+ return text -> text .matches ("\\ d+" );
28+ }
29+
30+ public static Predicate <String > nan () {
31+ return numeric ().negate ();
32+ }
33+
34+ }
Original file line number Diff line number Diff line change 1- package pl .mperor .lab .java .design .pattern .structural .decorator ;
1+ package pl .mperor .lab .java .design .pattern .structural .decorator . lambda . expression ;
22
33import org .junit .jupiter .api .Assertions ;
44import org .junit .jupiter .api .Test ;
55
6- public class TextProcessorTest {
6+ public class TextProcessorAndValidatorTest {
77
88 @ Test
99 public void shouldAllowToCreateTextProcessorAsChainOfFunctions () {
@@ -23,4 +23,17 @@ public void shouldAllowToCreateTextProcessorAsChainOfFunctions() {
2323 );
2424 }
2525
26+ @ Test
27+ public void shouldAllowToCreateTextValidator () {
28+ Assertions .assertTrue (
29+ TextValidator .notEmpty ()
30+ .and (TextValidator .nan ())
31+ .and (TextValidator .lengthBetween (1 , 3 ))
32+ .test ("abc" )
33+ );
34+
35+ var blankOrNumeric = TextValidator .numeric ().or (String ::isBlank );
36+ Assertions .assertTrue (blankOrNumeric .test (" " ) && blankOrNumeric .test ("123" ));
37+ }
38+
2639}
You can’t perform that action at this time.
0 commit comments