Default values in new SystemProperties APIs are now Strings instead of

Suppliers
This commit is contained in:
Gary Gregory 2024-05-02 15:23:04 -04:00
parent 059fb3b8c1
commit 5f72c77386
4 changed files with 22 additions and 14 deletions

View File

@ -278,7 +278,7 @@ public enum JavaVersion {
* @return the value of {@code java.specification.version} system property or 99.0 if it is not set.
*/
private static float maxVersion() {
final float v = toFloatVersion(SystemProperties.getJavaSpecificationVersion(() -> "99.0"));
final float v = toFloatVersion(SystemProperties.getJavaSpecificationVersion("99.0"));
return v > 0 ? v : 99f;
}

View File

@ -2335,7 +2335,7 @@ public final class SystemProperties {
* @return the current value from the system properties map.
* @since 3.15.0
*/
public static String getJavaSpecificationVersion(final Supplier<String> defaultValue) {
public static String getJavaSpecificationVersion(final String defaultValue) {
return getProperty(JAVA_SPECIFICATION_VERSION, defaultValue);
}
@ -3734,6 +3734,20 @@ public final class SystemProperties {
}
}
/**
* Gets a System property, defaulting to {@code null} if the property cannot be read.
* <p>
* If a {@link SecurityException} is caught, the return value is {@code null}.
* </p>
*
* @param property the system property name.
* @param defaultIfAbsent use this value when the property is empty or throws SecurityException.
* @return the system property value or {@code null} if a security problem occurs
*/
static String getProperty(final String property, final String defaultIfAbsent) {
return getProperty(property, () -> defaultIfAbsent);
}
/**
* Gets the current value from the system properties map.
* <p>
@ -3986,7 +4000,7 @@ public final class SystemProperties {
* @return the current value from the system properties map.
* @since 3.15.0
*/
public static String getUserName(final Supplier<String> defaultValue) {
public static String getUserName(final String defaultValue) {
return getProperty(USER_NAME, defaultValue);
}

View File

@ -17,7 +17,6 @@
package org.apache.commons.lang3;
import java.io.File;
import java.util.function.Supplier;
/**
* Helpers for {@link System}.
@ -2042,11 +2041,11 @@ public class SystemUtils {
* access to the specified system property.
* @see SystemProperties#getUserName()
* @since 3.10
* @deprecated Use {@link SystemProperties#getUserName(Supplier)}.
* @deprecated Use {@link SystemProperties#getUserName(String)}.
*/
@Deprecated
public static String getUserName(final String defaultValue) {
return System.getProperty(SystemProperties.USER_NAME, defaultValue);
return SystemProperties.getUserName(defaultValue);
}
/**

View File

@ -46,10 +46,6 @@ public class SystemPropertiesTest {
return SystemUtils.isJavaVersionAtLeast(JavaVersion.JAVA_11);
}
private boolean isJava21OrGreater() {
return SystemUtils.isJavaVersionAtLeast(JavaVersion.JAVA_21);
}
@Test
public void testActualKeys() {
basicKeyCheck(SystemProperties.APPLE_AWT_ENABLE_TEMPLATE_IMAGES);
@ -590,7 +586,7 @@ public class SystemPropertiesTest {
@Test
public void testGetJavaSpecificationVersionSupplier() {
assertNotNull(SystemProperties.getJavaSpecificationVersion(() -> "99.0"));
assertNotNull(SystemProperties.getJavaSpecificationVersion("99.0"));
}
@Test
@ -716,9 +712,8 @@ public class SystemPropertiesTest {
@Test
public void testGetUserName() {
assertNotNull(SystemProperties.getUserName());
assertNotNull(SystemProperties.getUserName(() -> ""));
assertNotNull(SystemProperties.getUserName(() -> "User"));
assertNotNull(SystemProperties.getUserName(() -> null));
assertNotNull(SystemProperties.getUserName(""));
assertNotNull(SystemProperties.getUserName("User"));
assertNotNull(SystemProperties.getUserName(null));
}