/*
* Copyright 2015-2017 the original author or authors.
*
* All rights reserved. This program and the accompanying materials are
* made available under the terms of the Eclipse Public License v1.0 which
* accompanies this distribution and is available at
*
* http://www.eclipse.org/legal/epl-v10.html
*/
package org.junit.platform.commons.util;
import static java.util.Arrays.stream;
import static java.util.stream.Collectors.joining;
import static org.junit.platform.commons.meta.API.Usage.Internal;
import java.util.function.Function;
import org.junit.platform.commons.meta.API;
/**
* Collection of utilities for working with {@link Class classes}.
*
* <h3>DISCLAIMER</h3>
*
* <p>These utilities are intended solely for usage within the JUnit framework
* itself. <strong>Any usage by external parties is not supported.</strong>
* Use at your own risk!
*
* @since 1.0
*/
@API(Internal)
public final class ClassUtils {
///CLOVER:OFF
private ClassUtils() {
/* no-op */
}
///CLOVER:ON
/**
* Generate a comma-separated list of fully qualified class names for the
* supplied classes.
*
* @param classes the classes whose names should be included in the
* generated string
* @return a comma-separated list of fully qualified class names, or an empty
* string if the supplied class array is {@code null} or empty
* @see #nullSafeToString(Function, Class...)
* @see StringUtils#nullSafeToString(Object)
*/
public static String nullSafeToString(Class<?>... classes) {
return nullSafeToString(Class::getName, classes);
}
/**
* Generate a comma-separated list of mapped values for the supplied classes.
*
* <p>The values are generated by the supplied {@code mapper}
* (e.g., {@code Class::getName}, {@code Class::getSimpleName}, etc.), unless
* a class reference is {@code null} in which case it will be mapped to
* {@code "null"}.
*
* @param mapper the mapper to use
* @param classes the classes to map
* @return a comma-separated list of mapped values, or an empty string if
* the supplied class array is {@code null} or empty
* @see #nullSafeToString(Class...)
* @see StringUtils#nullSafeToString(Object)
*/
public static String nullSafeToString(Function<? super Class<?>, ? extends String> mapper, Class<?>... classes) {
if (classes == null || classes.length == 0) {
return "";
}
return stream(classes).map(v -> v == null ? "null" : mapper.apply(v)).collect(joining(", "));
}
}