Better common primitives as getters
This commit is contained in:
parent
6cca6ff4a5
commit
7d7e8af450
|
@ -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);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue