void assertIsSorted(AssertionInfo info, Failures failures, Object array) { assertNotNull(info, array); if (comparisonStrategy instanceof ComparatorBasedComparisonStrategy) { // instead of comparing array elements with their natural comparator, use the one set by client. Comparator<?> comparator = ((ComparatorBasedComparisonStrategy) comparisonStrategy).getComparator(); assertIsSortedAccordingToComparator(info, failures, array, comparator); return; } // empty arrays are considered sorted even if component type is not sortable. if (sizeOf(array) == 0) return; assertThatArrayComponentTypeIsSortable(info, failures, array); try { // sorted assertion is only relevant if array elements are Comparable // => we should be able to build a Comparable array Comparable<Object>[] comparableArray = arrayOfComparableItems(array); // array with 0 or 1 element are considered sorted. if (comparableArray.length <= 1) return; for (int i = 0; i < comparableArray.length - 1; i++) { // array is sorted in ascending order iif element i is less or equal than element i+1 if (comparableArray[i].compareTo(comparableArray[i + 1]) > 0) throw failures.failure(info, shouldBeSorted(i, array)); } } catch (ClassCastException e) { // elements are either not Comparable or not mutually Comparable (e.g. array with String and Integer) throw failures.failure(info, shouldHaveMutuallyComparableElements(array)); } }
void assertIsSorted(AssertionInfo info, Failures failures, Object array) { assertNotNull(info, array); if (comparisonStrategy instanceof ComparatorBasedComparisonStrategy) { // instead of comparing array elements with their natural comparator, use the one set by client. Comparator<?> comparator = ((ComparatorBasedComparisonStrategy) comparisonStrategy).getComparator(); assertIsSortedAccordingToComparator(info, failures, array, comparator); return; } // empty arrays are considered sorted even if component type is not sortable. if (sizeOf(array) == 0) return; assertThatArrayComponentTypeIsSortable(info, failures, array); try { // sorted assertion is only relevant if array elements are Comparable // => we should be able to build a Comparable array Comparable<Object>[] comparableArray = arrayOfComparableItems(array); // array with 0 or 1 element are considered sorted. if (comparableArray.length <= 1) return; for (int i = 0; i < comparableArray.length - 1; i++) { // array is sorted in ascending order iif element i is less or equal than element i+1 if (comparableArray[i].compareTo(comparableArray[i + 1]) > 0) throw failures.failure(info, shouldBeSorted(i, array)); } } catch (ClassCastException e) { // elements are either not Comparable or not mutually Comparable (e.g. array with String and Integer) throw failures.failure(info, shouldHaveMutuallyComparableElements(array)); } }
void assertIsSorted(AssertionInfo info, Failures failures, Object array) { assertNotNull(info, array); if (comparisonStrategy instanceof ComparatorBasedComparisonStrategy) { // instead of comparing array elements with their natural comparator, use the one set by client. Comparator<?> comparator = ((ComparatorBasedComparisonStrategy) comparisonStrategy).getComparator(); assertIsSortedAccordingToComparator(info, failures, array, comparator); return; } // empty arrays are considered sorted even if component type is not sortable. if (sizeOf(array) == 0) return; assertThatArrayComponentTypeIsSortable(info, failures, array); try { // sorted assertion is only relevant if array elements are Comparable // => we should be able to build a Comparable array Comparable<Object>[] comparableArray = arrayOfComparableItems(array); // array with 0 or 1 element are considered sorted. if (comparableArray.length <= 1) return; for (int i = 0; i < comparableArray.length - 1; i++) { // array is sorted in ascending order iif element i is less or equal than element i+1 if (comparableArray[i].compareTo(comparableArray[i + 1]) > 0) throw failures.failure(info, shouldBeSorted(i, array)); } } catch (ClassCastException e) { // elements are either not Comparable or not mutually Comparable (e.g. array with String and Integer) throw failures.failure(info, shouldHaveMutuallyComparableElements(array)); } }