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 7d7e8af45 Better common primitives as getters
7d7e8af45 is described below

commit 7d7e8af45032f1f3a29a18888cd3b0ca6085a156
Author: Gary Gregory <[email protected]>
AuthorDate: Wed Apr 19 16:47:08 2023 -0400

    Better common primitives as getters
---
 .../org/apache/commons/lang3/SystemProperties.java  | 15 +++++++++------
 .../apache/commons/lang3/SystemPropertiesTest.java  | 21 ++++++++++++---------
 2 files changed, 21 insertions(+), 15 deletions(-)

diff --git a/src/main/java/org/apache/commons/lang3/SystemProperties.java 
b/src/main/java/org/apache/commons/lang3/SystemProperties.java
index a27aef602..1e54f0fda 100644
--- a/src/main/java/org/apache/commons/lang3/SystemProperties.java
+++ b/src/main/java/org/apache/commons/lang3/SystemProperties.java
@@ -17,6 +17,9 @@
 
 package org.apache.commons.lang3;
 
+import java.util.function.BooleanSupplier;
+import java.util.function.IntSupplier;
+import java.util.function.LongSupplier;
 import java.util.function.Supplier;
 
 /**
@@ -269,9 +272,9 @@ public final class SystemProperties {
      *            The default value
      * @return an {@code boolean} or defaultIfAbsent
      */
-    public static boolean getBoolean(final String key, final boolean 
defaultIfAbsent) {
+    public static boolean getBoolean(final String key, final BooleanSupplier 
defaultIfAbsent) {
         final String str = getProperty(key);
-        return str == null ? defaultIfAbsent : Boolean.parseBoolean(str);
+        return str == null ? defaultIfAbsent != null && 
defaultIfAbsent.getAsBoolean() : Boolean.parseBoolean(str);
     }
 
     /**
@@ -310,9 +313,9 @@ public final class SystemProperties {
      *            The default value
      * @return an {@code int} or defaultIfAbsent
      */
-    public static int getInt(final String key, final int defaultIfAbsent) {
+    public static int getInt(final String key, final IntSupplier 
defaultIfAbsent) {
         final String str = getProperty(key);
-        return str == null ? defaultIfAbsent : Integer.parseInt(str);
+        return str == null ? defaultIfAbsent != null ? 
defaultIfAbsent.getAsInt() : 0 : Integer.parseInt(str);
     }
 
     /**
@@ -686,9 +689,9 @@ public final class SystemProperties {
      *            The default value
      * @return a {@code long} or defaultIfAbsent
      */
-    public static long getLong(final String key, final long defaultIfAbsent) {
+    public static long getLong(final String key, final LongSupplier 
defaultIfAbsent) {
         final String str = getProperty(key);
-        return str == null ? defaultIfAbsent : Long.parseLong(str);
+        return str == null ? defaultIfAbsent != null ? 
defaultIfAbsent.getAsLong() : 0 : Long.parseLong(str);
     }
 
     /**
diff --git a/src/test/java/org/apache/commons/lang3/SystemPropertiesTest.java 
b/src/test/java/org/apache/commons/lang3/SystemPropertiesTest.java
index cd59fc2a1..4a24724d0 100644
--- a/src/test/java/org/apache/commons/lang3/SystemPropertiesTest.java
+++ b/src/test/java/org/apache/commons/lang3/SystemPropertiesTest.java
@@ -228,9 +228,10 @@ public class SystemPropertiesTest {
         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));
+            assertEquals(Boolean.TRUE, SystemProperties.getBoolean(key, () -> 
false));
+            assertEquals(Boolean.TRUE, SystemProperties.getBoolean(absentKey, 
() -> Boolean.TRUE));
+            assertEquals(false, SystemProperties.getBoolean(absentKey, () -> 
false));
+            assertEquals(false, SystemProperties.getBoolean(absentKey, null));
         } finally {
             System.clearProperty(key);
         }
@@ -243,9 +244,10 @@ public class SystemPropertiesTest {
         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));
+            assertEquals(Integer.MAX_VALUE, SystemProperties.getInt(key, () -> 
0));
+            assertEquals(Integer.MAX_VALUE, SystemProperties.getInt(absentKey, 
() -> Integer.MAX_VALUE));
+            assertEquals(0, SystemProperties.getInt(absentKey, () -> 0));
+            assertEquals(0, SystemProperties.getInt(absentKey, null));
         } finally {
             System.clearProperty(key);
         }
@@ -258,9 +260,10 @@ public class SystemPropertiesTest {
         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));
+            assertEquals(Long.MAX_VALUE, SystemProperties.getLong(key, () -> 
0));
+            assertEquals(Long.MAX_VALUE, SystemProperties.getLong(absentKey, 
() -> Long.MAX_VALUE));
+            assertEquals(0, SystemProperties.getLong(absentKey, () -> 0));
+            assertEquals(0, SystemProperties.getLong(absentKey, null));
         } finally {
             System.clearProperty(key);
         }

Reply via email to