Skip to content

Commit 8b27dec

Browse files
committed
Add SystemProperties.getBoolean(Class, String, BooleanSupplier)
- Add SystemProperties.getInt(Class, String, IntSupplier) - Add SystemProperties.getLong(Class, String, LongSupplier)
1 parent 9cbf068 commit 8b27dec

File tree

4 files changed

+136
-10
lines changed

4 files changed

+136
-10
lines changed

src/changes/changes.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,9 @@ The <action> type attribute can be add,update,fix,remove.
9696
<action type="add" dev="ggregory" due-to="Gary Gregory">Add DurationUtils.get(String, TemporalUnit, long).</action>
9797
<action type="add" dev="ggregory" due-to="Gary Gregory">Add DurationUtils.getMillis(String, long).</action>
9898
<action type="add" dev="ggregory" due-to="Gary Gregory">Add DurationUtils.getSeconds(String, long).</action>
99+
<action type="add" dev="ggregory" due-to="Gary Gregory">Add SystemProperties.getBoolean(Class, String, boolean).</action>
100+
<action type="add" dev="ggregory" due-to="Gary Gregory">Add SystemProperties.getInt(Class, String, int).</action>
101+
<action type="add" dev="ggregory" due-to="Gary Gregory">Add SystemProperties.getLong(Class, String, long).</action>
99102
<!-- UPDATE -->
100103
<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>
101104
<action type="update" dev="ggregory" due-to="Gary Gregory">Bump org.apache.commons:commons-parent from 85 to 87.</action>

src/main/java/org/apache/commons/lang3/ClassUtils.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -627,7 +627,11 @@ public static String getName(final Class<?> cls) {
627627
* @see Class#getName()
628628
*/
629629
public static String getName(final Class<?> cls, final String valueIfNull) {
630-
return cls == null ? valueIfNull : cls.getName();
630+
return getName(cls, valueIfNull, false);
631+
}
632+
633+
static String getName(final Class<?> cls, final String valueIfNull, final boolean simple) {
634+
return cls == null ? valueIfNull : simple ? cls.getSimpleName() : cls.getName();
631635
}
632636

633637
/**

src/main/java/org/apache/commons/lang3/SystemProperties.java

Lines changed: 73 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1551,12 +1551,32 @@ public static String getAwtToolkit() {
15511551
return getProperty(AWT_TOOLKIT);
15521552
}
15531553

1554+
/**
1555+
* Gets the current value for the property named {@code "SimpleClassName.Key"} as a {@code boolean}.
1556+
* <p>
1557+
* If a {@link SecurityException} is caught, the return value is {@code null}.
1558+
* </p>
1559+
*
1560+
* @param clazz The Class to use for the SimpleClassName.
1561+
* @param key The subkey.
1562+
* @param defaultIfAbsent The default value.
1563+
* @return an int or defaultIfAbsent.
1564+
* @see Class#getSimpleName()
1565+
* @since 3.19.0
1566+
*/
1567+
public static boolean getBoolean(final Class<?> clazz, final String key, final BooleanSupplier defaultIfAbsent) {
1568+
return getBoolean(toKey(clazz, key, true), defaultIfAbsent);
1569+
}
1570+
15541571
/**
15551572
* Gets the current value for the property named {@code key} as an {@code boolean}.
1573+
* <p>
1574+
* If a {@link SecurityException} is caught, the return value is {@code null}.
1575+
* </p>
15561576
*
1557-
* @param key The key
1558-
* @param defaultIfAbsent The default value
1559-
* @return an {@code boolean} or defaultIfAbsent
1577+
* @param key The key.
1578+
* @param defaultIfAbsent The default value.
1579+
* @return an {@code boolean} or defaultIfAbsent.
15601580
*/
15611581
public static boolean getBoolean(final String key, final BooleanSupplier defaultIfAbsent) {
15621582
final String str = getProperty(key);
@@ -1863,12 +1883,32 @@ public static String getHttpsProxyPort() {
18631883
return getProperty(HTTPS_PROXY_PORT);
18641884
}
18651885

1886+
/**
1887+
* Gets the current value for the property named {@code "SimpleClassName.Key"} as an {@code int}.
1888+
* <p>
1889+
* If a {@link SecurityException} is caught, the return value is {@code null}.
1890+
* </p>
1891+
*
1892+
* @param clazz The Class to use for the SimpleClassName.
1893+
* @param key The subkey.
1894+
* @param defaultIfAbsent The default value.
1895+
* @return an int or defaultIfAbsent.
1896+
* @see Class#getSimpleName()
1897+
* @since 3.19.0
1898+
*/
1899+
public static int getInt(final Class<?> clazz, final String key, final IntSupplier defaultIfAbsent) {
1900+
return getInt(toKey(clazz, key, true), defaultIfAbsent);
1901+
}
1902+
18661903
/**
18671904
* Gets the current value for the property named {@code key} as an {@code int}.
1905+
* <p>
1906+
* If a {@link SecurityException} is caught, the return value is {@code null}.
1907+
* </p>
18681908
*
1869-
* @param key The key
1870-
* @param defaultIfAbsent The default value
1871-
* @return an {@code int} or defaultIfAbsent
1909+
* @param key The key.
1910+
* @param defaultIfAbsent The default value.
1911+
* @return an {@code int} or defaultIfAbsent.
18721912
*/
18731913
public static int getInt(final String key, final IntSupplier defaultIfAbsent) {
18741914
final String str = getProperty(key);
@@ -3579,12 +3619,32 @@ public static String getLineSeparator(final Supplier<String> defaultIfAbsent) {
35793619
return getProperty(LINE_SEPARATOR, defaultIfAbsent);
35803620
}
35813621

3622+
/**
3623+
* Gets the current value for the property named {@code "SimpleClassName.Key"} as a {@code long}.
3624+
* <p>
3625+
* If a {@link SecurityException} is caught, the return value is {@code null}.
3626+
* </p>
3627+
*
3628+
* @param clazz The Class to use for the SimpleClassName.
3629+
* @param key The subkey.
3630+
* @param defaultIfAbsent The default value.
3631+
* @return a long or defaultIfAbsent.
3632+
* @see Class#getSimpleName()
3633+
* @since 3.19.0
3634+
*/
3635+
public static long getLong(final Class<?> clazz, final String key, final LongSupplier defaultIfAbsent) {
3636+
return getLong(toKey(clazz, key, true), defaultIfAbsent);
3637+
}
3638+
35823639
/**
35833640
* Gets the current value for the property named {@code key} as a {@code long}.
3641+
* <p>
3642+
* If a {@link SecurityException} is caught, the return value is {@code null}.
3643+
* </p>
35843644
*
3585-
* @param key The key
3586-
* @param defaultIfAbsent The default value
3587-
* @return a {@code long} or defaultIfAbsent
3645+
* @param key The key.
3646+
* @param defaultIfAbsent The default value.
3647+
* @return a {@code long} or defaultIfAbsent.
35883648
*/
35893649
public static long getLong(final String key, final LongSupplier defaultIfAbsent) {
35903650
final String str = getProperty(key);
@@ -4093,6 +4153,10 @@ public static boolean isPropertySet(final String property) {
40934153
return getProperty(property) != null;
40944154
}
40954155

4156+
private static String toKey(final Class<?> clazz, final String key, final boolean simpleKey) {
4157+
return ClassUtils.getName(clazz, StringUtils.EMPTY, simpleKey) + "." + key;
4158+
}
4159+
40964160
/**
40974161
* Make private in 4.0.
40984162
*

src/test/java/org/apache/commons/lang3/SystemPropertiesTest.java

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
@SetSystemProperty(key = SystemPropertiesTest.KEY_TAB_1, value = "value2") })
4040
class SystemPropertiesTest {
4141

42+
private static final String SIMPLE_NAME = SystemPropertiesTest.class.getSimpleName();
4243
static final String KEY_SPACE_1 = " ";
4344
static final String KEY_TAB_1 = "\t";
4445

@@ -278,6 +279,24 @@ void testGetBoolean() {
278279
}
279280
}
280281

282+
@Test
283+
void testGetBooleanClass() {
284+
final String key = RandomStringUtils.insecure().next(10);
285+
final String absentKey = RandomStringUtils.insecure().next(10);
286+
final String keyFull = SIMPLE_NAME + "." + key;
287+
final String absentKeyFull = SIMPLE_NAME + "." + absentKey;
288+
assertNull(System.getProperty(absentKeyFull));
289+
try {
290+
System.setProperty(keyFull, Boolean.TRUE.toString());
291+
assertTrue(SystemProperties.getBoolean(SystemPropertiesTest.class, key, () -> false));
292+
assertTrue(SystemProperties.getBoolean(SystemPropertiesTest.class, absentKey, () -> true));
293+
assertFalse(SystemProperties.getBoolean(SystemPropertiesTest.class, absentKey, () -> false));
294+
assertTrue(SystemProperties.getBoolean(SystemPropertiesTest.class, absentKey, () -> true));
295+
} finally {
296+
System.clearProperty(keyFull);
297+
}
298+
}
299+
281300
@Test
282301
void testGetDoesNotThrow() {
283302
assertDoesNotThrow(SystemProperties::getAppleAwtEnableTemplateImages);
@@ -497,6 +516,24 @@ void testGetInt() {
497516
}
498517
}
499518

519+
@Test
520+
void testGetIntClass() {
521+
final String key = RandomStringUtils.insecure().next(10);
522+
final String absentKey = RandomStringUtils.insecure().next(10);
523+
final String keyFull = SIMPLE_NAME + "." + key;
524+
final String absentKeyFull = SIMPLE_NAME + "." + absentKey;
525+
assertNull(System.getProperty(absentKeyFull));
526+
try {
527+
System.setProperty(keyFull, Long.toString(Integer.MAX_VALUE));
528+
assertEquals(Integer.MAX_VALUE, SystemProperties.getInt(SystemPropertiesTest.class, key, () -> 0));
529+
assertEquals(Integer.MAX_VALUE, SystemProperties.getInt(SystemPropertiesTest.class, absentKey, () -> Integer.MAX_VALUE));
530+
assertEquals(0, SystemProperties.getInt(SystemPropertiesTest.class, absentKey, () -> 0));
531+
assertEquals(1, SystemProperties.getInt(SystemPropertiesTest.class, absentKey, () -> 1));
532+
} finally {
533+
System.clearProperty(keyFull);
534+
}
535+
}
536+
500537
@Test
501538
void testGetJavaAwtFonts() {
502539
assertNull(SystemProperties.getJavaAwtFonts());
@@ -688,6 +725,24 @@ void testGetLong() {
688725
}
689726
}
690727

728+
@Test
729+
void testGetLongClass() {
730+
final String key = RandomStringUtils.insecure().next(10);
731+
final String absentKey = RandomStringUtils.insecure().next(10);
732+
final String keyFull = SIMPLE_NAME + "." + key;
733+
final String absentKeyFull = SIMPLE_NAME + "." + absentKey;
734+
assertNull(System.getProperty(absentKeyFull));
735+
try {
736+
System.setProperty(keyFull, Long.toString(Long.MAX_VALUE));
737+
assertEquals(Long.MAX_VALUE, SystemProperties.getLong(SystemPropertiesTest.class, key, () -> 0));
738+
assertEquals(Long.MAX_VALUE, SystemProperties.getLong(SystemPropertiesTest.class, absentKey, () -> Long.MAX_VALUE));
739+
assertEquals(0, SystemProperties.getLong(SystemPropertiesTest.class, absentKey, () -> 0));
740+
assertEquals(1, SystemProperties.getLong(SystemPropertiesTest.class, absentKey, () -> 1));
741+
} finally {
742+
System.clearProperty(keyFull);
743+
}
744+
}
745+
691746
@Test
692747
void testGetOsArch() {
693748
assertNotNull(SystemProperties.getOsArch());

0 commit comments

Comments
 (0)