diff --git a/src/changes/changes.xml b/src/changes/changes.xml index a7fbe838f..b6ec5baaf 100644 --- a/src/changes/changes.xml +++ b/src/changes/changes.xml @@ -53,6 +53,7 @@ The type attribute can be add,update,fix,remove. Add EnumUtils.getEnumSystemProperty(...). Add TriConsumer. Add and use EnumUtils.getFirstEnumIgnoreCase(Class, String, Function, E). + Add and use Suppliers. Bump spotbugs-maven-plugin from 4.2.0 to 4.2.2 #735. diff --git a/src/main/java/org/apache/commons/lang3/ObjectUtils.java b/src/main/java/org/apache/commons/lang3/ObjectUtils.java index 7eb227fb3..4bfac7e40 100644 --- a/src/main/java/org/apache/commons/lang3/ObjectUtils.java +++ b/src/main/java/org/apache/commons/lang3/ObjectUtils.java @@ -32,6 +32,7 @@ import java.util.TreeSet; import java.util.function.Supplier; import org.apache.commons.lang3.exception.CloneFailedException; +import org.apache.commons.lang3.function.Suppliers; import org.apache.commons.lang3.mutable.MutableInt; import org.apache.commons.lang3.text.StrBuilder; import org.apache.commons.lang3.time.DurationUtils; @@ -737,7 +738,7 @@ public class ObjectUtils { * @since 3.10 */ public static T getIfNull(final T object, final Supplier defaultSupplier) { - return object != null ? object : defaultSupplier == null ? null : defaultSupplier.get(); + return object != null ? object : Suppliers.get(defaultSupplier); } /** @@ -1318,7 +1319,7 @@ public class ObjectUtils { * @since 3.11 */ public static String toString(final Object obj, final Supplier supplier) { - return obj == null ? supplier == null ? null : supplier.get() : obj.toString(); + return obj == null ? Suppliers.get(supplier) : obj.toString(); } /** diff --git a/src/main/java/org/apache/commons/lang3/StringUtils.java b/src/main/java/org/apache/commons/lang3/StringUtils.java index d702e10f9..5f22048ba 100644 --- a/src/main/java/org/apache/commons/lang3/StringUtils.java +++ b/src/main/java/org/apache/commons/lang3/StringUtils.java @@ -31,6 +31,7 @@ import java.util.StringJoiner; import java.util.function.Supplier; import java.util.regex.Pattern; +import org.apache.commons.lang3.function.Suppliers; import org.apache.commons.lang3.function.ToBooleanBiFunction; /** @@ -2210,7 +2211,7 @@ public class StringUtils { * @since 3.10 */ public static T getIfBlank(final T str, final Supplier defaultSupplier) { - return isBlank(str) ? defaultSupplier == null ? null : defaultSupplier.get() : str; + return isBlank(str) ? Suppliers.get(defaultSupplier) : str; } /** @@ -2238,7 +2239,7 @@ public class StringUtils { * @since 3.10 */ public static T getIfEmpty(final T str, final Supplier defaultSupplier) { - return isEmpty(str) ? defaultSupplier == null ? null : defaultSupplier.get() : str; + return isEmpty(str) ? Suppliers.get(defaultSupplier) : str; } /** diff --git a/src/main/java/org/apache/commons/lang3/function/Suppliers.java b/src/main/java/org/apache/commons/lang3/function/Suppliers.java new file mode 100644 index 000000000..28d0013b5 --- /dev/null +++ b/src/main/java/org/apache/commons/lang3/function/Suppliers.java @@ -0,0 +1,40 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.commons.lang3.function; + +import java.util.function.Supplier; + +/** + * Helps use {@link Supplier}. + * + * @since 3.13.0 + */ +public class Suppliers { + + /** + * Null-safe call to {@link Supplier#get()}. + * + * @param the type of results supplied by this supplier. + * @param supplier the supplier or null. + * @return Result of {@link Supplier#get()} or null. + */ + public static T get(final Supplier supplier) { + return supplier == null ? null : supplier.get(); + } + +} diff --git a/src/test/java/org/apache/commons/lang3/function/SuppliersTest.java b/src/test/java/org/apache/commons/lang3/function/SuppliersTest.java new file mode 100644 index 000000000..d4e21098b --- /dev/null +++ b/src/test/java/org/apache/commons/lang3/function/SuppliersTest.java @@ -0,0 +1,41 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.commons.lang3.function; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNull; + +import java.util.function.Supplier; + +import org.junit.jupiter.api.Test; + +/** + * Tests {@link Suppliers}. + */ +public class SuppliersTest { + + /** + * Tests {@link Suppliers#get(Supplier)}. + */ + @Test + public void testGet() { + assertNull(Suppliers.get(null)); + assertNull(Suppliers.get(() -> null)); + assertEquals("foo", Suppliers.get(() -> "foo")); + } +}