5
5
package org .hibernate .internal .util .config ;
6
6
7
7
import java .util .HashMap ;
8
- import java .util .Iterator ;
9
8
import java .util .Locale ;
10
9
import java .util .Map ;
11
10
import java .util .Properties ;
14
13
15
14
import org .checkerframework .checker .nullness .qual .NonNull ;
16
15
import org .hibernate .Incubating ;
17
- import org .hibernate .boot .registry .StandardServiceRegistry ;
18
- import org .hibernate .cfg .AvailableSettings ;
16
+ import org .hibernate .cfg .MappingSettings ;
19
17
import org .hibernate .dialect .Dialect ;
20
18
import org .hibernate .engine .config .spi .ConfigurationService ;
21
19
import org .hibernate .engine .jdbc .spi .JdbcServices ;
@@ -186,10 +184,10 @@ public static int getInt(String name, Map<?,?> values, int defaultValue) {
186
184
if ( value == null ) {
187
185
return defaultValue ;
188
186
}
189
- if (value instanceof Integer integer ) {
187
+ else if (value instanceof Integer integer ) {
190
188
return integer ;
191
189
}
192
- if (value instanceof String string ) {
190
+ else if (value instanceof String string ) {
193
191
return Integer .parseInt (string );
194
192
}
195
193
throw new ConfigurationException (
@@ -344,6 +342,10 @@ public static String extractPropertyValue(String propertyName, Map<?,?> properti
344
342
return value ;
345
343
}
346
344
345
+ /**
346
+ * @deprecated No longer used
347
+ */
348
+ @ Deprecated (since = "7.2" , forRemoval = true )
347
349
public static String extractValue (
348
350
String name ,
349
351
Map <?,?> values ,
@@ -374,10 +376,10 @@ public static String extractValue(
374
376
@ SuppressWarnings ("rawtypes" )
375
377
@ Deprecated (since = "7" , forRemoval = true )
376
378
public static Map toMap (String propertyName , String delim , Properties properties ) {
377
- Map <String ,String > map = new HashMap <>();
378
- String value = extractPropertyValue ( propertyName , properties );
379
+ final Map <String ,String > map = new HashMap <>();
380
+ final String value = extractPropertyValue ( propertyName , properties );
379
381
if ( value != null ) {
380
- StringTokenizer tokens = new StringTokenizer ( value , delim );
382
+ final var tokens = new StringTokenizer ( value , delim );
381
383
while ( tokens .hasMoreTokens () ) {
382
384
map .put ( tokens .nextToken (), tokens .hasMoreElements () ? tokens .nextToken () : "" );
383
385
}
@@ -403,10 +405,10 @@ public static Map toMap(String propertyName, String delim, Properties properties
403
405
@ SuppressWarnings ("rawtypes" )
404
406
@ Deprecated (since = "7" , forRemoval = true )
405
407
public static Map toMap (String propertyName , String delim , Map <?,?> properties ) {
406
- Map <String ,String > map = new HashMap <>();
407
- String value = extractPropertyValue ( propertyName , properties );
408
+ final Map <String ,String > map = new HashMap <>();
409
+ final String value = extractPropertyValue ( propertyName , properties );
408
410
if ( value != null ) {
409
- StringTokenizer tokens = new StringTokenizer ( value , delim );
411
+ final var tokens = new StringTokenizer ( value , delim );
410
412
while ( tokens .hasMoreTokens () ) {
411
413
map .put ( tokens .nextToken (), tokens .hasMoreElements () ? tokens .nextToken () : "" );
412
414
}
@@ -440,12 +442,9 @@ public static String[] toStringArray(String propertyName, String delim, Properti
440
442
*/
441
443
public static String [] toStringArray (String stringForm , String delim ) {
442
444
// todo : move to StringHelper?
443
- if ( stringForm != null ) {
444
- return StringHelper .split ( delim , stringForm );
445
- }
446
- else {
447
- return ArrayHelper .EMPTY_STRING_ARRAY ;
448
- }
445
+ return stringForm != null
446
+ ? StringHelper .split ( delim , stringForm )
447
+ : ArrayHelper .EMPTY_STRING_ARRAY ;
449
448
}
450
449
451
450
/**
@@ -454,13 +453,12 @@ public static String[] toStringArray(String stringForm, String delim) {
454
453
* @param configurationValues The configuration map.
455
454
*/
456
455
public static void resolvePlaceHolders (Map <?,Object > configurationValues ) {
457
- final Iterator <? extends Map . Entry <?, Object >> itr = configurationValues .entrySet ().iterator ();
456
+ final var itr = configurationValues .entrySet ().iterator ();
458
457
while ( itr .hasNext () ) {
459
- final Map .Entry <?,Object > entry = itr .next ();
460
- final Object value = entry .getValue ();
461
- if ( value instanceof String string ) {
458
+ final var entry = itr .next ();
459
+ if ( entry .getValue () instanceof String string ) {
462
460
final String resolved = resolvePlaceHolder ( string );
463
- if ( !value .equals ( resolved ) ) {
461
+ if ( !string .equals ( resolved ) ) {
464
462
if ( resolved == null ) {
465
463
itr .remove ();
466
464
}
@@ -482,8 +480,8 @@ public static String resolvePlaceHolder(String property) {
482
480
if ( !property .contains ( PLACEHOLDER_START ) ) {
483
481
return property ;
484
482
}
485
- StringBuilder buff = new StringBuilder ();
486
- char [] chars = property .toCharArray ();
483
+ final var result = new StringBuilder ();
484
+ final char [] chars = property .toCharArray ();
487
485
for ( int pos = 0 ; pos < chars .length ; pos ++ ) {
488
486
if ( chars [pos ] == '$' ) {
489
487
// peek ahead
@@ -500,18 +498,17 @@ public static String resolvePlaceHolder(String property) {
500
498
}
501
499
}
502
500
final String systemProperty = extractFromSystem ( systemPropertyName );
503
- buff .append ( systemProperty == null ? "" : systemProperty );
501
+ result .append ( systemProperty == null ? "" : systemProperty );
504
502
pos = x + 1 ;
505
503
// make sure spinning forward did not put us past the end of the buffer...
506
504
if ( pos >= chars .length ) {
507
505
break ;
508
506
}
509
507
}
510
508
}
511
- buff .append ( chars [pos ] );
509
+ result .append ( chars [pos ] );
512
510
}
513
- final String result = buff .toString ();
514
- return result .isEmpty () ? null : result ;
511
+ return result .isEmpty () ? null : result .toString ();
515
512
}
516
513
517
514
private static String extractFromSystem (String systemPropertyName ) {
@@ -523,96 +520,65 @@ private static String extractFromSystem(String systemPropertyName) {
523
520
}
524
521
}
525
522
526
- @ Incubating
527
- public static synchronized int getPreferredSqlTypeCodeForBoolean (StandardServiceRegistry serviceRegistry ) {
528
- final Integer typeCode = serviceRegistry .requireService ( ConfigurationService .class ).getSetting (
529
- AvailableSettings .PREFERRED_BOOLEAN_JDBC_TYPE ,
530
- TypeCodeConverter .INSTANCE
531
- );
523
+ private static Integer getConfiguredTypeCode (ServiceRegistry serviceRegistry , String setting ) {
524
+ final Integer typeCode =
525
+ serviceRegistry .requireService ( ConfigurationService .class )
526
+ .getSetting ( setting , TypeCodeConverter .INSTANCE );
532
527
if ( typeCode != null ) {
533
- INCUBATION_LOGGER .incubatingSetting ( AvailableSettings .PREFERRED_BOOLEAN_JDBC_TYPE );
534
- return typeCode ;
528
+ INCUBATION_LOGGER .incubatingSetting ( setting );
535
529
}
530
+ return typeCode ;
531
+ }
536
532
537
- // default to the Dialect answer
538
- return serviceRegistry .requireService ( JdbcServices .class )
539
- .getJdbcEnvironment ()
540
- .getDialect ()
541
- .getPreferredSqlTypeCodeForBoolean ();
533
+ @ Incubating
534
+ public static synchronized int getPreferredSqlTypeCodeForBoolean (ServiceRegistry serviceRegistry ) {
535
+ final Integer typeCode =
536
+ getConfiguredTypeCode ( serviceRegistry , MappingSettings .PREFERRED_BOOLEAN_JDBC_TYPE );
537
+ return typeCode != null
538
+ ? typeCode
539
+ : serviceRegistry .requireService ( JdbcServices .class )
540
+ .getDialect ().getPreferredSqlTypeCodeForBoolean ();
542
541
}
543
542
544
543
@ Incubating
545
544
public static synchronized int getPreferredSqlTypeCodeForBoolean (ServiceRegistry serviceRegistry , Dialect dialect ) {
546
- final Integer typeCode = serviceRegistry .requireService ( ConfigurationService .class ).getSetting (
547
- AvailableSettings .PREFERRED_BOOLEAN_JDBC_TYPE ,
548
- TypeCodeConverter .INSTANCE
549
- );
550
- if ( typeCode != null ) {
551
- INCUBATION_LOGGER .incubatingSetting ( AvailableSettings .PREFERRED_BOOLEAN_JDBC_TYPE );
552
- return typeCode ;
553
- }
554
-
555
- // default to the Dialect answer
556
- return dialect .getPreferredSqlTypeCodeForBoolean ();
545
+ final Integer typeCode =
546
+ getConfiguredTypeCode ( serviceRegistry , MappingSettings .PREFERRED_BOOLEAN_JDBC_TYPE );
547
+ return typeCode != null ? typeCode : dialect .getPreferredSqlTypeCodeForBoolean ();
557
548
}
558
549
559
550
@ Incubating
560
- public static synchronized int getPreferredSqlTypeCodeForDuration (StandardServiceRegistry serviceRegistry ) {
561
- final Integer explicitSetting = serviceRegistry .requireService ( ConfigurationService .class ).getSetting (
562
- AvailableSettings .PREFERRED_DURATION_JDBC_TYPE ,
563
- TypeCodeConverter .INSTANCE
564
- );
565
- if ( explicitSetting != null ) {
566
- INCUBATION_LOGGER .incubatingSetting ( AvailableSettings .PREFERRED_DURATION_JDBC_TYPE );
567
- return explicitSetting ;
568
- }
551
+ public static synchronized int getPreferredSqlTypeCodeForDuration (ServiceRegistry serviceRegistry ) {
552
+ final Integer explicitSetting =
553
+ getConfiguredTypeCode ( serviceRegistry , MappingSettings .PREFERRED_DURATION_JDBC_TYPE );
554
+ return explicitSetting != null ? explicitSetting : SqlTypes .DURATION ;
569
555
570
- return SqlTypes .DURATION ;
571
556
}
572
557
573
558
@ Incubating
574
- public static synchronized int getPreferredSqlTypeCodeForUuid (StandardServiceRegistry serviceRegistry ) {
575
- final Integer explicitSetting = serviceRegistry .requireService ( ConfigurationService .class ).getSetting (
576
- AvailableSettings .PREFERRED_UUID_JDBC_TYPE ,
577
- TypeCodeConverter .INSTANCE
578
- );
579
- if ( explicitSetting != null ) {
580
- INCUBATION_LOGGER .incubatingSetting ( AvailableSettings .PREFERRED_UUID_JDBC_TYPE );
581
- return explicitSetting ;
582
- }
559
+ public static synchronized int getPreferredSqlTypeCodeForUuid (ServiceRegistry serviceRegistry ) {
560
+ final Integer explicitSetting =
561
+ getConfiguredTypeCode ( serviceRegistry , MappingSettings .PREFERRED_UUID_JDBC_TYPE );
562
+ return explicitSetting != null ? explicitSetting : SqlTypes .UUID ;
583
563
584
- return SqlTypes .UUID ;
585
564
}
586
565
587
566
@ Incubating
588
- public static synchronized int getPreferredSqlTypeCodeForInstant (StandardServiceRegistry serviceRegistry ) {
589
- final Integer explicitSetting = serviceRegistry .requireService ( ConfigurationService .class ).getSetting (
590
- AvailableSettings .PREFERRED_INSTANT_JDBC_TYPE ,
591
- TypeCodeConverter .INSTANCE
592
- );
593
- if ( explicitSetting != null ) {
594
- INCUBATION_LOGGER .incubatingSetting ( AvailableSettings .PREFERRED_INSTANT_JDBC_TYPE );
595
- return explicitSetting ;
596
- }
567
+ public static synchronized int getPreferredSqlTypeCodeForInstant (ServiceRegistry serviceRegistry ) {
568
+ final Integer explicitSetting =
569
+ getConfiguredTypeCode ( serviceRegistry , MappingSettings .PREFERRED_INSTANT_JDBC_TYPE );
570
+ return explicitSetting != null ? explicitSetting : SqlTypes .TIMESTAMP_UTC ;
597
571
598
- return SqlTypes .TIMESTAMP_UTC ;
599
572
}
600
573
601
574
@ Incubating
602
- public static synchronized int getPreferredSqlTypeCodeForArray (StandardServiceRegistry serviceRegistry ) {
603
- final Integer explicitSetting = serviceRegistry .requireService ( ConfigurationService .class ).getSetting (
604
- AvailableSettings .PREFERRED_ARRAY_JDBC_TYPE ,
605
- TypeCodeConverter .INSTANCE
606
- );
607
- if ( explicitSetting != null ) {
608
- INCUBATION_LOGGER .incubatingSetting ( AvailableSettings .PREFERRED_ARRAY_JDBC_TYPE );
609
- return explicitSetting ;
610
- }
611
- // default to the Dialect answer
612
- return serviceRegistry .requireService ( JdbcServices .class )
613
- .getJdbcEnvironment ()
614
- .getDialect ()
615
- .getPreferredSqlTypeCodeForArray ();
575
+ public static synchronized int getPreferredSqlTypeCodeForArray (ServiceRegistry serviceRegistry ) {
576
+ final Integer explicitSetting =
577
+ getConfiguredTypeCode ( serviceRegistry , MappingSettings .PREFERRED_ARRAY_JDBC_TYPE );
578
+ return explicitSetting != null
579
+ ? explicitSetting
580
+ : serviceRegistry .requireService ( JdbcServices .class )
581
+ .getDialect ().getPreferredSqlTypeCodeForArray ();
616
582
}
617
583
618
584
public static void setIfNotEmpty (String value , String settingName , Map <String , String > configuration ) {
0 commit comments