1 /* 2 * Licensed to the Apache Software Foundation (ASF) under one or more 3 * contributor license agreements. See the NOTICE file distributed with 4 * this work for additional information regarding copyright ownership. 5 * The ASF licenses this file to You under the Apache License, Version 2.0 6 * (the "License"); you may not use this file except in compliance with 7 * the License. You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 18 package org.apache.commons.lang3.function; 19 20 import java.util.function.Supplier; 21 22 /** 23 * Helps use {@link Supplier}. 24 * 25 * @since 3.13.0 26 */ 27 public class Suppliers { 28 29 /** 30 * Returns the singleton supplier that always returns null. 31 * <p> 32 * This supplier never throws an exception. 33 * </p> 34 */ 35 @SuppressWarnings("rawtypes") 36 private static Supplier NUL = () -> null; 37 38 /** 39 * Null-safe call to {@link Supplier#get()}. 40 * 41 * @param <T> the type of results supplied by this supplier. 42 * @param supplier the supplier or null. 43 * @return Result of {@link Supplier#get()} or null. 44 */ 45 public static <T> T get(final Supplier<T> supplier) { 46 return supplier == null ? null : supplier.get(); 47 } 48 49 /** 50 * Returns the singleton supplier that always returns null. 51 * <p> 52 * This supplier never throws an exception. 53 * </p> 54 * 55 * @param <T> Supplied type. 56 * @return The NUL singleton. 57 * @since 3.14.0 58 */ 59 @SuppressWarnings("unchecked") 60 public static <T> Supplier<T> nul() { 61 return NUL; 62 } 63 64 /** 65 * Make private in 4.0. 66 * 67 * @deprecated TODO Make private in 4.0. 68 */ 69 @Deprecated 70 public Suppliers() { 71 // empty 72 } 73 }