Add support for Java 9's "java.locale.providers" in SystemProperties

Add most common primitives as getters
This commit is contained in:
Gary Gregory 2023-04-19 15:17:45 -04:00
parent cdcd915d02
commit 16c34b222d
3 changed files with 126 additions and 11 deletions

View File

@ -103,6 +103,11 @@ public final class SystemProperties {
*/
public static final String JAVA_LIBRARY_PATH = "java.library.path";
/**
* The System property name {@value}.
*/
public static final String JAVA_LOCALE_PROVIDERS = "java.locale.providers";
/**
* The System property name {@value}.
*/
@ -255,6 +260,20 @@ public final class SystemProperties {
return getProperty(AWT_TOOLKIT);
}
/**
* Gets the current value for the property named {@code key} as an {@code boolean}.
*
* @param key
* The key
* @param defaultIfAbsent
* The default value
* @return an {@code boolean} or defaultIfAbsent
*/
public static boolean getBoolean(final String key, final boolean defaultIfAbsent) {
final String str = getProperty(key);
return str == null ? defaultIfAbsent : Boolean.parseBoolean(str);
}
/**
* Gets the current value from the system properties map.
* <p>
@ -282,6 +301,20 @@ public final class SystemProperties {
return getProperty(FILE_SEPARATOR);
}
/**
* Gets the current value for the property named {@code key} as an {@code int}.
*
* @param key
* The key
* @param defaultIfAbsent
* The default value
* @return an {@code int} or defaultIfAbsent
*/
public static int getInt(final String key, final int defaultIfAbsent) {
final String str = getProperty(key);
return str == null ? defaultIfAbsent : Integer.parseInt(str);
}
/**
* Gets the current value from the system properties map.
* <p>
@ -426,6 +459,20 @@ public final class SystemProperties {
return getProperty(JAVA_LIBRARY_PATH);
}
/**
* Gets the current value from the system properties map.
* <p>
* Returns {@code null} if the property cannot be read due to a {@link SecurityException}.
* </p>
* <p>
* Java 9 and above.
* </p>
* @return the current value from the system properties map.
*/
public static String getJavaLocaleProviders() {
return getProperty(JAVA_LOCALE_PROVIDERS);
}
/**
* Gets the current value from the system properties map.
* <p>
@ -630,6 +677,20 @@ public final class SystemProperties {
return getProperty(LINE_SEPARATOR);
}
/**
* Gets the current value for the property named {@code key} as a {@code long}.
*
* @param key
* The key
* @param defaultIfAbsent
* The default value
* @return a {@code long} or defaultIfAbsent
*/
public static long getLong(final String key, final long defaultIfAbsent) {
final String str = getProperty(key);
return str == null ? defaultIfAbsent : Long.parseLong(str);
}
/**
* Gets the current value from the system properties map.
* <p>

View File

@ -56,7 +56,6 @@ run_inlined_CharUtils_isAsciiNumeric: 84,420 milliseconds.
*/
public class CharUtilsPerfRun {
private static final String VERSION = "$Id$";
private static final int WARM_UP = 100;
@ -76,23 +75,22 @@ public class CharUtilsPerfRun {
}
private void printSysInfo() {
System.out.println(VERSION);
System.out.println("Now: " + Calendar.getInstance().getTime());
System.out.println(System.getProperty("java.vendor")
System.out.println(SystemProperties.getJavaVendor()
+ " "
+ System.getProperty("java.runtime.name")
+ SystemProperties.getJavaRuntimeName()
+ " "
+ System.getProperty("java.runtime.version"));
System.out.println(System.getProperty("java.vm.vendor")
+ SystemProperties.getJavaRuntimeVersion());
System.out.println(SystemProperties.getJavaVmVendor()
+ " "
+ System.getProperty("java.vm.name")
+ SystemProperties.getJavaVmName()
+ " "
+ System.getProperty("java.vm.version"));
System.out.println(System.getProperty("os.name")
+ SystemProperties.getJavaVmVersion());
System.out.println(SystemProperties.getOsName()
+ " "
+ System.getProperty("os.version")
+ SystemProperties.getOsVersion()
+ " "
+ System.getProperty("os.arch")
+ SystemProperties.getOsArch()
+ " "
+ System.getProperty("sun.cpu.isalist"));
}

View File

@ -18,8 +18,11 @@
package org.apache.commons.lang3;
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assumptions.assumeTrue;
import org.junit.jupiter.api.Test;
public class SystemPropertiesTest {
@ -116,6 +119,13 @@ public class SystemPropertiesTest {
assertNotNull(SystemProperties.getJavaLibraryPath());
}
@Test
public void testGetJavaLocaleProviders() {
assumeTrue(SystemUtils.isJavaVersionAtLeast(JavaVersion.JAVA_9));
// default is null
assertNull(SystemProperties.getJavaLocaleProviders(), SystemProperties.getJavaVersion());
}
@Test
public void testGetJavaRuntimeName() {
assertNotNull(SystemProperties.getJavaRuntimeName());
@ -211,6 +221,52 @@ public class SystemPropertiesTest {
assertNotNull(SystemProperties.getOsName());
}
@Test
public void testGetBoolean() {
final String key = RandomStringUtils.random(10);
final String absentKey = RandomStringUtils.random(10);
assertNull(System.getProperty(absentKey));
try {
System.setProperty(key, Boolean.toString(Boolean.TRUE));
assertEquals(Boolean.TRUE, SystemProperties.getBoolean(key, false));
assertEquals(Boolean.TRUE, SystemProperties.getBoolean(absentKey, Boolean.TRUE));
assertEquals(false, SystemProperties.getBoolean(absentKey, false));
} finally {
System.clearProperty(key);
}
}
@Test
public void testGetInt() {
final String key = RandomStringUtils.random(10);
final String absentKey = RandomStringUtils.random(10);
assertNull(System.getProperty(absentKey));
try {
System.setProperty(key, Integer.toString(Integer.MAX_VALUE));
assertEquals(Integer.MAX_VALUE, SystemProperties.getInt(key, 0));
assertEquals(Integer.MAX_VALUE, SystemProperties.getInt(absentKey, Integer.MAX_VALUE));
assertEquals(0, SystemProperties.getInt(absentKey, 0));
} finally {
System.clearProperty(key);
}
}
@Test
public void testGetLong() {
final String key = RandomStringUtils.random(10);
final String absentKey = RandomStringUtils.random(10);
assertNull(System.getProperty(absentKey));
try {
System.setProperty(key, Long.toString(Long.MAX_VALUE));
assertEquals(Long.MAX_VALUE, SystemProperties.getLong(key, 0));
assertEquals(Long.MAX_VALUE, SystemProperties.getLong(absentKey, Long.MAX_VALUE));
assertEquals(0, SystemProperties.getLong(absentKey, 0));
} finally {
System.clearProperty(key);
}
}
@Test
public void testGetOsVersion() {
assertNotNull(SystemProperties.getOsVersion());