Skip to content

Commit 9a61aa4

Browse files
DanCorderjoel-costigliola
authored andcommitted
Fixes assertj#126 : Remove duplicated code in array assertions
1 parent cd6d29c commit 9a61aa4

File tree

1 file changed

+48
-121
lines changed

1 file changed

+48
-121
lines changed

src/main/java/org/assertj/core/internal/Arrays.java

Lines changed: 48 additions & 121 deletions
Original file line numberDiff line numberDiff line change
@@ -435,176 +435,103 @@ void assertDoesNotContainNull(AssertionInfo info, Failures failures, Object arra
435435

436436
public <E> void assertAre(AssertionInfo info, Failures failures, Conditions conditions, Object array,
437437
Condition<E> condition) {
438-
assertNotNull(info, array);
439-
conditions.assertIsNotNull(condition);
440-
try {
441-
List<E> notSatisfiesCondition = elementsNotSatisfyingCondition(array, condition);
442-
if (!notSatisfiesCondition.isEmpty())
443-
throw failures.failure(info, elementsShouldBe(array, notSatisfiesCondition, condition));
444-
} catch (ClassCastException e) {
445-
throw failures.failure(info, shouldBeSameGenericBetweenIterableAndCondition(array, condition));
438+
List<E> notMatchingCondition = getElementsNotMatchingCondition(info, failures, conditions, array, condition);
439+
if (!notMatchingCondition.isEmpty())
440+
throw failures.failure(info, elementsShouldBe(array, notMatchingCondition, condition));
446441
}
447-
}
448442

449443
public <E> void assertAreNot(AssertionInfo info, Failures failures, Conditions conditions, Object array,
450444
Condition<E> condition) {
451-
assertNotNull(info, array);
452-
conditions.assertIsNotNull(condition);
453-
try {
454-
List<E> satisfiesCondition = elementsSatisfyingCondition(array, condition);
455-
if (satisfiesCondition.isEmpty()) {
456-
return;
457-
}
458-
throw failures.failure(info, elementsShouldNotBe(array, satisfiesCondition, condition));
459-
} catch (ClassCastException e) {
460-
throw failures.failure(info, shouldBeSameGenericBetweenIterableAndCondition(array, condition));
445+
List<E> matchingElements = getElementsMatchingCondition(info, failures, conditions, array, condition);
446+
if (!matchingElements.isEmpty())
447+
throw failures.failure(info, elementsShouldNotBe(array, matchingElements, condition));
461448
}
462-
}
463449

464450
public <E> void assertHave(AssertionInfo info, Failures failures, Conditions conditions, Object array,
465451
Condition<E> condition) {
466-
assertNotNull(info, array);
467-
conditions.assertIsNotNull(condition);
468-
try {
469-
List<E> notSatisfiesCondition = elementsNotSatisfyingCondition(array, condition);
470-
if (notSatisfiesCondition.isEmpty()) {
471-
return;
472-
}
473-
throw failures.failure(info, elementsShouldHave(array, notSatisfiesCondition, condition));
474-
} catch (ClassCastException e) {
475-
throw failures.failure(info, shouldBeSameGenericBetweenIterableAndCondition(array, condition));
452+
List<E> notMatchingCondition = getElementsNotMatchingCondition(info, failures, conditions, array, condition);
453+
if (!notMatchingCondition.isEmpty())
454+
throw failures.failure(info, elementsShouldHave(array, notMatchingCondition, condition));
476455
}
477-
}
478456

479457
public <E> void assertHaveNot(AssertionInfo info, Failures failures, Conditions conditions, Object array,
480458
Condition<E> condition) {
481-
assertNotNull(info, array);
482-
conditions.assertIsNotNull(condition);
483-
try {
484-
List<E> satisfiesCondition = elementsSatisfyingCondition(array, condition);
485-
if (satisfiesCondition.isEmpty()) {
486-
return;
487-
}
488-
throw failures.failure(info, elementsShouldNotHave(array, satisfiesCondition, condition));
489-
} catch (ClassCastException e) {
490-
throw failures.failure(info, shouldBeSameGenericBetweenIterableAndCondition(array, condition));
491-
}
459+
List<E> matchingElements = getElementsMatchingCondition(info, failures, conditions, array, condition);
460+
if (!matchingElements.isEmpty())
461+
throw failures.failure(info, elementsShouldNotHave(array, matchingElements, condition));
492462
}
493463

494464
public <E> void assertAreAtLeast(AssertionInfo info, Failures failures, Conditions conditions, Object array,
495465
int times, Condition<E> condition) {
496-
assertNotNull(info, array);
497-
conditions.assertIsNotNull(condition);
498-
try {
499-
List<E> satisfiesCondition = elementsSatisfyingCondition(array, condition);
500-
if (satisfiesCondition.size() >= times) {
501-
return;
502-
}
466+
List<E> matchingElements = getElementsMatchingCondition(info, failures, conditions, array, condition);
467+
if (matchingElements.size() < times)
503468
throw failures.failure(info, elementsShouldBeAtLeast(array, times, condition));
504-
} catch (ClassCastException e) {
505-
throw failures.failure(info, shouldBeSameGenericBetweenIterableAndCondition(array, condition));
506469
}
507-
}
508470

509471
public <E> void assertAreAtMost(AssertionInfo info, Failures failures, Conditions conditions, Object array,
510472
int times, Condition<E> condition) {
511-
assertNotNull(info, array);
512-
conditions.assertIsNotNull(condition);
513-
try {
514-
List<E> satisfiesCondition = elementsSatisfyingCondition(array, condition);
515-
if (satisfiesCondition.size() <= times) {
516-
return;
517-
}
518-
throw failures.failure(info, elementsShouldBeAtMost(array, times, condition));
519-
} catch (ClassCastException e) {
520-
throw failures.failure(info, shouldBeSameGenericBetweenIterableAndCondition(array, condition));
473+
List<E> matchingElements = getElementsMatchingCondition(info, failures, conditions, array, condition);
474+
if (matchingElements.size() > times) throw failures.failure(info, elementsShouldBeAtMost(array, times, condition));
521475
}
522-
}
523476

524477
public <E> void assertAreExactly(AssertionInfo info, Failures failures, Conditions conditions, Object array,
525478
int times, Condition<E> condition) {
526-
assertNotNull(info, array);
527-
conditions.assertIsNotNull(condition);
528-
try {
529-
List<E> satisfiesCondition = elementsSatisfyingCondition(array, condition);
530-
if (satisfiesCondition.size() == times) {
531-
return;
532-
}
479+
List<E> matchingElements = getElementsMatchingCondition(info, failures, conditions, array, condition);
480+
if (matchingElements.size() != times)
533481
throw failures.failure(info, elementsShouldBeExactly(array, times, condition));
534-
} catch (ClassCastException e) {
535-
throw failures.failure(info, shouldBeSameGenericBetweenIterableAndCondition(array, condition));
536482
}
537-
}
538483

539484
public <E> void assertHaveAtLeast(AssertionInfo info, Failures failures, Conditions conditions, Object array,
540485
int times, Condition<E> condition) {
541-
assertNotNull(info, array);
542-
conditions.assertIsNotNull(condition);
543-
try {
544-
List<E> satisfiesCondition = elementsSatisfyingCondition(array, condition);
545-
if (satisfiesCondition.size() >= times) {
546-
return;
547-
}
486+
List<E> matchingElements = getElementsMatchingCondition(info, failures, conditions, array, condition);
487+
if (matchingElements.size() < times)
548488
throw failures.failure(info, elementsShouldHaveAtLeast(array, times, condition));
549-
} catch (ClassCastException e) {
550-
throw failures.failure(info, shouldBeSameGenericBetweenIterableAndCondition(array, condition));
551-
}
489+
552490
}
553491

554492
public <E> void assertHaveAtMost(AssertionInfo info, Failures failures, Conditions conditions, Object array,
555493
int times, Condition<E> condition) {
556-
assertNotNull(info, array);
557-
conditions.assertIsNotNull(condition);
558-
try {
559-
List<E> satisfiesCondition = elementsSatisfyingCondition(array, condition);
560-
if (satisfiesCondition.size() <= times) {
561-
return;
562-
}
494+
List<E> matchingElements = getElementsMatchingCondition(info, failures, conditions, array, condition);
495+
if (matchingElements.size() > times)
563496
throw failures.failure(info, elementsShouldHaveAtMost(array, times, condition));
564-
} catch (ClassCastException e) {
565-
throw failures.failure(info, shouldBeSameGenericBetweenIterableAndCondition(array, condition));
566-
}
497+
567498
}
568499

569500
public <E> void assertHaveExactly(AssertionInfo info, Failures failures, Conditions conditions, Object array,
570501
int times, Condition<E> condition) {
502+
List<E> matchingElements = getElementsMatchingCondition(info, failures, conditions, array, condition);
503+
if (matchingElements.size() != times)
504+
throw failures.failure(info, elementsShouldHaveExactly(array, times, condition));
505+
}
506+
507+
private <E> List<E> getElementsMatchingCondition(AssertionInfo info, Failures failures, Conditions conditions,
508+
Object array, Condition<E> condition) {
509+
return filterElements(info, failures, conditions, array, condition, false);
510+
}
511+
512+
private <E> List<E> getElementsNotMatchingCondition(AssertionInfo info, Failures failures, Conditions conditions,
513+
Object array, Condition<E> condition) {
514+
return filterElements(info, failures, conditions, array, condition, true);
515+
}
516+
517+
@SuppressWarnings("unchecked")
518+
private <E> List<E> filterElements(AssertionInfo info, Failures failures, Conditions conditions, Object array,
519+
Condition<E> condition, boolean negateCondition) throws AssertionError {
571520
assertNotNull(info, array);
572521
conditions.assertIsNotNull(condition);
573522
try {
574-
List<E> satisfiesCondition = elementsSatisfyingCondition(array, condition);
575-
if (satisfiesCondition.size() == times) {
576-
return;
523+
List<E> filteredElements = new LinkedList<E>();
524+
int arraySize = sizeOf(array);
525+
for (int i = 0; i < arraySize; i++) {
526+
E element = (E) Array.get(array, i);
527+
if (negateCondition ? !condition.matches(element) : condition.matches(element)) filteredElements.add(element);
577528
}
578-
throw failures.failure(info, elementsShouldHaveExactly(array, times, condition));
529+
return filteredElements;
579530
} catch (ClassCastException e) {
580531
throw failures.failure(info, shouldBeSameGenericBetweenIterableAndCondition(array, condition));
581532
}
582533
}
583534

584-
@SuppressWarnings("unchecked")
585-
private <E> List<E> elementsNotSatisfyingCondition(Object array, Condition<E> condition) {
586-
List<E> elementsNotSatisfyingCondition = new LinkedList<E>();
587-
int arraySize = sizeOf(array);
588-
for (int i = 0; i < arraySize; i++) {
589-
Object o = Array.get(array, i);
590-
if (!condition.matches((E) o))
591-
elementsNotSatisfyingCondition.add((E) o);
592-
}
593-
return elementsNotSatisfyingCondition;
594-
}
595-
596-
@SuppressWarnings("unchecked")
597-
private <E> List<E> elementsSatisfyingCondition(Object array, Condition<E> condition) {
598-
List<E> elementsSatisfyingCondition = new LinkedList<E>();
599-
int arraySize = sizeOf(array);
600-
for (int i = 0; i < arraySize; i++) {
601-
Object o = Array.get(array, i);
602-
if (condition.matches((E) o))
603-
elementsSatisfyingCondition.add((E) o);
604-
}
605-
return elementsSatisfyingCondition;
606-
}
607-
608535
void assertIsSorted(AssertionInfo info, Failures failures, Object array) {
609536
assertNotNull(info, array);
610537
if (comparisonStrategy instanceof ComparatorBasedComparisonStrategy) {

0 commit comments

Comments
 (0)