You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by gg...@apache.org on 2023/04/19 19:17:50 UTC

[commons-lang] branch master updated: Add support for Java 9's "java.locale.providers" in SystemProperties

This is an automated email from the ASF dual-hosted git repository.

ggregory pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-lang.git


The following commit(s) were added to refs/heads/master by this push:
     new 16c34b222 Add support for Java 9's "java.locale.providers" in SystemProperties
16c34b222 is described below

commit 16c34b222d6a16849798e3345174ca9f0ebc430d
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Wed Apr 19 15:17:45 2023 -0400

    Add support for Java 9's "java.locale.providers" in SystemProperties
    
    Add most common primitives as getters
---
 .../org/apache/commons/lang3/SystemProperties.java | 61 ++++++++++++++++++++++
 .../org/apache/commons/lang3/CharUtilsPerfRun.java | 20 ++++---
 .../apache/commons/lang3/SystemPropertiesTest.java | 56 ++++++++++++++++++++
 3 files changed, 126 insertions(+), 11 deletions(-)

diff --git a/src/main/java/org/apache/commons/lang3/SystemProperties.java b/src/main/java/org/apache/commons/lang3/SystemProperties.java
index 221c7c973..a27aef602 100644
--- a/src/main/java/org/apache/commons/lang3/SystemProperties.java
+++ b/src/main/java/org/apache/commons/lang3/SystemProperties.java
@@ -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>
diff --git a/src/test/java/org/apache/commons/lang3/CharUtilsPerfRun.java b/src/test/java/org/apache/commons/lang3/CharUtilsPerfRun.java
index 1aa4d9879..56aa26ca4 100644
--- a/src/test/java/org/apache/commons/lang3/CharUtilsPerfRun.java
+++ b/src/test/java/org/apache/commons/lang3/CharUtilsPerfRun.java
@@ -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"));
     }
diff --git a/src/test/java/org/apache/commons/lang3/SystemPropertiesTest.java b/src/test/java/org/apache/commons/lang3/SystemPropertiesTest.java
index 46b282995..cd59fc2a1 100644
--- a/src/test/java/org/apache/commons/lang3/SystemPropertiesTest.java
+++ b/src/test/java/org/apache/commons/lang3/SystemPropertiesTest.java
@@ -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());