Example usage for org.apache.commons.collections4 IterableUtils isEmpty

List of usage examples for org.apache.commons.collections4 IterableUtils isEmpty

Introduction

In this page you can find the example usage for org.apache.commons.collections4 IterableUtils isEmpty.

Prototype

public static boolean isEmpty(final Iterable<?> iterable) 

Source Link

Document

Answers true if the provided iterable is empty.

Usage

From source file:fr.landel.utils.commons.CollectionUtils2.java

/**
 * To array an iterable (take the type of the first element, otherwise use
 * the default 'toArray(new T[0])'). Returns {@code null} if the iterable is
 * empty.//  w  w  w .  j  a v a  2  s  . com
 * 
 * @param iterable
 *            The input iterable (required)
 * @param type
 *            the type of objects (optional, if null take the type of the
 *            first element)
 * @param <T>
 *            The type of object in collection
 * @return The array
 */
@SuppressWarnings("unchecked")
public static <T> T[] toArray(final Iterable<T> iterable, final Class<T> type) {
    if (!IterableUtils.isEmpty(iterable)) {
        final Iterator<T> iterator = iterable.iterator();
        final List<T> list = new ArrayList<>();
        final Set<Class<T>> classes = new HashSet<>();
        while (iterator.hasNext()) {
            final T obj = iterator.next();
            list.add(obj);
            if (obj != null) {
                classes.add(ClassUtils.getClass(obj));
            }
        }
        final Class<?> typeClass;
        if (type != null) {
            typeClass = type;
        } else if (classes.size() == 1) {
            typeClass = classes.iterator().next();
        } else {
            typeClass = Object.class;
        }
        return list.toArray((T[]) Array.newInstance(typeClass, list.size()));
    } else if (iterable != null && type != null) {
        return (T[]) Array.newInstance(type, 0);
    }
    return null;
}

From source file:fr.landel.utils.commons.CollectionUtils2.java

/**
 * List all classes in an iterable ({@code null} values are excluded in the
 * output set)/*from  ww w .  j  av  a2  s .com*/
 * 
 * @param iterable
 *            The iterable to check
 * @param <T>
 *            The generic type of iterable
 * @return a list of classes, even if iterable is null or empty
 */
public static <T> Set<Class<T>> getClasses(final Iterable<T> iterable) {
    final Set<Class<T>> classes = new HashSet<>();
    if (!IterableUtils.isEmpty(iterable)) {
        final Iterator<T> iterator = iterable.iterator();
        while (iterator.hasNext()) {
            final T obj = iterator.next();
            if (obj != null) {
                classes.add(ClassUtils.getClass(obj));
            }
        }
    }
    return classes;
}

From source file:fr.landel.utils.commons.CollectionUtils2.java

/**
 * Check if the iterable contains specific types (all {@code null} values
 * and classes are excluded)//from www.j  a  va 2s  .  c om
 * 
 * @param iterable
 *            The iterable to check
 * @param classes
 *            The list of classes ({@code null}, returns false, all
 *            {@code null} classes are removed)
 * @param <T>
 *            The generic type of iterable
 * @return true, if all classes are found
 */
public static <T> boolean containsClasses(final Iterable<T> iterable, final Class<?>... classes) {
    if (classes != null && !IterableUtils.isEmpty(iterable)) {
        int found = 0;

        // remove null classes
        final Set<Class<?>> classRefSet = new HashSet<>();
        for (Class<?> clazz : classes) {
            if (clazz != null) {
                classRefSet.add(clazz);
            }
        }

        // list all classes in provided iterable
        final Set<Class<T>> classSet = getClasses(iterable);

        // count
        for (Class<?> clazz : classRefSet) {
            if (classSet.contains(clazz)) {
                found++;
            }
        }

        return found == classRefSet.size();
    }
    return false;
}

From source file:fr.landel.utils.assertor.utils.AssertorIterable.java

/**
 * Prepare the next step to validate if the {@link Iterable} is {@code null}
 * or empty//ww w  .  java 2 s .c  o m
 * 
 * <p>
 * precondition: none
 * </p>
 * 
 * @param step
 *            the current step
 * @param message
 *            the message if invalid
 * @param <I>
 *            the {@link Iterable} type
 * @param <T>
 *            the {@link Iterable} elements type
 * @return the next step
 */
public static <I extends Iterable<T>, T> StepAssertor<I> isEmpty(final StepAssertor<I> step,
        final MessageAssertor message) {

    final BiPredicate<I, Boolean> checker = (iterable, not) -> IterableUtils.isEmpty(iterable);

    return new StepAssertor<>(step, checker, false, message, MSG.ITERABLE.EMPTY, false);
}

From source file:fr.landel.utils.assertor.utils.AssertorIterable.java

/**
 * Prepare the next step to validate if the {@link Iterable} is NOT
 * {@code null} and NOT empty//from   w w w  .j  a  va  2  s.  c o m
 * 
 * <p>
 * precondition: none
 * </p>
 * 
 * @param step
 *            the current step
 * @param message
 *            the message if invalid
 * @param <I>
 *            the {@link Iterable} type
 * @param <T>
 *            the {@link Iterable} elements type
 * @return the next step
 */
public static <I extends Iterable<T>, T> StepAssertor<I> isNotEmpty(final StepAssertor<I> step,
        final MessageAssertor message) {

    final BiPredicate<I, Boolean> checker = (iterable, not) -> !IterableUtils.isEmpty(iterable);

    return new StepAssertor<>(step, checker, false, message, MSG.ITERABLE.EMPTY, true);
}

From source file:fr.landel.utils.assertor.utils.AssertorIterable.java

private static <I extends Iterable<T>, T> StepAssertor<I> match(final StepAssertor<I> step,
        final Predicate<T> predicate, final boolean all, final String messageKey,
        final MessageAssertor message) {

    final Predicate<I> preChecker = (iterable) -> !IterableUtils.isEmpty(iterable) && predicate != null;

    final BiPredicate<I, Boolean> checker = (iterable, not) -> AssertorIterable.has(iterable, predicate, all,
            step.getAnalysisMode());//from  w w  w .  java2  s  . com

    return new StepAssertor<>(step, preChecker, checker, false, message, messageKey, false,
            new ParameterAssertor<>(predicate, EnumType.UNKNOWN));
}

From source file:fr.landel.utils.assertor.utils.AssertorIterable.java

/**
 * Prepare the next step to validate if the {@link Iterable} contains the
 * specified value//from   ww  w  . j  a  va  2  s  . c  om
 * 
 * <p>
 * precondition: the {@link Iterable} cannot be {@code null} or empty
 * </p>
 * 
 * @param step
 *            the current step
 * @param value
 *            the value to find
 * @param message
 *            the message if invalid
 * @param <I>
 *            the {@link Iterable} type
 * @param <T>
 *            the {@link Iterable} elements type
 * @return the next step
 */
public static <I extends Iterable<T>, T> StepAssertor<I> contains(final StepAssertor<I> step, final T value,
        final MessageAssertor message) {

    final Predicate<I> preChecker = (iterable) -> !IterableUtils.isEmpty(iterable);

    final BiPredicate<I, Boolean> checker = (iterable, not) -> AssertorIterable.has(iterable, value,
            step.getAnalysisMode());

    return new StepAssertor<>(step, preChecker, checker, false, message, MSG.ITERABLE.CONTAINS_OBJECT, false,
            new ParameterAssertor<>(value));
}

From source file:fr.landel.utils.assertor.utils.AssertorIterable.java

private static <I extends Iterable<T>, T> StepAssertor<I> contains(final StepAssertor<I> step,
        final Iterable<T> iterable, final CharSequence key, final boolean all, final MessageAssertor message) {

    final Predicate<I> preChecker = (iterable1) -> !IterableUtils.isEmpty(iterable1)
            && !IterableUtils.isEmpty(iterable);

    final BiPredicate<I, Boolean> checker = (iterable1, not) -> AssertorIterable.has(iterable1, iterable, all,
            not, step.getAnalysisMode());

    return new StepAssertor<>(step, preChecker, checker, true, message, key, false,
            new ParameterAssertor<>(iterable, EnumType.ITERABLE));
}

From source file:fr.landel.utils.assertor.utils.AssertorIterable.java

/**
 * Prepare the next step to validate if the {@link Iterable} contains values
 * in a specified order//  w w  w .j  a va 2s.c o m
 * 
 * <p>
 * precondition: neither {@link Iterable} can be {@code null} or empty
 * </p>
 * 
 * @param step
 *            the current step
 * @param values
 *            the values to find
 * @param message
 *            the message if invalid
 * @param <I>
 *            the {@link Iterable} type
 * @param <T>
 *            the {@link Iterable} elements type
 * @return the next step
 */
public static <I extends Iterable<T>, T> StepAssertor<I> containsInOrder(final StepAssertor<I> step,
        final Iterable<T> values, final MessageAssertor message) {

    final Predicate<I> preChecker = (iterable1) -> !IterableUtils.isEmpty(iterable1)
            && !IterableUtils.isEmpty(values);

    final BiPredicate<I, Boolean> checker = (iterable1, not) -> AssertorIterable.hasInOrder(iterable1, values,
            not, step.getAnalysisMode());

    return new StepAssertor<>(step, preChecker, checker, true, message, MSG.ITERABLE.CONTAINS_IN_ORDER, false,
            new ParameterAssertor<>(values, EnumType.ITERABLE));
}

From source file:fr.landel.utils.assertor.utils.AssertorMap.java

private static <M extends Map<K, V>, K, V> StepAssertor<M> contains(final StepAssertor<M> step,
        final Iterable<K> keys, final CharSequence key, final boolean all, final MessageAssertor message) {

    final Predicate<M> preChecker = (map) -> MapUtils.isNotEmpty(map) && !IterableUtils.isEmpty(keys);

    final BiPredicate<M, Boolean> checker = (map, not) -> AssertorMap.contains(map, keys, map::containsKey, all,
            not, step.getAnalysisMode());

    return new StepAssertor<>(step, preChecker, checker, true, message, key, false,
            new ParameterAssertor<>(keys, EnumType.ITERABLE));
}