Util { public static Object emptyValueOf(Type type) { return EMPTIES.getOrDefault(Types.getRawType(type), () -> null).get(); } private  Util(); static void checkArgument(boolean expression,
                                   String errorMessageTemplate,
                                   Object... errorMessageArgs); static T checkNotNull(T reference,
                                   String errorMessageTemplate,
                                   Object... errorMessageArgs); static void checkState(boolean expression,
                                String errorMessageTemplate,
                                Object... errorMessageArgs); static boolean isDefault(Method method); static String emptyToNull(String string); @SuppressWarnings("unchecked") static T[] removeValues(T[] values, Predicate<T> shouldRemove, Class<T> type); @SuppressWarnings("unchecked") static T[] toArray(Iterable<? extends T> iterable, Class<T> type); static Collection<T> valuesOrEmpty(Map<String, Collection<T>> map, String key); static void ensureClosed(Closeable closeable); static Type resolveLastTypeParameter(Type genericContext, Class<?> supertype); static Object emptyValueOf(Type type); static String toString(Reader reader); static byte[] toByteArray(InputStream in); static String decodeOrDefault(byte[] data, Charset charset, String defaultValue); static boolean isNotBlank(String value); static boolean isBlank(String value); static final String CONTENT_LENGTH; static final String CONTENT_ENCODING; static final String RETRY_AFTER; static final String ENCODING_GZIP; static final String ENCODING_DEFLATE; static final Charset UTF_8; static final Charset ISO_8859_1; static final Type MAP_STRING_WILDCARD; }
@Test public void emptyValueOf() throws Exception { assertEquals(false, Util.emptyValueOf(boolean.class)); assertEquals(false, Util.emptyValueOf(Boolean.class)); assertThat((byte[]) Util.emptyValueOf(byte[].class)).isEmpty(); assertEquals(Collections.emptyList(), Util.emptyValueOf(Collection.class)); assertThat((Iterator<?>) Util.emptyValueOf(Iterator.class)).isEmpty(); assertEquals(Collections.emptyList(), Util.emptyValueOf(List.class)); assertEquals(Collections.emptyMap(), Util.emptyValueOf(Map.class)); assertEquals(Collections.emptySet(), Util.emptyValueOf(Set.class)); assertEquals(Optional.empty(), Util.emptyValueOf(Optional.class)); }
@Test public void emptyValueOf_considersRawType() throws Exception { Type listStringType = LastTypeParameter.class.getDeclaredField("LIST_STRING").getGenericType(); assertThat((List<?>) Util.emptyValueOf(listStringType)).isEmpty(); }
@Test public void emptyValueOf_nullForUndefined() throws Exception { assertThat(Util.emptyValueOf(Number.class)).isNull(); assertThat(Util.emptyValueOf(Parameterized.class)).isNull(); }