com.fitbur.assertj.api.ArraySortedAssert Maven / Gradle / Ivy
/**
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*
* Copyright 2012-2016 the original author or authors.
*/
package com.fitbur.assertj.api;
import java.util.Comparator;
/**
* Assertions applicable to primitive arrays or arrays of elements either naturally {@link Comparable} or according to a given
* {@link Comparator}.
*
* Note that the contract defined here is can't be totally applied to List (that's why its name is not SortedAssert), the
* differences being that we can't check that - for empty List - the list parameter is comparable or compatible with given
* comparator due to type erasure.
*
* @param the "self" type of this assertion class that must be a array type (e.g. arrays, collections).
* Please read "Emulating 'self types' using Java Generics to
* simplify fluent API implementation" for more details.
* @param the array element type.
*
* @author Joel Costigliola
* @author Mikhail Mazursky
*/
public interface ArraySortedAssert, E> {
/**
* Verifies that the actual array is sorted into ascending order according to the natural ordering of its elements.
*
* All array elements must be primitive or implement the {@link Comparable} interface and must be mutually comparable (that is,
* e1.compareTo(e2) must not throw a ClassCastException for any elements e1 and e2 in the array), examples :
*
* - a array composed of {2, 4, 6} is ok because the element type is a primitive type.
* - a array composed of {"a1", "a2", "a3"} is ok because the element type (String) is Comparable
* - a array composed of Rectangle {r1, r2, r3} is NOT ok because Rectangle is not Comparable
* - a array composed of {True, "abc", False} is NOT ok because elements are not mutually comparable (even though each
* element type implements Comparable)
*
* Empty or one element arrays are considered sorted (unless the array element type is not Comparable).
*
* @return {@code this} assertion object.
*
* @throws AssertionError if the actual array is not sorted into ascending order according to the natural ordering of its
* elements.
* @throws AssertionError if the actual array is null
.
* @throws AssertionError if the actual array element type does not implement {@link Comparable}.
* @throws AssertionError if the actual array elements are not mutually {@link Comparable}.
*/
S isSorted();
/**
* Verifies that the actual array is sorted according to the given comparator. Empty arrays are considered sorted whatever
* the comparator is. One element arrays are considered sorted if element is compatible with comparator, otherwise an
* AssertionError is thrown.
*
* @param comparator the {@link Comparator} used to compare array elements
*
* @return {@code this} assertion object.
*
* @throws AssertionError if the actual array is not sorted according to the given comparator.
* @throws AssertionError if the actual array is null
.
* @throws NullPointerException if the given comparator is null
.
* @throws AssertionError if the actual array elements are not mutually comparable according to given Comparator.
*/
S isSortedAccordingTo(Comparator super E> comparator);
}