001// Generated by delombok at Sat Feb 25 17:58:34 UTC 2023 002package io.cui.tools.lang; 003 004import static io.cui.tools.base.Preconditions.checkArgument; 005import static io.cui.tools.collect.MoreCollections.isEmpty; 006import java.util.function.Supplier; 007 008/** 009 * Provides some utilities in the context of {@link Object} 010 * 011 * @author Oliver Wolff 012 */ 013public final class MoreObjects { 014 // owolff: It is actually checked before. 015 /** 016 * Checks and returns the given Object if it is assignable to the given targetType. 017 * Otherwise, it throws an {@link IllegalArgumentException}. This will be thrown also if 018 * one of the parameters is {@code null}. 019 * 020 * @param <T> defining the type to be returned. 021 * 022 * @param underCheck KeyStoreType to be checked / cast. If it is null or is not assignable to 023 * expectedType an {@link IllegalArgumentException} will be thrown. 024 * @param expectedType checks the type . If it is null an {@link IllegalArgumentException} will 025 * be thrown 026 * @return the cast Objected of type T if applicable. 027 * @throws IllegalArgumentException if the given type is either null or not the expected type 028 */ 029 @SuppressWarnings("unchecked") 030 public static <T> T requireType(final Object underCheck, Class<T> expectedType) { 031 checkArgument(null != underCheck, "Object to be checked must not be null"); 032 checkArgument(null != expectedType, "expectedType must not be null"); 033 checkArgument(expectedType.isAssignableFrom(underCheck.getClass()), "KeyStoreType to be checked \'%s\' is not assignable to \'%s\'", underCheck.getClass(), expectedType.getName()); 034 return (T) underCheck; 035 } 036 037 /** 038 * Simple helper checking whether a number of given Objects are not {@code null} 039 * 040 * @param objects 041 * @return {@code true} if there is no {@code null} value given, {@code false} if at least one 042 * null value is given. An empty varags parameter therefore results in {@code true} (no 043 * null object found) 044 */ 045 public static boolean allNonNull(Object... objects) { 046 if (isEmpty(objects)) { 047 return true; 048 } 049 for (Object object : objects) { 050 if (null == object) { 051 return false; 052 } 053 } 054 return true; 055 } 056 057 /** 058 * Simple helper checking whether a number of given Objects are {@code null} 059 * 060 * @param objects 061 * @return {@code true} if there is no non-{@code null} value given, {@code false} if at least 062 * one non-null value is given. An empty varags parameter therefore results in 063 * {@code true} (no non-null object found) 064 */ 065 public static boolean allNull(Object... objects) { 066 if (isEmpty(objects)) { 067 return true; 068 } 069 for (Object object : objects) { 070 if (null != object) { 071 return false; 072 } 073 } 074 return true; 075 } 076 077 /** 078 * <p> 079 * Returns the first value in the array which is not {@code null}. 080 * If all the values are {@code null} or the array is {@code null} 081 * or empty then {@code null} is returned. 082 * </p> 083 * 084 * <pre> 085 * MoreObjects.firstNonNull(null, null) = null 086 * MoreObjects.firstNonNull(null, "") = "" 087 * MoreObjects.firstNonNull(null, null, "") = "" 088 * MoreObjects.firstNonNull(null, "zz") = "zz" 089 * MoreObjects.firstNonNull("abc", *) = "abc" 090 * MoreObjects.firstNonNull(null, "xyz", *) = "xyz" 091 * MoreObjects.firstNonNull(Boolean.TRUE, *) = Boolean.TRUE 092 * MoreObjects.firstNonNull() = null 093 * </pre> 094 * 095 * @param <T> the component type of the array 096 * @param values the values to test, may be {@code null} or empty 097 * 098 * @return the first value from {@code values} which is not {@code null}, 099 * or {@code null} if there are no non-null values 100 */ 101 @SafeVarargs 102 public static <T> T firstNonNull(final T... values) { 103 if (values != null) { 104 for (final T val : values) { 105 if (val != null) { 106 return val; 107 } 108 } 109 } 110 return null; 111 } 112 113 /** 114 * <p> 115 * Executes the given suppliers in order and returns the first return 116 * value where a value other than {@code null} is returned. 117 * Once a non-{@code null} value is obtained, all following suppliers are 118 * not executed anymore. 119 * If all the return values are {@code null} or no suppliers are provided 120 * then {@code null} is returned. 121 * </p> 122 * 123 * <pre> 124 * MoreObjects.getFirstNonNull(null, () -> null) = null 125 * MoreObjects.getFirstNonNull(() -> null, () -> "") = "" 126 * MoreObjects.getFirstNonNull(() -> "", () -> throw new IllegalStateException()) = "" 127 * MoreObjects.getFirstNonNull(() -> null, () -> "zz) = "zz" 128 * MoreObjects.getFirstNonNull() = null 129 * </pre> 130 * 131 * @param <T> the type of the return values 132 * @param suppliers the suppliers returning the values to test. 133 * {@code null} values are ignored. 134 * Suppliers may return {@code null} or a value of type @{code T} 135 * 136 * @return the first return value from {@code suppliers} which is not {@code null}, 137 * or {@code null} if there are no non-null values 138 */ 139 @SafeVarargs 140 public static <T> T getFirstNonNull(final Supplier<T>... suppliers) { 141 if (suppliers != null) { 142 for (final Supplier<T> supplier : suppliers) { 143 if (supplier != null) { 144 final var value = supplier.get(); 145 if (value != null) { 146 return value; 147 } 148 } 149 } 150 } 151 return null; 152 } 153 154 @java.lang.SuppressWarnings("all") 155 @lombok.Generated 156 private MoreObjects() { 157 throw new java.lang.UnsupportedOperationException("This is a utility class and cannot be instantiated"); 158 } 159}