20
20
import java .util .Collection ;
21
21
import java .util .Collections ;
22
22
import java .util .List ;
23
+ import java .util .Locale ;
23
24
import java .util .Map ;
24
25
import java .util .function .Function ;
26
+ import java .util .function .IntFunction ;
25
27
26
28
import org .springframework .util .Assert ;
27
29
import org .springframework .util .StringUtils ;
@@ -63,10 +65,14 @@ public final class ConfigurationPropertyName implements Comparable<Configuration
63
65
64
66
private final CharSequence [] uniformElements ;
65
67
66
- private String string ;
67
-
68
68
private int hashCode ;
69
69
70
+ private String [] string = new String [ToStringFormat .values ().length ];
71
+
72
+ private Boolean hasDashedElement ;
73
+
74
+ private ConfigurationPropertyName systemEnvironmentLegacyName ;
75
+
70
76
private ConfigurationPropertyName (Elements elements ) {
71
77
this .elements = elements ;
72
78
this .uniformElements = new CharSequence [elements .getSize ()];
@@ -525,15 +531,41 @@ public int hashCode() {
525
531
return hashCode ;
526
532
}
527
533
534
+ ConfigurationPropertyName asSystemEnvironmentLegacyName () {
535
+ ConfigurationPropertyName name = this .systemEnvironmentLegacyName ;
536
+ if (name == null ) {
537
+ name = ConfigurationPropertyName
538
+ .ofIfValid (buildSimpleToString ('.' , (i ) -> getElement (i , Form .DASHED ).replace ('-' , '.' )));
539
+ this .systemEnvironmentLegacyName = (name != null ) ? name : EMPTY ;
540
+ }
541
+ return (name != EMPTY ) ? name : null ;
542
+ }
543
+
528
544
@ Override
529
545
public String toString () {
530
- if (this .string == null ) {
531
- this .string = buildToString ();
546
+ return toString (ToStringFormat .DEFAULT );
547
+ }
548
+
549
+ String toString (ToStringFormat format ) {
550
+ String string = this .string [format .ordinal ()];
551
+ if (string == null ) {
552
+ string = buildToString (format );
553
+ this .string [format .ordinal ()] = string ;
532
554
}
533
- return this .string ;
555
+ return string ;
556
+ }
557
+
558
+ private String buildToString (ToStringFormat format ) {
559
+ return switch (format ) {
560
+ case DEFAULT -> buildDefaultToString ();
561
+ case SYSTEM_ENVIRONMENT ->
562
+ buildSimpleToString ('_' , (i ) -> getElement (i , Form .UNIFORM ).toUpperCase (Locale .ENGLISH ));
563
+ case LEGACY_SYSTEM_ENVIRONMENT -> buildSimpleToString ('_' ,
564
+ (i ) -> getElement (i , Form .ORIGINAL ).replace ('-' , '_' ).toUpperCase (Locale .ENGLISH ));
565
+ };
534
566
}
535
567
536
- private String buildToString () {
568
+ private String buildDefaultToString () {
537
569
if (this .elements .canShortcutWithSource (ElementType .UNIFORM , ElementType .DASHED )) {
538
570
return this .elements .getSource ().toString ();
539
571
}
@@ -556,6 +588,32 @@ private String buildToString() {
556
588
return result .toString ();
557
589
}
558
590
591
+ private String buildSimpleToString (char joinChar , IntFunction <String > elementConverter ) {
592
+ StringBuilder result = new StringBuilder ();
593
+ for (int i = 0 ; i < getNumberOfElements (); i ++) {
594
+ if (!result .isEmpty ()) {
595
+ result .append (joinChar );
596
+ }
597
+ result .append (elementConverter .apply (i ));
598
+ }
599
+ return result .toString ();
600
+ }
601
+
602
+ boolean hasDashedElement () {
603
+ Boolean hasDashedElement = this .hasDashedElement ;
604
+ if (hasDashedElement != null ) {
605
+ return hasDashedElement ;
606
+ }
607
+ for (int i = 0 ; i < getNumberOfElements (); i ++) {
608
+ if (getElement (i , Form .DASHED ).indexOf ('-' ) != -1 ) {
609
+ this .hasDashedElement = true ;
610
+ return true ;
611
+ }
612
+ }
613
+ this .hasDashedElement = false ;
614
+ return false ;
615
+ }
616
+
559
617
/**
560
618
* Returns if the given name is valid. If this method returns {@code true} then the
561
619
* name may be used with {@link #of(CharSequence)} without throwing an exception.
@@ -1132,4 +1190,13 @@ private interface ElementCharPredicate {
1132
1190
1133
1191
}
1134
1192
1193
+ /**
1194
+ * Formats for {@code toString}.
1195
+ */
1196
+ enum ToStringFormat {
1197
+
1198
+ DEFAULT , SYSTEM_ENVIRONMENT , LEGACY_SYSTEM_ENVIRONMENT
1199
+
1200
+ }
1201
+
1135
1202
}
0 commit comments