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

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

Introduction

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

Prototype

public static int size(final Iterable<?> iterable) 

Source Link

Document

Returns the number of elements contained in the given iterator.

Usage

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

/**
 * Prepare the next step to validate that the {@link Iterable} size is equal
 * to {@code size}.//  w  w w  .jav  a2 s  . co  m
 * 
 * <p>
 * precondition: {@link Iterable} cannot be {@code null} and size cannot be
 * lower than zero
 * </p>
 * 
 * @param step
 *            the current step
 * @param size
 *            the size to validate
 * @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> hasSize(final StepAssertor<I> step, final int size,
        final MessageAssertor message) {

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

    return checkSize(step, size, checker, MSG.ITERABLE.SIZE, message);
}

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

/**
 * Prepare the next step to validate that the {@link Iterable} size is
 * greater than {@code size}./*w ww  .ja  va  2 s.com*/
 * 
 * <p>
 * precondition: {@link Iterable} cannot be {@code null} and size cannot be
 * lower than zero
 * </p>
 * 
 * @param step
 *            the current step
 * @param size
 *            the size to validate
 * @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> hasSizeGT(final StepAssertor<I> step, final int size,
        final MessageAssertor message) {

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

    return checkSize(step, size, checker, MSG.ITERABLE.SIZE_GT, message);
}

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

/**
 * Prepare the next step to validate that the {@link Iterable} size is
 * greater than or equal to {@code size}.
 * //w  w w  .ja  va 2s  .  c o m
 * <p>
 * precondition: {@link Iterable} cannot be {@code null} and size cannot be
 * lower than zero
 * </p>
 * 
 * @param step
 *            the current step
 * @param size
 *            the size to validate
 * @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> hasSizeGTE(final StepAssertor<I> step, final int size,
        final MessageAssertor message) {

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

    return checkSize(step, size, checker, MSG.ITERABLE.SIZE_GTE, message);
}

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

/**
 * Prepare the next step to validate that the {@link Iterable} size is lower
 * than {@code size}./*w  w w  .ja v a2 s .c om*/
 * 
 * <p>
 * precondition: {@link Iterable} cannot be {@code null} and size cannot be
 * lower than zero
 * </p>
 * 
 * @param step
 *            the current step
 * @param size
 *            the size to validate
 * @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> hasSizeLT(final StepAssertor<I> step, final int size,
        final MessageAssertor message) {

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

    return checkSize(step, size, checker, MSG.ITERABLE.SIZE_LT, message);
}

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

/**
 * Prepare the next step to validate that the {@link Iterable} size is lower
 * than or equal to {@code size}.//  www. j a  v  a2 s  .  co m
 * 
 * <p>
 * precondition: {@link Iterable} cannot be {@code null} and size cannot be
 * lower than zero
 * </p>
 * 
 * @param step
 *            the current step
 * @param size
 *            the size to validate
 * @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> hasSizeLTE(final StepAssertor<I> step, final int size,
        final MessageAssertor message) {

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

    return checkSize(step, size, checker, MSG.ITERABLE.SIZE_LTE, message);
}

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

private static <I extends Iterable<T>, T> boolean has(final I iterable1, final Iterable<T> iterable2,
        final boolean all, final boolean not, final EnumAnalysisMode analysisMode) {

    final int size2 = IterableUtils.size(iterable2);

    if (all && !not && size2 > IterableUtils.size(iterable1)) {
        return false;
    }//from ww w  .  j a v a2 s. com

    if (EnumAnalysisMode.STANDARD.equals(analysisMode)) {
        if (all && !not) {
            for (final T objectRef : iterable2) {
                if (!AssertorIterable.has(iterable1, objectRef, analysisMode)) {
                    return false;
                }
            }
            return true;
        } else if (!all) { // any and not any
            for (final T objectRef : iterable2) {
                if (AssertorIterable.has(iterable1, objectRef, analysisMode)) {
                    return !not;
                }
            }
            return not;
        } else { // not all
            long found = 0;
            for (final T objectRef : iterable2) {
                if (AssertorIterable.has(iterable1, objectRef, analysisMode)) {
                    ++found;
                }
            }
            return HelperAssertor.isValid(all, not, found, size2);
        }

    } else {
        return HelperAssertor.isValid(
                StreamSupport.stream(iterable2.spliterator(), EnumAnalysisMode.PARALLEL.equals(analysisMode)),
                o -> AssertorIterable.has(iterable1, o, analysisMode), all, not, () -> size2);
    }
}

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

private static <I extends Iterable<T>, T> boolean hasInOrder(final I iterable1, final Iterable<T> iterable2,
        final boolean not, final EnumAnalysisMode analysisMode) {

    long found = 0;
    final int size1 = IterableUtils.size(iterable1);
    final int size2 = IterableUtils.size(iterable2);

    if (size1 < size2) {
        return not;
    } else if (size1 == size2) {
        return not ^ iterable1.equals(iterable2);
    }/*from   w w w .j  a  va 2s  .  c  o m*/

    if (EnumAnalysisMode.STANDARD.equals(analysisMode)) {
        final Iterator<T> iterator1 = iterable1.iterator();
        Iterator<T> iterator2 = iterable2.iterator();

        // not empty pre-check, so we call next directly
        T value2 = iterator2.next();
        while (iterator1.hasNext() && found < size2) {
            if (Objects.equals(iterator1.next(), value2)) {
                ++found;
                if (iterator2.hasNext()) {
                    value2 = iterator2.next();
                }
            } else if (found > 0) {
                found = 0;
                iterator2 = iterable2.iterator();
                value2 = iterator2.next();
            }
        }
    } else {
        final AtomicInteger count = new AtomicInteger(0);

        final List<T> list2 = IterableUtils.toList(iterable2);

        StreamSupport.stream(iterable1.spliterator(), EnumAnalysisMode.PARALLEL.equals(analysisMode))
                .forEachOrdered(o -> {
                    int inc = count.get();
                    if (inc < size2) {
                        if (Objects.equals(o, list2.get(inc))) {
                            count.incrementAndGet();
                        } else if (inc > 0) {
                            count.set(0);
                        }
                    }
                });

        found = count.get();
    }

    return not ^ (found == size2);
}

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

private static <M extends Map<K, V>, K, V, T> boolean contains(final M map, final Iterable<T> objects,
        final Predicate<T> predicate, final boolean all, final boolean not,
        final EnumAnalysisMode analysisMode) {

    long found = 0;

    if (EnumAnalysisMode.STANDARD.equals(analysisMode)) {
        for (T object : objects) {
            if (predicate.test(object)) {
                ++found;/* w w  w.  j ava 2  s. c om*/
            }
        }
    } else {
        found = StreamSupport.stream(objects.spliterator(), EnumAnalysisMode.PARALLEL.equals(analysisMode))
                .filter(predicate).count();
    }

    return HelperAssertor.isValid(all, not, found, IterableUtils.size(objects));
}

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

private static <M extends Map<K, V>, K, V, T> boolean hasInOrder(final M map, final Iterable<T> objects,
        final boolean not, final EnumAnalysisMode analysisMode,
        final BiPredicate<Entry<K, V>, T> entriesEqualChecker, final Class<T> objectsClass) {

    int found = 0;

    final int size1 = map.size();
    final int size2 = IterableUtils.size(objects);

    if (size1 < size2) {
        return not;
    }/*from   w w w. j av a  2s . c  o  m*/

    final Set<Entry<K, V>> entries1 = map.entrySet();
    final List<T> entries2 = IterableUtils.toList(objects);

    if (EnumAnalysisMode.STANDARD.equals(analysisMode)) {
        for (Entry<K, V> entry1 : entries1) {
            if (found < size2) {
                if (entriesEqualChecker.test(entry1, entries2.get(found))) {
                    ++found;
                } else if (found > 0) {
                    found = 0;
                }
            }
        }
    } else {
        final AtomicInteger count = new AtomicInteger(0);

        final Stream<Entry<K, V>> stream;
        if (EnumAnalysisMode.PARALLEL.equals(analysisMode)) {
            stream = entries1.parallelStream();
        } else {
            stream = entries1.stream();
        }

        stream.forEachOrdered(o -> {
            int inc = count.get();
            if (inc < size2) {
                if (entriesEqualChecker.test(o, entries2.get(inc))) {
                    count.incrementAndGet();
                } else if (inc > 0) {
                    count.set(0);
                }
            }
        });

        found = count.get();
    }

    return not ^ (found == size2);
}