|
27 | 27 | import java.util.HashMap; |
28 | 28 | import java.util.Map; |
29 | 29 | import java.util.TreeSet; |
| 30 | +import java.util.function.Consumer; |
30 | 31 | import java.util.function.Supplier; |
31 | 32 |
|
32 | 33 | import org.apache.commons.lang3.exception.CloneFailedException; |
@@ -226,6 +227,62 @@ public static boolean anyNotNull(final Object... values) { |
226 | 227 | return firstNonNull(values) != null; |
227 | 228 | } |
228 | 229 |
|
| 230 | + /** |
| 231 | + * <p> |
| 232 | + * Invokes the given {@code consumer's} {@link Consumer#accept(Object)} with the first {@code non-null} value from |
| 233 | + * {@code objects}. If all the values are null, the consumer is not invoked. |
| 234 | + * </p> |
| 235 | + * |
| 236 | + * <p> |
| 237 | + * The caller is responsible for thread-safety and exception handling of consumer. |
| 238 | + * </p> |
| 239 | + * |
| 240 | + * <pre> |
| 241 | + * ObjectUtils.applyIfNonNull(bean::setValue, null) - setValue not invoked |
| 242 | + * ObjectUtils.applyIfNonNull(bean::setValue, null, "abc", "def") - setValue invoked with "abc" |
| 243 | + * ObjectUtils.applyIfNonNull(v -> bean.setValue(v), "abc") - setValue invoked with "abc" |
| 244 | + * </pre> |
| 245 | + * |
| 246 | + * @param <T> the type of the object |
| 247 | + * @param objects the values to test, may be {@code null} or empty |
| 248 | + * @param consumer the consumer operation to invoke with the first non-null {@code objects}. |
| 249 | + * @see #firstNonNull(Object...) |
| 250 | + * @see #applyIfNonNull(Consumer, Object) |
| 251 | + * @since 3.12 |
| 252 | + */ |
| 253 | + @SafeVarargs |
| 254 | + public static <T> void applyFirstNonNull(final Consumer<T> consumer, final T... objects) { |
| 255 | + applyIfNonNull(consumer, firstNonNull(objects)); |
| 256 | + } |
| 257 | + |
| 258 | + /** |
| 259 | + * <p> |
| 260 | + * Invokes the given {@code consumer's} {@link Consumer#accept(Object)} with the {@code object} if it is |
| 261 | + * {@code non-null}, otherwise the consumer is not invoked. |
| 262 | + * </p> |
| 263 | + * |
| 264 | + * <p> |
| 265 | + * The caller is responsible for thread-safety and exception handling of consumer. |
| 266 | + * </p> |
| 267 | + * |
| 268 | + * <pre> |
| 269 | + * ObjectUtils.applyIfNonNull(bean::setValue, null) - setValue not invoked |
| 270 | + * ObjectUtils.applyIfNonNull(bean::setValue, "abc") - setValue invoked with "abc" |
| 271 | + * ObjectUtils.applyIfNonNull(v -> bean.setValue(v), "abc") - setValue invoked with "abc" |
| 272 | + * </pre> |
| 273 | + * |
| 274 | + * @param <T> the type of the object |
| 275 | + * @param object the {@code Object} to test, may be {@code null} |
| 276 | + * @param consumer the consumer operation to invoke with {@code object} if it is {@code non-null} |
| 277 | + * @see #applyFirstNonNull(Consumer, Object...) |
| 278 | + * @since 3.12 |
| 279 | + */ |
| 280 | + public static <T> void applyIfNonNull(final Consumer<T> consumer, final T object) { |
| 281 | + if (object != null) { |
| 282 | + consumer.accept(object); |
| 283 | + } |
| 284 | + } |
| 285 | + |
229 | 286 | // cloning |
230 | 287 | //----------------------------------------------------------------------- |
231 | 288 | /** |
|
0 commit comments