/** * Returns true if given {@link Iterable} contains given value according to {@link #comparator}, false otherwise.<br> * If given {@link Iterable} is null or empty, return false. * * @param iterable the {@link Iterable} to search value in * @param value the object to look for in given {@link Iterable} * @return true if given {@link Iterable} contains given value according to {@link #comparator}, false otherwise. */ @SuppressWarnings("unchecked") public boolean iterableContains(Iterable<?> iterable, Object value) { if (isNullOrEmpty(iterable)) return false; for (Object element : iterable) { // avoid comparison when objects are the same or both null if (element == value) return true; // both objects are not null => if one is then the other is not => compare next element with value if (value == null || element == null) continue; if (comparator.compare(element, value) == 0) return true; } return false; }
/** * Assert that the actual {@code Iterable} has the same size as the other {@code Iterable}. * * @param info contains information about the assertion. * @param actual the given {@code Iterable}. * @param other the given {@code Iterable}. * @throws AssertionError if the actual group is {@code null}. * @throws AssertionError if the other group is {@code null}. * @throws AssertionError if actual and other {@code Iterable} don't have the same size. */ public void assertHasSameSizeAs(AssertionInfo info, Iterable<?> actual, Iterable<?> other) { assertNotNull(info, actual); checkNotNull(info, other); int sizeOfActual = sizeOf(actual); int sizeOfOther = sizeOf(other); if (sizeOfActual == sizeOfOther) { return; } throw failures.failure(info, shouldHaveSameSizeAs(actual, sizeOfActual, sizeOfOther)); }
/** * Returns a <code>{@link List}</code> containing the values of the given property name, from the elements of the given * <code>{@link Iterable}</code>. If the given {@code Iterable} is empty or {@code null}, this method will return an empty * {@code List}. This method supports nested properties (e.g. "address.street.number"). * @param propertyName the name of the property. It may be a nested property. It is left to the clients to validate for * {@code null} or empty. * @param target the given {@code Iterable}. * @return an {@code Iterable} containing the values of the given property name, from the elements of the given {@code Iterable}. * @throws IntrospectionError if an element in the given {@code Iterable} does not have a property with a matching name. */ public <T> Iterable<T> propertyValues(String propertyName, Class<T> clazz, Iterable<?> target) { // ignore null elements as we can't extract a property from a null object Iterable<?> cleanedUp = nonNullElementsIn(target); if (isNullOrEmpty(cleanedUp)) { return emptyList(); } if (isNestedProperty(propertyName)) { String firstPropertyName = popPropertyNameFrom(propertyName); Iterable<Object> propertyValues = propertyValues(firstPropertyName, Object.class, cleanedUp); // extract next sub-property values until reaching the last sub-property return propertyValues(nextPropertyNameFrom(propertyName), clazz, propertyValues); } return simplePropertyValues(propertyName, clazz, cleanedUp); }
@Override public Iterable<?> duplicatesFrom(Iterable<?> iterable) { Set<Object> duplicates = new HashSet<Object>(); if (isNullOrEmpty(iterable)) { return duplicates; } Set<Object> noDuplicates = new HashSet<Object>(); for (Object element : iterable) { if (iterableContains(noDuplicates, element)) { duplicates.add(element); continue; } noDuplicates.add(element); } return duplicates; }
/** * Asserts that the number of entries in the given {@code String} has the same size as the other {@code Iterable}. * * @param info contains information about the assertion. * @param actual the given {@code String}. * @param other the group to compare * @throws AssertionError if the given {@code String}. is {@code null}. * @throws AssertionError if the given {@code Iterable} is {@code null}. * @throws AssertionError if the number of entries in the given {@code String} does not have the same size. */ public void assertHasSameSizeAs(AssertionInfo info, String actual, Iterable<?> other) { assertNotNull(info, actual); if (other == null) { throw new NullPointerException("The iterable to look for should not be null"); } int sizeOfActual = actual.length(); int sizeOfOther = sizeOf(other); if (sizeOfActual == sizeOfOther) { return; } throw failures.failure(info, shouldHaveSameSizeAs(actual, sizeOfActual, sizeOfOther)); }
/** * Asserts that the given <code>{@link Iterable}</code> is {@code null} or empty. * * @param info contains information about the assertion. * @param actual the given {@code Iterable}. * @throws AssertionError if the given {@code Iterable} is not {@code null} *and* contains one or more elements. */ public void assertNullOrEmpty(AssertionInfo info, Iterable<?> actual) { if (actual == null || isNullOrEmpty(actual)) { return; } throw failures.failure(info, shouldBeNullOrEmpty(actual)); }
void assertHasSameSizeAs(AssertionInfo info, Failures failures, Object array, Iterable<?> other) { assertNotNull(info, array); if (other == null) { throw new NullPointerException("The iterable to look for should not be null"); } int sizeOfActual = sizeOf(array); int sizeOfOther = org.fest.util.Iterables.sizeOf(other); if (sizeOfActual == sizeOfOther) { return; } throw failures.failure(info, shouldHaveSameSizeAs(array, sizeOfActual, sizeOfOther)); }
/** * Asserts that the given {@code Iterable} is not empty. * * @param info contains information about the assertion. * @param actual the given {@code Iterable}. * @throws AssertionError if the given {@code Iterable} is {@code null}. * @throws AssertionError if the given {@code Iterable} is empty. */ public void assertNotEmpty(AssertionInfo info, Iterable<?> actual) { assertNotNull(info, actual); if (!isNullOrEmpty(actual)) { return; } throw failures.failure(info, shouldNotBeEmpty()); }
/** * Asserts that the number of entries in the given {@code Map} has the same size as the other {@code Iterable}. * @param info contains information about the assertion. * @param map the given {@code Map}. * @param other the group to compare * @throws AssertionError if the given {@code Map} is {@code null}. * @throws AssertionError if the given {@code Iterable} is {@code null}. * @throws AssertionError if the number of entries in the given {@code Map} does not have the same size. */ public void assertHasSameSizeAs(AssertionInfo info, Map<?, ?> map, Iterable<?> other) { assertNotNull(info, map); if (other == null) { throw new NullPointerException("The iterable to look for should not be null"); } int sizeOfActual = map.size(); int sizeOfOther = sizeOf(other); if (sizeOfActual == sizeOfOther) { return; } throw failures.failure(info, shouldHaveSameSizeAs(map, sizeOfActual, sizeOfOther)); }
/** * Asserts that the given {@code Iterable} is empty. * * @param info contains information about the assertion. * @param actual the given {@code Iterable}. * @throws AssertionError if the given {@code Iterable} is {@code null}. * @throws AssertionError if the given {@code Iterable} is not empty. */ public void assertEmpty(AssertionInfo info, Iterable<?> actual) { assertNotNull(info, actual); if (isNullOrEmpty(actual)) { return; } throw failures.failure(info, shouldBeEmpty(actual)); }
/** * Asserts that the number of elements in the given {@code Iterable} is equal to the expected one. * * @param info contains information about the assertion. * @param actual the given {@code Iterable}. * @param expectedSize the expected size of {@code actual}. * @throws AssertionError if the given {@code Iterable} is {@code null}. * @throws AssertionError if the number of elements in the given {@code Iterable} is different than the expected one. */ public void assertHasSize(AssertionInfo info, Iterable<?> actual, int expectedSize) { assertNotNull(info, actual); int sizeOfActual = sizeOf(actual); if (sizeOfActual == expectedSize) { return; } throw failures.failure(info, shouldHaveSize(actual, sizeOfActual, expectedSize)); }
/** * Asserts that the given {@code Iterable} does not have duplicate values. * * @param info contains information about the assertion. * @param actual the given {@code Iterable}. * @throws NullPointerException if the array of values is {@code null}. * @throws IllegalArgumentException if the array of values is empty. * @throws AssertionError if the given {@code Iterable} is {@code null}. * @throws AssertionError if the given {@code Iterable} contains duplicate values. */ public void assertDoesNotHaveDuplicates(AssertionInfo info, Iterable<?> actual) { assertNotNull(info, actual); Iterable<?> duplicates = comparisonStrategy.duplicatesFrom(actual); if (isNullOrEmpty(duplicates)) { return; } throw failures.failure(info, shouldNotHaveDuplicates(actual, duplicates, comparisonStrategy)); }
/** * Assert that the actual {@code Iterable} has the same size as the other array. * * @param info contains information about the assertion. * @param actual the given {@code Iterable}. * @param other the given array to compare. * @throws AssertionError if the actual group is {@code null}. * @throws AssertionError if the other group is {@code null}. * @throws AssertionError if actual {@code Iterable} and other array don't have the same size. */ public void assertHasSameSizeAs(AssertionInfo info, Iterable<?> actual, Object[] other) { assertNotNull(info, actual); if (other == null) { throw arrayOfValuesToLookForIsNull(); } int sizeOfActual = sizeOf(actual); int sizeOfOther = other.length; if (sizeOfActual == sizeOfOther) { return; } throw failures.failure(info, shouldHaveSameSizeAs(actual, sizeOfActual, sizeOfOther)); }
void assertDoesNotHaveDuplicates(AssertionInfo info, Failures failures, Object array) { assertNotNull(info, array); ArrayWrapperList wrapped = wrap(array); Iterable<?> duplicates = comparisonStrategy.duplicatesFrom(wrapped); if (isNullOrEmpty(duplicates)) { return; } throw failures.failure(info, shouldNotHaveDuplicates(array, duplicates, comparisonStrategy)); }
assertNotNull(info, actual); int sequenceSize = sequence.length; if (sizeOf(actual) < sequenceSize) { throw actualDoesNotStartWithSequence(info, actual, sequence);
/** * Returns all the non-{@code null} elements in the given {@link Iterable}. * * @param <T> the type of elements of the {@code Iterable}. * @param i the given {@code Iterable}. * @return all the non-{@code null} elements in the given {@code Iterable}. An empty list is returned if the given * {@code Iterable} is {@code null}. * @since 1.1.3 */ public static <T> List<T> nonNullElementsIn(Iterable<T> i) { if (isNullOrEmpty(i)) { return emptyList(); } List<T> nonNull = new ArrayList<T>(); for (T element : i) { if (element != null) { nonNull.add(element); } } return nonNull; }
assertNotNull(info, actual); int sequenceSize = sequence.length; int sizeOfActual = sizeOf(actual); if (sizeOfActual < sequenceSize) { throw actualDoesNotEndWithSequence(info, actual, sequence);