69
69
*/
70
70
public class PropertyUtils {
71
71
72
- static final String NOT_BLANK_REGEX = "^\\ s*(\\ S+\\ s*)+$" ;
73
-
74
72
private static final Map <ResolvableType , ResolvableType > DOMAIN_TYPE_CACHE = new ConcurrentReferenceHashMap <>();
75
73
private static final Map <ResolvableType , InputPayloadMetadata > METADATA_CACHE = new ConcurrentReferenceHashMap <>();
76
74
private static final Set <String > FIELDS_TO_IGNORE = new HashSet <>(Arrays .asList ("class" , "links" ));
@@ -80,6 +78,8 @@ public class PropertyUtils {
80
78
Arrays .asList (EntityModel .class , CollectionModel .class , HttpEntity .class ));
81
79
private static final ResolvableType OBJECT_TYPE = ResolvableType .forClass (Object .class );
82
80
81
+ static final String NOT_BLANK_REGEX = "^(?=\\ s*\\ S).*$" ;
82
+
83
83
static {
84
84
if (ClassUtils .isPresent ("org.reactivestreams.Publisher" , PropertyUtils .class .getClassLoader ())) {
85
85
TYPES_TO_UNWRAP .addAll (ReactiveWrappers .getTypesToUnwrap ());
@@ -600,7 +600,7 @@ private Jsr303AwarePropertyMetadata(AnnotatedProperty property) {
600
600
601
601
/*
602
602
* (non-Javadoc)
603
- * @see org.springframework.hateoas.mediatype.PropertyUtils.PropertyMetadata #isRequired()
603
+ * @see org.springframework.hateoas.mediatype.PropertyUtils.DefaultPropertyMetadata #isRequired()
604
604
*/
605
605
@ Override
606
606
public boolean isRequired () {
@@ -612,18 +612,13 @@ public boolean isRequired() {
612
612
613
613
/*
614
614
* (non-Javadoc)
615
- * @see org.springframework.hateoas.mediatype.PropertyUtils.PropertyMetadata#getRegex ()
615
+ * @see org.springframework.hateoas.mediatype.PropertyUtils.DefaultPropertyMetadata#getPattern ()
616
616
*/
617
617
@ Override
618
618
public Optional <String > getPattern () {
619
619
620
- Optional <String > attribute = getAnnotationAttribute (Pattern .class , "regexp" , String .class );
621
-
622
- if (!attribute .isPresent () && property .getAnnotation (NotBlank .class ).isPresent ()) {
623
- attribute = Optional .of (NOT_BLANK_REGEX );
624
- }
625
-
626
- return attribute ;
620
+ return getAnnotationAttribute (Pattern .class , "regexp" , String .class ) //
621
+ .or (this ::getDefaultPatternForNonBlank );
627
622
}
628
623
629
624
/*
@@ -634,28 +629,11 @@ public Optional<String> getPattern() {
634
629
@ Override
635
630
public Number getMin () {
636
631
637
- if (RANGE_ANNOTATION != null ) {
638
-
639
- Optional <Long > attribute = getAnnotationAttribute (RANGE_ANNOTATION , "min" , Long .class );
640
-
641
- if (attribute .isPresent ()) {
642
- return attribute .get ();
643
- }
644
- }
645
-
646
- Optional <Long > minLong = getAnnotationAttribute (Min .class , "value" , Long .class );
647
-
648
- if (minLong .isPresent ()) {
649
- return minLong .get ();
650
- }
651
-
652
- Optional <String > minDecimal = getAnnotationAttribute (DecimalMin .class , "value" , String .class );
653
-
654
- if (minDecimal .isPresent ()) {
655
- return new BigDecimal (minDecimal .get ());
656
- }
657
-
658
- return null ;
632
+ return Optional .ofNullable (RANGE_ANNOTATION ) //
633
+ .flatMap (it -> getAnnotationAttribute (it , "min" , Number .class )) //
634
+ .or (() -> getAnnotationAttribute (Min .class , "value" , Number .class )) //
635
+ .or (() -> parsePropertyAnnotationValue (DecimalMin .class )) //
636
+ .orElse (null );
659
637
}
660
638
661
639
/*
@@ -666,28 +644,11 @@ public Number getMin() {
666
644
@ Override
667
645
public Number getMax () {
668
646
669
- if (RANGE_ANNOTATION != null ) {
670
-
671
- Optional <Long > attribute = getAnnotationAttribute (RANGE_ANNOTATION , "max" , Long .class );
672
-
673
- if (attribute .isPresent ()) {
674
- return attribute .get ();
675
- }
676
- }
677
-
678
- Optional <Long > maxLong = getAnnotationAttribute (Max .class , "value" , Long .class );
679
-
680
- if (maxLong .isPresent ()) {
681
- return maxLong .get ();
682
- }
683
-
684
- Optional <String > maxDecimal = getAnnotationAttribute (DecimalMax .class , "value" , String .class );
685
-
686
- if (maxDecimal .isPresent ()) {
687
- return new BigDecimal (maxDecimal .get ());
688
- }
689
-
690
- return null ;
647
+ return Optional .ofNullable (RANGE_ANNOTATION ) //
648
+ .flatMap (it -> getAnnotationAttribute (it , "max" , Number .class )) //
649
+ .or (() -> getAnnotationAttribute (Max .class , "value" , Number .class )) //
650
+ .or (() -> parsePropertyAnnotationValue (DecimalMax .class )) //
651
+ .orElse (null );
691
652
}
692
653
693
654
/*
@@ -737,6 +698,19 @@ public String getInputType() {
737
698
return cacheAndReturn (inputType != null ? inputType : super .getInputType ());
738
699
}
739
700
701
+ private Optional <String > getDefaultPatternForNonBlank () {
702
+
703
+ return Optional .of (property .getAnnotation (NotBlank .class ))
704
+ .filter (MergedAnnotation ::isPresent )
705
+ .map (__ -> NOT_BLANK_REGEX );
706
+ }
707
+
708
+ private Optional <Number > parsePropertyAnnotationValue (Class <? extends Annotation > type ) {
709
+
710
+ return getAnnotationAttribute (type , "value" , String .class )
711
+ .map (BigDecimal ::new );
712
+ }
713
+
740
714
private String cacheAndReturn (String value ) {
741
715
742
716
this .inputType = Optional .ofNullable (value );
0 commit comments