clean up lots of warnings in ConfigurationHelper
deprecate some unused methods add a useful method
This commit is contained in:
parent
c18b7c90fe
commit
6e38830cbe
|
@ -12,6 +12,7 @@ import java.util.Properties;
|
|||
import java.util.StringTokenizer;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
import org.checkerframework.checker.nullness.qual.NonNull;
|
||||
import org.hibernate.Incubating;
|
||||
import org.hibernate.boot.registry.StandardServiceRegistry;
|
||||
import org.hibernate.cfg.AvailableSettings;
|
||||
|
@ -50,12 +51,24 @@ public final class ConfigurationHelper {
|
|||
*
|
||||
* @return The value, or null if not found
|
||||
*/
|
||||
public static String getString(String name, Map values) {
|
||||
Object value = values.get( name );
|
||||
if ( value == null ) {
|
||||
return null;
|
||||
public static String getString(String name, Map<?,?> values) {
|
||||
final Object value = values.get( name );
|
||||
return value == null ? null : value.toString();
|
||||
}
|
||||
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.
|
||||
*/
|
||||
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 );
|
||||
}
|
||||
|
||||
|
@ -81,11 +94,7 @@ public final class ConfigurationHelper {
|
|||
*/
|
||||
public static String getString(String name, Map<?,?> values, Supplier<String> defaultValueSupplier) {
|
||||
final Object value = values.get( name );
|
||||
if ( value != null ) {
|
||||
return value.toString();
|
||||
}
|
||||
|
||||
return defaultValueSupplier.get();
|
||||
return value != null ? value.toString() : defaultValueSupplier.get();
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -96,7 +105,7 @@ public final class ConfigurationHelper {
|
|||
*
|
||||
* @return The value.
|
||||
*/
|
||||
public static boolean getBoolean(String name, Map values) {
|
||||
public static boolean getBoolean(String name, Map<?,?> values) {
|
||||
return getBoolean( name, values, false );
|
||||
}
|
||||
|
||||
|
@ -109,34 +118,33 @@ public final class ConfigurationHelper {
|
|||
*
|
||||
* @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 Boolean value = toBoolean( raw, defaultValue );
|
||||
if ( value == null ) {
|
||||
throw new ConfigurationException(
|
||||
"Could not determine how to handle configuration raw [name=" + name + ", value=" + raw + "] as boolean"
|
||||
);
|
||||
}
|
||||
|
||||
else {
|
||||
return value;
|
||||
}
|
||||
}
|
||||
|
||||
public static Boolean toBoolean(Object value, boolean defaultValue) {
|
||||
if ( value == null ) {
|
||||
return defaultValue;
|
||||
}
|
||||
|
||||
if (value instanceof Boolean) {
|
||||
return (Boolean) value;
|
||||
else if (value instanceof Boolean bool) {
|
||||
return bool;
|
||||
}
|
||||
|
||||
if (value instanceof String) {
|
||||
return Boolean.parseBoolean( (String) value );
|
||||
else if (value instanceof String string) {
|
||||
return Boolean.parseBoolean(string);
|
||||
}
|
||||
|
||||
else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the config value as a boolean (default of false)
|
||||
|
@ -146,21 +154,23 @@ public final class ConfigurationHelper {
|
|||
*
|
||||
* @return The value.
|
||||
*/
|
||||
public static Boolean getBooleanWrapper(String name, Map values, Boolean defaultValue) {
|
||||
Object value = values.get( name );
|
||||
public static Boolean getBooleanWrapper(String name, Map<?,?> values, Boolean defaultValue) {
|
||||
final Object value = values.get( name );
|
||||
if ( value == null ) {
|
||||
return defaultValue;
|
||||
}
|
||||
if (value instanceof Boolean) {
|
||||
return (Boolean) value;
|
||||
else if (value instanceof Boolean bool) {
|
||||
return bool;
|
||||
}
|
||||
if (value instanceof String) {
|
||||
return Boolean.valueOf( (String) value );
|
||||
else if (value instanceof String string) {
|
||||
return Boolean.valueOf(string);
|
||||
}
|
||||
else {
|
||||
throw new ConfigurationException(
|
||||
"Could not determine how to handle configuration value [name=" + name + ", value=" + value + "] as boolean"
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the config value as an int
|
||||
|
@ -171,16 +181,16 @@ public final class ConfigurationHelper {
|
|||
*
|
||||
* @return The value.
|
||||
*/
|
||||
public static int getInt(String name, Map values, int defaultValue) {
|
||||
Object value = values.get( name );
|
||||
public static int getInt(String name, Map<?,?> values, int defaultValue) {
|
||||
final Object value = values.get( name );
|
||||
if ( value == null ) {
|
||||
return defaultValue;
|
||||
}
|
||||
if (value instanceof Integer) {
|
||||
return (Integer) value;
|
||||
if (value instanceof Integer integer) {
|
||||
return integer;
|
||||
}
|
||||
if (value instanceof String) {
|
||||
return Integer.parseInt( (String) value );
|
||||
if (value instanceof String string) {
|
||||
return Integer.parseInt(string);
|
||||
}
|
||||
throw new ConfigurationException(
|
||||
"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
|
||||
*/
|
||||
public static Integer getInteger(String name, Map values) {
|
||||
Object value = values.get( name );
|
||||
public static Integer getInteger(String name, Map<?,?> values) {
|
||||
final Object value = values.get( name );
|
||||
if ( value == null ) {
|
||||
return null;
|
||||
}
|
||||
if (value instanceof Integer) {
|
||||
return (Integer) value;
|
||||
else if (value instanceof Integer integer) {
|
||||
return integer;
|
||||
}
|
||||
if (value instanceof String) {
|
||||
else if (value instanceof String string) {
|
||||
//empty values are ignored
|
||||
final String trimmed = value.toString().trim();
|
||||
if ( trimmed.isEmpty() ) {
|
||||
return null;
|
||||
}
|
||||
return Integer.valueOf( trimmed );
|
||||
final String trimmed = string.trim();
|
||||
return trimmed.isEmpty() ? null : Integer.valueOf( trimmed );
|
||||
}
|
||||
throw new ConfigurationException(
|
||||
"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) {
|
||||
Object value = values.get( name );
|
||||
public static long getLong(String name, Map<?,?> values, int defaultValue) {
|
||||
final Object value = values.get( name );
|
||||
if ( value == null ) {
|
||||
return defaultValue;
|
||||
}
|
||||
if (value instanceof Long) {
|
||||
return (Long) value;
|
||||
else if (value instanceof Long number) {
|
||||
return number;
|
||||
}
|
||||
if (value instanceof String) {
|
||||
return Long.parseLong( (String) value );
|
||||
else if (value instanceof String string) {
|
||||
return Long.parseLong(string);
|
||||
}
|
||||
else {
|
||||
throw new ConfigurationException(
|
||||
"Could not determine how to handle configuration value [name=" + name +
|
||||
", value=" + value + "(" + value.getClass().getName() + ")] as long"
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Make a clone of the configuration values.
|
||||
|
@ -241,26 +250,25 @@ public final class ConfigurationHelper {
|
|||
* @param configurationValues The config values to clone
|
||||
*
|
||||
* @return The clone
|
||||
*
|
||||
* @deprecated No longer used
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
@SuppressWarnings("rawtypes")
|
||||
@Deprecated(since = "7", forRemoval = true)
|
||||
public static Map clone(Map<?,?> configurationValues) {
|
||||
if ( configurationValues == null ) {
|
||||
return null;
|
||||
}
|
||||
else if ( configurationValues instanceof Properties properties ) {
|
||||
// If a Properties object, leverage its clone() impl
|
||||
if (configurationValues instanceof Properties) {
|
||||
return (Properties) ( (Properties) configurationValues ).clone();
|
||||
return (Properties) properties.clone();
|
||||
}
|
||||
else {
|
||||
// Otherwise make a manual copy
|
||||
HashMap clone = new HashMap();
|
||||
for ( Map.Entry entry : configurationValues.entrySet() ) {
|
||||
clone.put( entry.getKey(), entry.getValue() );
|
||||
return new HashMap<>( configurationValues );
|
||||
}
|
||||
return clone;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* replace a property by a starred version
|
||||
*
|
||||
|
@ -270,17 +278,13 @@ public final class ConfigurationHelper {
|
|||
* @return cloned and masked properties
|
||||
*/
|
||||
public static Properties maskOut(Properties props, String key) {
|
||||
Properties clone = ( Properties ) props.clone();
|
||||
final Properties clone = (Properties) props.clone();
|
||||
if ( clone.get( key ) != null ) {
|
||||
clone.setProperty( key, "****" );
|
||||
}
|
||||
return clone;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Extract a property value by name from the given properties object.
|
||||
* <p>
|
||||
|
@ -310,7 +314,7 @@ public final class ConfigurationHelper {
|
|||
* @param properties The properties object
|
||||
* @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 );
|
||||
if ( value == null ) {
|
||||
return null;
|
||||
|
@ -324,7 +328,7 @@ public final class ConfigurationHelper {
|
|||
|
||||
public static String extractValue(
|
||||
String name,
|
||||
Map values,
|
||||
Map<?,?> values,
|
||||
Supplier<String> fallbackValueFactory) {
|
||||
final String value = extractPropertyValue( name, values );
|
||||
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 properties The properties object
|
||||
* @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) {
|
||||
Map map = new HashMap();
|
||||
Map<String,String> map = new HashMap<>();
|
||||
String value = extractPropertyValue( propertyName, properties );
|
||||
if ( value != null ) {
|
||||
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 properties The properties object
|
||||
* @return The resulting map; never null, though perhaps empty.
|
||||
*
|
||||
* @deprecated No longer used
|
||||
*/
|
||||
public static Map toMap(String propertyName, String delim, Map properties) {
|
||||
Map map = new HashMap();
|
||||
@SuppressWarnings("rawtypes")
|
||||
@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 );
|
||||
if ( value != null ) {
|
||||
StringTokenizer tokens = new StringTokenizer( value, delim );
|
||||
|
@ -423,10 +435,10 @@ public final class ConfigurationHelper {
|
|||
*
|
||||
* @param configurationValues The configuration map.
|
||||
*/
|
||||
public static void resolvePlaceHolders(Map<?,?> configurationValues) {
|
||||
Iterator itr = configurationValues.entrySet().iterator();
|
||||
public static void resolvePlaceHolders(Map<?,Object> configurationValues) {
|
||||
final Iterator<? extends Map.Entry<?,Object>> itr = configurationValues.entrySet().iterator();
|
||||
while ( itr.hasNext() ) {
|
||||
final Map.Entry entry = ( Map.Entry ) itr.next();
|
||||
final Map.Entry<?,Object> entry = itr.next();
|
||||
final Object value = entry.getValue();
|
||||
if (value instanceof String) {
|
||||
final String resolved = resolvePlaceHolder( ( String ) value );
|
||||
|
@ -449,7 +461,7 @@ public final class ConfigurationHelper {
|
|||
* @return The (possibly) interpolated property value.
|
||||
*/
|
||||
public static String resolvePlaceHolder(String property) {
|
||||
if ( property.indexOf( PLACEHOLDER_START ) < 0 ) {
|
||||
if ( !property.contains( PLACEHOLDER_START ) ) {
|
||||
return property;
|
||||
}
|
||||
StringBuilder buff = new StringBuilder();
|
||||
|
@ -469,7 +481,7 @@ public final class ConfigurationHelper {
|
|||
throw new IllegalArgumentException( "unmatched placeholder start [" + property + "]" );
|
||||
}
|
||||
}
|
||||
String systemProperty = extractFromSystem( systemPropertyName );
|
||||
final String systemProperty = extractFromSystem( systemPropertyName );
|
||||
buff.append( systemProperty == null ? "" : systemProperty );
|
||||
pos = x + 1;
|
||||
// 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] );
|
||||
}
|
||||
String rtn = buff.toString();
|
||||
return rtn.isEmpty() ? null : rtn;
|
||||
final String result = buff.toString();
|
||||
return result.isEmpty() ? null : result;
|
||||
}
|
||||
|
||||
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) {
|
||||
if ( value != null ) {
|
||||
configuration.put( settingName, value );
|
||||
|
@ -602,9 +615,10 @@ public final class ConfigurationHelper {
|
|||
public static final TypeCodeConverter INSTANCE = new TypeCodeConverter();
|
||||
|
||||
@Override
|
||||
@NonNull
|
||||
public Integer convert(Object value) {
|
||||
if ( value instanceof Number ) {
|
||||
return ( (Number) value ).intValue();
|
||||
if ( value instanceof Number number ) {
|
||||
return number.intValue();
|
||||
}
|
||||
|
||||
final String string = value.toString().toUpperCase( Locale.ROOT );
|
||||
|
|
Loading…
Reference in New Issue