clean up lots of warnings in ConfigurationHelper

deprecate some unused methods
add a useful method
This commit is contained in:
Gavin King 2024-10-07 19:06:41 +02:00
parent c18b7c90fe
commit 6e38830cbe
1 changed files with 104 additions and 90 deletions

View File

@ -12,6 +12,7 @@ import java.util.Properties;
import java.util.StringTokenizer; import java.util.StringTokenizer;
import java.util.function.Supplier; import java.util.function.Supplier;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.hibernate.Incubating; import org.hibernate.Incubating;
import org.hibernate.boot.registry.StandardServiceRegistry; import org.hibernate.boot.registry.StandardServiceRegistry;
import org.hibernate.cfg.AvailableSettings; import org.hibernate.cfg.AvailableSettings;
@ -50,12 +51,24 @@ public final class ConfigurationHelper {
* *
* @return The value, or null if not found * @return The value, or null if not found
*/ */
public static String getString(String name, Map values) { public static String getString(String name, Map<?,?> values) {
Object value = values.get( name ); final Object value = values.get( name );
if ( value == null ) { return value == null ? null : value.toString();
return null;
} }
return value.toString();
/**
* Get the config value as a {@link String}
*
* @param preferred The preferred config setting name.
* @param fallback The fallback config setting name, when the preferred
* configuration is not set.
* @param values The map of config values
*
* @return The value, or null if not found
*/
public static String getString(String preferred, String fallback, Map<?,?> values) {
final String preferredValue = getString( preferred, values );
return preferredValue == null ? getString( fallback, values ) : preferredValue;
} }
/** /**
@ -67,7 +80,7 @@ public final class ConfigurationHelper {
* *
* @return The value. * @return The value.
*/ */
public static String getString(String name, Map values, String defaultValue) { public static String getString(String name, Map<?,?> values, String defaultValue) {
return getString( name, values, () -> defaultValue ); return getString( name, values, () -> defaultValue );
} }
@ -81,11 +94,7 @@ public final class ConfigurationHelper {
*/ */
public static String getString(String name, Map<?,?> values, Supplier<String> defaultValueSupplier) { public static String getString(String name, Map<?,?> values, Supplier<String> defaultValueSupplier) {
final Object value = values.get( name ); final Object value = values.get( name );
if ( value != null ) { return value != null ? value.toString() : defaultValueSupplier.get();
return value.toString();
}
return defaultValueSupplier.get();
} }
/** /**
@ -96,7 +105,7 @@ public final class ConfigurationHelper {
* *
* @return The value. * @return The value.
*/ */
public static boolean getBoolean(String name, Map values) { public static boolean getBoolean(String name, Map<?,?> values) {
return getBoolean( name, values, false ); return getBoolean( name, values, false );
} }
@ -109,34 +118,33 @@ public final class ConfigurationHelper {
* *
* @return The value. * @return The value.
*/ */
public static boolean getBoolean(String name, Map values, boolean defaultValue) { public static boolean getBoolean(String name, Map<?,?> values, boolean defaultValue) {
final Object raw = values.get( name ); final Object raw = values.get( name );
final Boolean value = toBoolean( raw, defaultValue ); final Boolean value = toBoolean( raw, defaultValue );
if ( value == null ) { if ( value == null ) {
throw new ConfigurationException( throw new ConfigurationException(
"Could not determine how to handle configuration raw [name=" + name + ", value=" + raw + "] as boolean" "Could not determine how to handle configuration raw [name=" + name + ", value=" + raw + "] as boolean"
); );
} }
else {
return value; return value;
} }
}
public static Boolean toBoolean(Object value, boolean defaultValue) { public static Boolean toBoolean(Object value, boolean defaultValue) {
if ( value == null ) { if ( value == null ) {
return defaultValue; return defaultValue;
} }
else if (value instanceof Boolean bool) {
if (value instanceof Boolean) { return bool;
return (Boolean) value;
} }
else if (value instanceof String string) {
if (value instanceof String) { return Boolean.parseBoolean(string);
return Boolean.parseBoolean( (String) value );
} }
else {
return null; return null;
} }
}
/** /**
* Get the config value as a boolean (default of false) * Get the config value as a boolean (default of false)
@ -146,21 +154,23 @@ public final class ConfigurationHelper {
* *
* @return The value. * @return The value.
*/ */
public static Boolean getBooleanWrapper(String name, Map values, Boolean defaultValue) { public static Boolean getBooleanWrapper(String name, Map<?,?> values, Boolean defaultValue) {
Object value = values.get( name ); final Object value = values.get( name );
if ( value == null ) { if ( value == null ) {
return defaultValue; return defaultValue;
} }
if (value instanceof Boolean) { else if (value instanceof Boolean bool) {
return (Boolean) value; return bool;
} }
if (value instanceof String) { else if (value instanceof String string) {
return Boolean.valueOf( (String) value ); return Boolean.valueOf(string);
} }
else {
throw new ConfigurationException( throw new ConfigurationException(
"Could not determine how to handle configuration value [name=" + name + ", value=" + value + "] as boolean" "Could not determine how to handle configuration value [name=" + name + ", value=" + value + "] as boolean"
); );
} }
}
/** /**
* Get the config value as an int * Get the config value as an int
@ -171,16 +181,16 @@ public final class ConfigurationHelper {
* *
* @return The value. * @return The value.
*/ */
public static int getInt(String name, Map values, int defaultValue) { public static int getInt(String name, Map<?,?> values, int defaultValue) {
Object value = values.get( name ); final Object value = values.get( name );
if ( value == null ) { if ( value == null ) {
return defaultValue; return defaultValue;
} }
if (value instanceof Integer) { if (value instanceof Integer integer) {
return (Integer) value; return integer;
} }
if (value instanceof String) { if (value instanceof String string) {
return Integer.parseInt( (String) value ); return Integer.parseInt(string);
} }
throw new ConfigurationException( throw new ConfigurationException(
"Could not determine how to handle configuration value [name=" + name + "Could not determine how to handle configuration value [name=" + name +
@ -196,21 +206,18 @@ public final class ConfigurationHelper {
* *
* @return The value, or null if not found * @return The value, or null if not found
*/ */
public static Integer getInteger(String name, Map values) { public static Integer getInteger(String name, Map<?,?> values) {
Object value = values.get( name ); final Object value = values.get( name );
if ( value == null ) { if ( value == null ) {
return null; return null;
} }
if (value instanceof Integer) { else if (value instanceof Integer integer) {
return (Integer) value; return integer;
} }
if (value instanceof String) { else if (value instanceof String string) {
//empty values are ignored //empty values are ignored
final String trimmed = value.toString().trim(); final String trimmed = string.trim();
if ( trimmed.isEmpty() ) { return trimmed.isEmpty() ? null : Integer.valueOf( trimmed );
return null;
}
return Integer.valueOf( trimmed );
} }
throw new ConfigurationException( throw new ConfigurationException(
"Could not determine how to handle configuration value [name=" + name + "Could not determine how to handle configuration value [name=" + name +
@ -218,22 +225,24 @@ public final class ConfigurationHelper {
); );
} }
public static long getLong(String name, Map values, int defaultValue) { public static long getLong(String name, Map<?,?> values, int defaultValue) {
Object value = values.get( name ); final Object value = values.get( name );
if ( value == null ) { if ( value == null ) {
return defaultValue; return defaultValue;
} }
if (value instanceof Long) { else if (value instanceof Long number) {
return (Long) value; return number;
} }
if (value instanceof String) { else if (value instanceof String string) {
return Long.parseLong( (String) value ); return Long.parseLong(string);
} }
else {
throw new ConfigurationException( throw new ConfigurationException(
"Could not determine how to handle configuration value [name=" + name + "Could not determine how to handle configuration value [name=" + name +
", value=" + value + "(" + value.getClass().getName() + ")] as long" ", value=" + value + "(" + value.getClass().getName() + ")] as long"
); );
} }
}
/** /**
* Make a clone of the configuration values. * Make a clone of the configuration values.
@ -241,26 +250,25 @@ public final class ConfigurationHelper {
* @param configurationValues The config values to clone * @param configurationValues The config values to clone
* *
* @return The clone * @return The clone
*
* @deprecated No longer used
*/ */
@SuppressWarnings("unchecked") @SuppressWarnings("rawtypes")
@Deprecated(since = "7", forRemoval = true)
public static Map clone(Map<?,?> configurationValues) { public static Map clone(Map<?,?> configurationValues) {
if ( configurationValues == null ) { if ( configurationValues == null ) {
return null; return null;
} }
else if ( configurationValues instanceof Properties properties ) {
// If a Properties object, leverage its clone() impl // If a Properties object, leverage its clone() impl
if (configurationValues instanceof Properties) { return (Properties) properties.clone();
return (Properties) ( (Properties) configurationValues ).clone();
} }
else {
// Otherwise make a manual copy // Otherwise make a manual copy
HashMap clone = new HashMap(); return new HashMap<>( configurationValues );
for ( Map.Entry entry : configurationValues.entrySet() ) {
clone.put( entry.getKey(), entry.getValue() );
} }
return clone;
} }
/** /**
* replace a property by a starred version * replace a property by a starred version
* *
@ -270,17 +278,13 @@ public final class ConfigurationHelper {
* @return cloned and masked properties * @return cloned and masked properties
*/ */
public static Properties maskOut(Properties props, String key) { public static Properties maskOut(Properties props, String key) {
Properties clone = ( Properties ) props.clone(); final Properties clone = (Properties) props.clone();
if ( clone.get( key ) != null ) { if ( clone.get( key ) != null ) {
clone.setProperty( key, "****" ); clone.setProperty( key, "****" );
} }
return clone; return clone;
} }
/** /**
* Extract a property value by name from the given properties object. * Extract a property value by name from the given properties object.
* <p> * <p>
@ -310,7 +314,7 @@ public final class ConfigurationHelper {
* @param properties The properties object * @param properties The properties object
* @return The property value; may be null. * @return The property value; may be null.
*/ */
public static String extractPropertyValue(String propertyName, Map properties) { public static String extractPropertyValue(String propertyName, Map<?,?> properties) {
String value = (String) properties.get( propertyName ); String value = (String) properties.get( propertyName );
if ( value == null ) { if ( value == null ) {
return null; return null;
@ -324,7 +328,7 @@ public final class ConfigurationHelper {
public static String extractValue( public static String extractValue(
String name, String name,
Map values, Map<?,?> values,
Supplier<String> fallbackValueFactory) { Supplier<String> fallbackValueFactory) {
final String value = extractPropertyValue( name, values ); final String value = extractPropertyValue( name, values );
if ( value != null ) { if ( value != null ) {
@ -346,9 +350,13 @@ public final class ConfigurationHelper {
* @param delim The string defining tokens used as both entry and key/value delimiters. * @param delim The string defining tokens used as both entry and key/value delimiters.
* @param properties The properties object * @param properties The properties object
* @return The resulting map; never null, though perhaps empty. * @return The resulting map; never null, though perhaps empty.
*
* @deprecated No longer used
*/ */
@SuppressWarnings("rawtypes")
@Deprecated(since = "7", forRemoval = true)
public static Map toMap(String propertyName, String delim, Properties properties) { public static Map toMap(String propertyName, String delim, Properties properties) {
Map map = new HashMap(); Map<String,String> map = new HashMap<>();
String value = extractPropertyValue( propertyName, properties ); String value = extractPropertyValue( propertyName, properties );
if ( value != null ) { if ( value != null ) {
StringTokenizer tokens = new StringTokenizer( value, delim ); StringTokenizer tokens = new StringTokenizer( value, delim );
@ -371,9 +379,13 @@ public final class ConfigurationHelper {
* @param delim The string defining tokens used as both entry and key/value delimiters. * @param delim The string defining tokens used as both entry and key/value delimiters.
* @param properties The properties object * @param properties The properties object
* @return The resulting map; never null, though perhaps empty. * @return The resulting map; never null, though perhaps empty.
*
* @deprecated No longer used
*/ */
public static Map toMap(String propertyName, String delim, Map properties) { @SuppressWarnings("rawtypes")
Map map = new HashMap(); @Deprecated(since = "7", forRemoval = true)
public static Map toMap(String propertyName, String delim, Map<?,?> properties) {
Map<String,String> map = new HashMap<>();
String value = extractPropertyValue( propertyName, properties ); String value = extractPropertyValue( propertyName, properties );
if ( value != null ) { if ( value != null ) {
StringTokenizer tokens = new StringTokenizer( value, delim ); StringTokenizer tokens = new StringTokenizer( value, delim );
@ -423,10 +435,10 @@ public final class ConfigurationHelper {
* *
* @param configurationValues The configuration map. * @param configurationValues The configuration map.
*/ */
public static void resolvePlaceHolders(Map<?,?> configurationValues) { public static void resolvePlaceHolders(Map<?,Object> configurationValues) {
Iterator itr = configurationValues.entrySet().iterator(); final Iterator<? extends Map.Entry<?,Object>> itr = configurationValues.entrySet().iterator();
while ( itr.hasNext() ) { while ( itr.hasNext() ) {
final Map.Entry entry = ( Map.Entry ) itr.next(); final Map.Entry<?,Object> entry = itr.next();
final Object value = entry.getValue(); final Object value = entry.getValue();
if (value instanceof String) { if (value instanceof String) {
final String resolved = resolvePlaceHolder( ( String ) value ); final String resolved = resolvePlaceHolder( ( String ) value );
@ -449,7 +461,7 @@ public final class ConfigurationHelper {
* @return The (possibly) interpolated property value. * @return The (possibly) interpolated property value.
*/ */
public static String resolvePlaceHolder(String property) { public static String resolvePlaceHolder(String property) {
if ( property.indexOf( PLACEHOLDER_START ) < 0 ) { if ( !property.contains( PLACEHOLDER_START ) ) {
return property; return property;
} }
StringBuilder buff = new StringBuilder(); StringBuilder buff = new StringBuilder();
@ -469,7 +481,7 @@ public final class ConfigurationHelper {
throw new IllegalArgumentException( "unmatched placeholder start [" + property + "]" ); throw new IllegalArgumentException( "unmatched placeholder start [" + property + "]" );
} }
} }
String systemProperty = extractFromSystem( systemPropertyName ); final String systemProperty = extractFromSystem( systemPropertyName );
buff.append( systemProperty == null ? "" : systemProperty ); buff.append( systemProperty == null ? "" : systemProperty );
pos = x + 1; pos = x + 1;
// make sure spinning forward did not put us past the end of the buffer... // make sure spinning forward did not put us past the end of the buffer...
@ -480,8 +492,8 @@ public final class ConfigurationHelper {
} }
buff.append( chars[pos] ); buff.append( chars[pos] );
} }
String rtn = buff.toString(); final String result = buff.toString();
return rtn.isEmpty() ? null : rtn; return result.isEmpty() ? null : result;
} }
private static String extractFromSystem(String systemPropertyName) { private static String extractFromSystem(String systemPropertyName) {
@ -591,6 +603,7 @@ public final class ConfigurationHelper {
} }
} }
@Deprecated(since = "7", forRemoval = true)
public static void setIfNotNull(Object value, String settingName, Map<String, Object> configuration) { public static void setIfNotNull(Object value, String settingName, Map<String, Object> configuration) {
if ( value != null ) { if ( value != null ) {
configuration.put( settingName, value ); configuration.put( settingName, value );
@ -602,9 +615,10 @@ public final class ConfigurationHelper {
public static final TypeCodeConverter INSTANCE = new TypeCodeConverter(); public static final TypeCodeConverter INSTANCE = new TypeCodeConverter();
@Override @Override
@NonNull
public Integer convert(Object value) { public Integer convert(Object value) {
if ( value instanceof Number ) { if ( value instanceof Number number ) {
return ( (Number) value ).intValue(); return number.intValue();
} }
final String string = value.toString().toUpperCase( Locale.ROOT ); final String string = value.toString().toUpperCase( Locale.ROOT );