Add and use Suppliers.
This commit is contained in:
parent
320486fe17
commit
4e18561e49
|
@ -53,6 +53,7 @@ The <action> type attribute can be add,update,fix,remove.
|
|||
<action type="add" dev="ggregory" due-to="Gary Gregory">Add EnumUtils.getEnumSystemProperty(...).</action>
|
||||
<action type="add" dev="ggregory" due-to="Gary Gregory">Add TriConsumer.</action>
|
||||
<action type="add" dev="ggregory" due-to="Gary Gregory">Add and use EnumUtils.getFirstEnumIgnoreCase(Class, String, Function, E).</action>
|
||||
<action type="add" dev="ggregory" due-to="Gary Gregory">Add and use Suppliers.</action>
|
||||
<!-- UPDATE -->
|
||||
<action type="add" dev="ggregory" due-to="Dependabot">Bump spotbugs-maven-plugin from 4.2.0 to 4.2.2 #735.</action>
|
||||
</release>
|
||||
|
|
|
@ -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> T getIfNull(final T object, final Supplier<T> 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<String> supplier) {
|
||||
return obj == null ? supplier == null ? null : supplier.get() : obj.toString();
|
||||
return obj == null ? Suppliers.get(supplier) : obj.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -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 extends CharSequence> T getIfBlank(final T str, final Supplier<T> 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 extends CharSequence> T getIfEmpty(final T str, final Supplier<T> defaultSupplier) {
|
||||
return isEmpty(str) ? defaultSupplier == null ? null : defaultSupplier.get() : str;
|
||||
return isEmpty(str) ? Suppliers.get(defaultSupplier) : str;
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -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 <T> 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> T get(final Supplier<T> supplier) {
|
||||
return supplier == null ? null : supplier.get();
|
||||
}
|
||||
|
||||
}
|
|
@ -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"));
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue