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 2025/09/18 15:10:55 UTC
(commons-lang) 01/02: Add SystemProperties.getBoolean(Class, String, BooleanSupplier)
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
commit 8b27dec0340e501fee098278d358d7ddd62ad2ad
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Thu Sep 18 11:08:35 2025 -0400
Add SystemProperties.getBoolean(Class, String, BooleanSupplier)
- Add SystemProperties.getInt(Class, String, IntSupplier)
- Add SystemProperties.getLong(Class, String, LongSupplier)
---
src/changes/changes.xml | 3 +
.../java/org/apache/commons/lang3/ClassUtils.java | 6 +-
.../org/apache/commons/lang3/SystemProperties.java | 82 +++++++++++++++++++---
.../apache/commons/lang3/SystemPropertiesTest.java | 55 +++++++++++++++
4 files changed, 136 insertions(+), 10 deletions(-)
diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index d78dbff35..248a05137 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -96,6 +96,9 @@ The <action> type attribute can be add,update,fix,remove.
<action type="add" dev="ggregory" due-to="Gary Gregory">Add DurationUtils.get(String, TemporalUnit, long).</action>
<action type="add" dev="ggregory" due-to="Gary Gregory">Add DurationUtils.getMillis(String, long).</action>
<action type="add" dev="ggregory" due-to="Gary Gregory">Add DurationUtils.getSeconds(String, long).</action>
+ <action type="add" dev="ggregory" due-to="Gary Gregory">Add SystemProperties.getBoolean(Class, String, boolean).</action>
+ <action type="add" dev="ggregory" due-to="Gary Gregory">Add SystemProperties.getInt(Class, String, int).</action>
+ <action type="add" dev="ggregory" due-to="Gary Gregory">Add SystemProperties.getLong(Class, String, long).</action>
<!-- UPDATE -->
<action type="update" dev="ggregory" due-to="Gary Gregory">[test] Bump org.apache.commons:commons-text from 1.13.1 to 1.14.0.</action>
<action type="update" dev="ggregory" due-to="Gary Gregory">Bump org.apache.commons:commons-parent from 85 to 87.</action>
diff --git a/src/main/java/org/apache/commons/lang3/ClassUtils.java b/src/main/java/org/apache/commons/lang3/ClassUtils.java
index d2a36f276..2e811b4f4 100644
--- a/src/main/java/org/apache/commons/lang3/ClassUtils.java
+++ b/src/main/java/org/apache/commons/lang3/ClassUtils.java
@@ -627,7 +627,11 @@ public static String getName(final Class<?> cls) {
* @see Class#getName()
*/
public static String getName(final Class<?> cls, final String valueIfNull) {
- return cls == null ? valueIfNull : cls.getName();
+ return getName(cls, valueIfNull, false);
+ }
+
+ static String getName(final Class<?> cls, final String valueIfNull, final boolean simple) {
+ return cls == null ? valueIfNull : simple ? cls.getSimpleName() : cls.getName();
}
/**
diff --git a/src/main/java/org/apache/commons/lang3/SystemProperties.java b/src/main/java/org/apache/commons/lang3/SystemProperties.java
index dc9b12139..64dfa266e 100644
--- a/src/main/java/org/apache/commons/lang3/SystemProperties.java
+++ b/src/main/java/org/apache/commons/lang3/SystemProperties.java
@@ -1551,12 +1551,32 @@ public static String getAwtToolkit() {
return getProperty(AWT_TOOLKIT);
}
+ /**
+ * Gets the current value for the property named {@code "SimpleClassName.Key"} as a {@code boolean}.
+ * <p>
+ * If a {@link SecurityException} is caught, the return value is {@code null}.
+ * </p>
+ *
+ * @param clazz The Class to use for the SimpleClassName.
+ * @param key The subkey.
+ * @param defaultIfAbsent The default value.
+ * @return an int or defaultIfAbsent.
+ * @see Class#getSimpleName()
+ * @since 3.19.0
+ */
+ public static boolean getBoolean(final Class<?> clazz, final String key, final BooleanSupplier defaultIfAbsent) {
+ return getBoolean(toKey(clazz, key, true), defaultIfAbsent);
+ }
+
/**
* Gets the current value for the property named {@code key} as an {@code boolean}.
+ * <p>
+ * If a {@link SecurityException} is caught, the return value is {@code null}.
+ * </p>
*
- * @param key The key
- * @param defaultIfAbsent The default value
- * @return an {@code boolean} or defaultIfAbsent
+ * @param key The key.
+ * @param defaultIfAbsent The default value.
+ * @return an {@code boolean} or defaultIfAbsent.
*/
public static boolean getBoolean(final String key, final BooleanSupplier defaultIfAbsent) {
final String str = getProperty(key);
@@ -1863,12 +1883,32 @@ public static String getHttpsProxyPort() {
return getProperty(HTTPS_PROXY_PORT);
}
+ /**
+ * Gets the current value for the property named {@code "SimpleClassName.Key"} as an {@code int}.
+ * <p>
+ * If a {@link SecurityException} is caught, the return value is {@code null}.
+ * </p>
+ *
+ * @param clazz The Class to use for the SimpleClassName.
+ * @param key The subkey.
+ * @param defaultIfAbsent The default value.
+ * @return an int or defaultIfAbsent.
+ * @see Class#getSimpleName()
+ * @since 3.19.0
+ */
+ public static int getInt(final Class<?> clazz, final String key, final IntSupplier defaultIfAbsent) {
+ return getInt(toKey(clazz, key, true), defaultIfAbsent);
+ }
+
/**
* Gets the current value for the property named {@code key} as an {@code int}.
+ * <p>
+ * If a {@link SecurityException} is caught, the return value is {@code null}.
+ * </p>
*
- * @param key The key
- * @param defaultIfAbsent The default value
- * @return an {@code int} or defaultIfAbsent
+ * @param key The key.
+ * @param defaultIfAbsent The default value.
+ * @return an {@code int} or defaultIfAbsent.
*/
public static int getInt(final String key, final IntSupplier defaultIfAbsent) {
final String str = getProperty(key);
@@ -3579,12 +3619,32 @@ public static String getLineSeparator(final Supplier<String> defaultIfAbsent) {
return getProperty(LINE_SEPARATOR, defaultIfAbsent);
}
+ /**
+ * Gets the current value for the property named {@code "SimpleClassName.Key"} as a {@code long}.
+ * <p>
+ * If a {@link SecurityException} is caught, the return value is {@code null}.
+ * </p>
+ *
+ * @param clazz The Class to use for the SimpleClassName.
+ * @param key The subkey.
+ * @param defaultIfAbsent The default value.
+ * @return a long or defaultIfAbsent.
+ * @see Class#getSimpleName()
+ * @since 3.19.0
+ */
+ public static long getLong(final Class<?> clazz, final String key, final LongSupplier defaultIfAbsent) {
+ return getLong(toKey(clazz, key, true), defaultIfAbsent);
+ }
+
/**
* Gets the current value for the property named {@code key} as a {@code long}.
+ * <p>
+ * If a {@link SecurityException} is caught, the return value is {@code null}.
+ * </p>
*
- * @param key The key
- * @param defaultIfAbsent The default value
- * @return a {@code long} or defaultIfAbsent
+ * @param key The key.
+ * @param defaultIfAbsent The default value.
+ * @return a {@code long} or defaultIfAbsent.
*/
public static long getLong(final String key, final LongSupplier defaultIfAbsent) {
final String str = getProperty(key);
@@ -4093,6 +4153,10 @@ public static boolean isPropertySet(final String property) {
return getProperty(property) != null;
}
+ private static String toKey(final Class<?> clazz, final String key, final boolean simpleKey) {
+ return ClassUtils.getName(clazz, StringUtils.EMPTY, simpleKey) + "." + key;
+ }
+
/**
* Make private in 4.0.
*
diff --git a/src/test/java/org/apache/commons/lang3/SystemPropertiesTest.java b/src/test/java/org/apache/commons/lang3/SystemPropertiesTest.java
index 83dbb626e..bb526651c 100644
--- a/src/test/java/org/apache/commons/lang3/SystemPropertiesTest.java
+++ b/src/test/java/org/apache/commons/lang3/SystemPropertiesTest.java
@@ -39,6 +39,7 @@
@SetSystemProperty(key = SystemPropertiesTest.KEY_TAB_1, value = "value2") })
class SystemPropertiesTest {
+ private static final String SIMPLE_NAME = SystemPropertiesTest.class.getSimpleName();
static final String KEY_SPACE_1 = " ";
static final String KEY_TAB_1 = "\t";
@@ -278,6 +279,24 @@ void testGetBoolean() {
}
}
+ @Test
+ void testGetBooleanClass() {
+ final String key = RandomStringUtils.insecure().next(10);
+ final String absentKey = RandomStringUtils.insecure().next(10);
+ final String keyFull = SIMPLE_NAME + "." + key;
+ final String absentKeyFull = SIMPLE_NAME + "." + absentKey;
+ assertNull(System.getProperty(absentKeyFull));
+ try {
+ System.setProperty(keyFull, Boolean.TRUE.toString());
+ assertTrue(SystemProperties.getBoolean(SystemPropertiesTest.class, key, () -> false));
+ assertTrue(SystemProperties.getBoolean(SystemPropertiesTest.class, absentKey, () -> true));
+ assertFalse(SystemProperties.getBoolean(SystemPropertiesTest.class, absentKey, () -> false));
+ assertTrue(SystemProperties.getBoolean(SystemPropertiesTest.class, absentKey, () -> true));
+ } finally {
+ System.clearProperty(keyFull);
+ }
+ }
+
@Test
void testGetDoesNotThrow() {
assertDoesNotThrow(SystemProperties::getAppleAwtEnableTemplateImages);
@@ -497,6 +516,24 @@ void testGetInt() {
}
}
+ @Test
+ void testGetIntClass() {
+ final String key = RandomStringUtils.insecure().next(10);
+ final String absentKey = RandomStringUtils.insecure().next(10);
+ final String keyFull = SIMPLE_NAME + "." + key;
+ final String absentKeyFull = SIMPLE_NAME + "." + absentKey;
+ assertNull(System.getProperty(absentKeyFull));
+ try {
+ System.setProperty(keyFull, Long.toString(Integer.MAX_VALUE));
+ assertEquals(Integer.MAX_VALUE, SystemProperties.getInt(SystemPropertiesTest.class, key, () -> 0));
+ assertEquals(Integer.MAX_VALUE, SystemProperties.getInt(SystemPropertiesTest.class, absentKey, () -> Integer.MAX_VALUE));
+ assertEquals(0, SystemProperties.getInt(SystemPropertiesTest.class, absentKey, () -> 0));
+ assertEquals(1, SystemProperties.getInt(SystemPropertiesTest.class, absentKey, () -> 1));
+ } finally {
+ System.clearProperty(keyFull);
+ }
+ }
+
@Test
void testGetJavaAwtFonts() {
assertNull(SystemProperties.getJavaAwtFonts());
@@ -688,6 +725,24 @@ void testGetLong() {
}
}
+ @Test
+ void testGetLongClass() {
+ final String key = RandomStringUtils.insecure().next(10);
+ final String absentKey = RandomStringUtils.insecure().next(10);
+ final String keyFull = SIMPLE_NAME + "." + key;
+ final String absentKeyFull = SIMPLE_NAME + "." + absentKey;
+ assertNull(System.getProperty(absentKeyFull));
+ try {
+ System.setProperty(keyFull, Long.toString(Long.MAX_VALUE));
+ assertEquals(Long.MAX_VALUE, SystemProperties.getLong(SystemPropertiesTest.class, key, () -> 0));
+ assertEquals(Long.MAX_VALUE, SystemProperties.getLong(SystemPropertiesTest.class, absentKey, () -> Long.MAX_VALUE));
+ assertEquals(0, SystemProperties.getLong(SystemPropertiesTest.class, absentKey, () -> 0));
+ assertEquals(1, SystemProperties.getLong(SystemPropertiesTest.class, absentKey, () -> 1));
+ } finally {
+ System.clearProperty(keyFull);
+ }
+ }
+
@Test
void testGetOsArch() {
assertNotNull(SystemProperties.getOsArch());