Top Description Fields Constructors Methods
org.assertj.core.internal

public Class Comparables

extends Object
Class Inheritance
Known Direct Subclasses
org.assertj.core.internal.Characters, org.assertj.core.internal.Numbers
Static Imports
java.lang.String.format, java.util.Objects.hash, .Objects.requireNonNull, org.assertj.core.error.ShouldBeBetween.shouldBeBetween, .ShouldBeEqual.shouldBeEqual, .ShouldNotBeEqual.shouldNotBeEqual, org.assertj.core.util.Preconditions.checkArgument

Reusable assertions for types that can be compared between each other, most of the time it means Comparables but also for type not implementing Comparable like Number (surprisingly not comparable).
Authors
Alex Ruiz, Joel Costigliola

Field Summary

Modifier and TypeField and Description
private final ComparisonStrategy
pack-priv Failures

Constructor Summary

AccessConstructor and Description
public
public
Comparables(ComparisonStrategy comparisonStrategy)

Method Summary

Modifier and TypeMethod and Description
protected <T> boolean
areEqual(T actual, T expected)

public <
the type of actual and expected
T
>
void
assertEqual(AssertionInfo
contains information about the assertion.
info
,
T
the actual value.
actual
,
T
the expected value.
expected
)

Asserts that two T instances are equal.

public <
used to guarantee that two objects of the same type are being compared against each other.
T
>
void
assertEqualByComparison(AssertionInfo
contains information about the assertion.
info
,
Comparable<? super T>
the actual value.
actual
,
T
the expected value.
expected
)

Asserts that two Comparables are equal by invoking Comparable#compareTo(Object).

public <T> void
assertGreaterThan(AssertionInfo info, Comparable<? super T> actual, T other)

private <
used to guarantee that two objects of the same type are being compared against each other.
T
>
void
assertGreaterThan(AssertionInfo
contains information about the assertion.
info
,
Comparable<? super T>
the actual value.
actual
,
T
the value to compare the actual value to.
other
,
TriFunction<Comparable<? super T>, T, ComparisonStrategy, ErrorMessageFactory>
the desired error message factory to generate the suitable error message
errorMessageFactory
)

Asserts that the actual value is greater than the other one.

public <T> void
assertGreaterThanOrEqualTo(AssertionInfo info, Comparable<? super T> actual, T other)

private <
used to guarantee that two objects of the same type are being compared against each other.
T
>
void
assertGreaterThanOrEqualTo(AssertionInfo
contains information about the assertion.
info
,
Comparable<? super T>
the actual value.
actual
,
T
the value to compare the actual value to.
other
,
TriFunction<Comparable<? super T>, T, ComparisonStrategy, ErrorMessageFactory>
the desired error message factory to generate the suitable error message
errorMessageFactory
)

Asserts that the actual value is greater than or equal to the other one.

public <T> void
assertIsAfter(AssertionInfo info, Comparable<? super T> actual, T other)

public <T> void
assertIsAfterOrEqualTo(AssertionInfo info, Comparable<? super T> actual, T other)

public <
used to guarantee that two objects of the same type are being compared against each other.
T
>
void
assertIsBefore(AssertionInfo
contains information about the assertion.
info
,
Comparable<? super T>
the actual value.
actual
,
T
the value to compare the actual value to.
other
)

Asserts that the actual value is strictly before the other one.

public <T> void
assertIsBeforeOrEqualTo(AssertionInfo info, Comparable<? super T> actual, T other)

public <
used to guarantee that two objects of the same type are being compared against each other.
T
>
void
assertIsBetween(AssertionInfo
contains information about the assertion.
info
,
Comparable<? super T>
the actual value.
actual
,
T
the start value.
start
,
T
the end value.
end
,
boolean
if start is inclusive (fail is actual == start and inclusiveStart is false).
inclusiveStart
,
boolean
if end is inclusive (fail is actual == end and inclusiveEnd is false).
inclusiveEnd
)

Asserts that the actual value is between start and end, inclusive or not.

public <
used to guarantee that two objects of the same type are being compared against each other.
T
>
void
assertLessThan(AssertionInfo
contains information about the assertion.
info
,
Comparable<? super T>
the actual value.
actual
,
T
the value to compare the actual value to.
other
)

Asserts that the actual value is less than the other one.

private <
used to guarantee that two objects of the same type are being compared against each other.
T
>
void
assertLessThan(AssertionInfo
contains information about the assertion.
info
,
Comparable<? super T>
the actual value.
actual
,
T
the value to compare the actual value to.
other
,
TriFunction<Comparable<? super T>, T, ComparisonStrategy, ErrorMessageFactory>
the desired error message factory to generate the suitable error message
errorMessageFactory
)

Asserts that the actual value is less than the other one and throws an error with the given message factory.

public <T> void
assertLessThanOrEqualTo(AssertionInfo info, Comparable<? super T> actual, T other)

private <
used to guarantee that two objects of the same type are being compared against each other.
T
>
void
assertLessThanOrEqualTo(AssertionInfo
contains information about the assertion.
info
,
Comparable<? super T>
the actual value.
actual
,
T
the value to compare the actual value to.
other
,
TriFunction<Comparable<? super T>, T, ComparisonStrategy, ErrorMessageFactory>
the desired error message factory to generate the suitable error message
errorMessageFactory
)

Asserts that the actual value is less than or equal to the other one.

public <
the type of actual and expected
T
>
void
assertNotEqual(AssertionInfo
contains information about the assertion.
info
,
T
the actual value.
actual
,
T
the value to compare the actual value to.
other
)

Asserts that two T instances are not equal.

public <
used to guarantee that two objects of the same type are being compared against each other.
T
>
void
assertNotEqualByComparison(AssertionInfo
contains information about the assertion.
info
,
Comparable<? super T>
the actual value.
actual
,
T
the value to compare the actual value to.
other
)

Asserts that two Comparables are not equal by invoking Comparable#compareTo(Object) .

protected static <T> void
assertNotNull(AssertionInfo info, T actual)

private <T> void
checkBoundsValidity(T start, T end, boolean inclusiveStart, boolean inclusiveEnd)

public boolean
equals(Object
the reference object with which to compare.
obj
)

Overrides java.lang.Object.equals.

Indicates whether some other object is "equal to" this one.
public Comparator<?>
public int
hashCode()

Overrides java.lang.Object.hashCode.

Returns a hash code value for this object.
private boolean
isGreaterThan(Object actual, Object other)

private <T> boolean
isLessThan(Object actual, Object other)

pack-priv void
pack-priv void
public String
toString()

Overrides java.lang.Object.toString.

Returns a string representation of the object.
Inherited from java.lang.Object:
clonefinalizegetClassnotifynotifyAllwaitwaitwait

Field Detail

comparisonStrategyback to summary
private final ComparisonStrategy comparisonStrategy
failuresback to summary
pack-priv Failures failures
Annotations
@VisibleForTesting

Constructor Detail

Comparablesback to summary
public Comparables()

Build a Comparables using a StandardComparisonStrategy.

Annotations
@VisibleForTesting
Comparablesback to summary
public Comparables(ComparisonStrategy comparisonStrategy)

Method Detail

areEqualback to summary
protected <T> boolean areEqual(T actual, T expected)
assertEqualback to summary
public <T> void assertEqual(AssertionInfo info, T actual, T expected)

Asserts that two T instances are equal.

Parameters
<T>
the type of actual and expected
info:AssertionInfo

contains information about the assertion.

actual:T

the actual value.

expected:T

the expected value.

Exceptions
AssertionError:
  • if the actual value is null.
  • if the actual value is not equal to the expected one. This method will throw a org.junit.ComparisonFailure instead if JUnit is in the classpath and the expected and actual values are not equal.
assertEqualByComparisonback to summary
public <T> void assertEqualByComparison(AssertionInfo info, Comparable<? super T> actual, T expected)

Asserts that two Comparables are equal by invoking Comparable#compareTo(Object).
Note that it does not rely on the custom comparisonStrategy if one has been set.

Parameters
<T>
used to guarantee that two objects of the same type are being compared against each other.
info:AssertionInfo

contains information about the assertion.

actual:Comparable<? super T>

the actual value.

expected:T

the expected value.

Exceptions
AssertionError:
  • if the actual value is null.
  • if the actual value is not equal to the expected one. This method will throw a org.junit.ComparisonFailure instead if JUnit is in the classpath and the expected and actual values are not equal.
assertGreaterThanback to summary
public <T> void assertGreaterThan(AssertionInfo info, Comparable<? super T> actual, T other)
assertGreaterThanback to summary
private <T> void assertGreaterThan(AssertionInfo info, Comparable<? super T> actual, T other, TriFunction<Comparable<? super T>, T, ComparisonStrategy, ErrorMessageFactory> errorMessageFactory)

Asserts that the actual value is greater than the other one.

Parameters
<T>
used to guarantee that two objects of the same type are being compared against each other.
info:AssertionInfo

contains information about the assertion.

actual:Comparable<? super T>

the actual value.

other:T

the value to compare the actual value to.

errorMessageFactory:TriFunction<Comparable<? super T>, T, ComparisonStrategy, ErrorMessageFactory>

the desired error message factory to generate the suitable error message

Exceptions
AssertionError:
  • if the actual value is null.
  • if the actual value is not greater than the other one: this assertion will fail if the actual value is equal to or less than the other value.
assertGreaterThanOrEqualToback to summary
public <T> void assertGreaterThanOrEqualTo(AssertionInfo info, Comparable<? super T> actual, T other)
assertGreaterThanOrEqualToback to summary
private <T> void assertGreaterThanOrEqualTo(AssertionInfo info, Comparable<? super T> actual, T other, TriFunction<Comparable<? super T>, T, ComparisonStrategy, ErrorMessageFactory> errorMessageFactory)

Asserts that the actual value is greater than or equal to the other one.

Parameters
<T>
used to guarantee that two objects of the same type are being compared against each other.
info:AssertionInfo

contains information about the assertion.

actual:Comparable<? super T>

the actual value.

other:T

the value to compare the actual value to.

errorMessageFactory:TriFunction<Comparable<? super T>, T, ComparisonStrategy, ErrorMessageFactory>

the desired error message factory to generate the suitable error message

Exceptions
AssertionError:
  • if the actual value is null.
  • if the actual value is less than the other one.
assertIsAfterback to summary
public <T> void assertIsAfter(AssertionInfo info, Comparable<? super T> actual, T other)
assertIsAfterOrEqualToback to summary
public <T> void assertIsAfterOrEqualTo(AssertionInfo info, Comparable<? super T> actual, T other)
assertIsBeforeback to summary
public <T> void assertIsBefore(AssertionInfo info, Comparable<? super T> actual, T other)

Asserts that the actual value is strictly before the other one.

Parameters
<T>
used to guarantee that two objects of the same type are being compared against each other.
info:AssertionInfo

contains information about the assertion.

actual:Comparable<? super T>

the actual value.

other:T

the value to compare the actual value to.

Exceptions
AssertionError:
  • if the actual value is null.
  • if the actual value is not before the other one: this assertion will fail if the actual value is equal to or greater than the other value.
assertIsBeforeOrEqualToback to summary
public <T> void assertIsBeforeOrEqualTo(AssertionInfo info, Comparable<? super T> actual, T other)
assertIsBetweenback to summary
public <T> void assertIsBetween(AssertionInfo info, Comparable<? super T> actual, T start, T end, boolean inclusiveStart, boolean inclusiveEnd)

Asserts that the actual value is between start and end, inclusive or not.

Parameters
<T>
used to guarantee that two objects of the same type are being compared against each other.
info:AssertionInfo

contains information about the assertion.

actual:Comparable<? super T>

the actual value.

start:T

the start value.

end:T

the end value.

inclusiveStart:boolean

if start is inclusive (fail is actual == start and inclusiveStart is false).

inclusiveEnd:boolean

if end is inclusive (fail is actual == end and inclusiveEnd is false).

Exceptions
AssertionError:
  • if the actual value is null.
  • if the actual value is not between start and end.
NullPointerException:
  • if start value is null.
  • if end value is null.
IllegalArgumentException:
if end value is less than start value.
assertLessThanback to summary
public <T> void assertLessThan(AssertionInfo info, Comparable<? super T> actual, T other)

Asserts that the actual value is less than the other one.

Parameters
<T>
used to guarantee that two objects of the same type are being compared against each other.
info:AssertionInfo

contains information about the assertion.

actual:Comparable<? super T>

the actual value.

other:T

the value to compare the actual value to.

Exceptions
AssertionError:
  • if the actual value is null.
  • if the actual value is not less than the other one: this assertion will fail if the actual value is equal to or greater than the other value.
assertLessThanback to summary
private <T> void assertLessThan(AssertionInfo info, Comparable<? super T> actual, T other, TriFunction<Comparable<? super T>, T, ComparisonStrategy, ErrorMessageFactory> errorMessageFactory)

Asserts that the actual value is less than the other one and throws an error with the given message factory.

Parameters
<T>
used to guarantee that two objects of the same type are being compared against each other.
info:AssertionInfo

contains information about the assertion.

actual:Comparable<? super T>

the actual value.

other:T

the value to compare the actual value to.

errorMessageFactory:TriFunction<Comparable<? super T>, T, ComparisonStrategy, ErrorMessageFactory>

the desired error message factory to generate the suitable error message

Exceptions
AssertionError:
  • if the actual value is null.
  • if the actual value is not before the other one: this assertion will fail if the actual value is equal to or after the other value.
assertLessThanOrEqualToback to summary
public <T> void assertLessThanOrEqualTo(AssertionInfo info, Comparable<? super T> actual, T other)
assertLessThanOrEqualToback to summary
private <T> void assertLessThanOrEqualTo(AssertionInfo info, Comparable<? super T> actual, T other, TriFunction<Comparable<? super T>, T, ComparisonStrategy, ErrorMessageFactory> errorMessageFactory)

Asserts that the actual value is less than or equal to the other one.

Parameters
<T>
used to guarantee that two objects of the same type are being compared against each other.
info:AssertionInfo

contains information about the assertion.

actual:Comparable<? super T>

the actual value.

other:T

the value to compare the actual value to.

errorMessageFactory:TriFunction<Comparable<? super T>, T, ComparisonStrategy, ErrorMessageFactory>

the desired error message factory to generate the suitable error message

Exceptions
AssertionError:
  • if the actual value is null.
  • if the actual value is greater than the other one.
assertNotEqualback to summary
public <T> void assertNotEqual(AssertionInfo info, T actual, T other)

Asserts that two T instances are not equal.

Parameters
<T>
the type of actual and expected
info:AssertionInfo

contains information about the assertion.

actual:T

the actual value.

other:T

the value to compare the actual value to.

Exceptions
AssertionError:
  • if the actual value is null.
  • if the actual value is equal to the other one.
assertNotEqualByComparisonback to summary
public <T> void assertNotEqualByComparison(AssertionInfo info, Comparable<? super T> actual, T other)

Asserts that two Comparables are not equal by invoking Comparable#compareTo(Object) .
Note that it does not rely on the custom comparisonStrategy if one has been set.

Parameters
<T>
used to guarantee that two objects of the same type are being compared against each other.
info:AssertionInfo

contains information about the assertion.

actual:Comparable<? super T>

the actual value.

other:T

the value to compare the actual value to.

Exceptions
AssertionError:
  • if the actual value is null.
  • if the actual value is equal to the other one.
assertNotNullback to summary
protected static <T> void assertNotNull(AssertionInfo info, T actual)
checkBoundsValidityback to summary
private <T> void checkBoundsValidity(T start, T end, boolean inclusiveStart, boolean inclusiveEnd)
equalsback to summary
public boolean equals(Object obj)

Overrides java.lang.Object.equals.

Doc from java.lang.Object.equals.

Indicates whether some other object is "equal to" this one.

The equals method implements an equivalence relation on non-null object references:

  • It is reflexive: for any non-null reference value x, x.equals(x) should return true.
  • It is symmetric: for any non-null reference values x and y, x.equals(y) should return true if and only if y.equals(x) returns true.
  • It is transitive: for any non-null reference values x, y, and z, if x.equals(y) returns true and y.equals(z) returns true, then x.equals(z) should return true.
  • It is consistent: for any non-null reference values x and y, multiple invocations of x.equals(y) consistently return true or consistently return false, provided no information used in equals comparisons on the objects is modified.
  • For any non-null reference value x, x.equals(null) should return false.

An equivalence relation partitions the elements it operates on into equivalence classes; all the members of an equivalence class are equal to each other. Members of an equivalence class are substitutable for each other, at least for some purposes.

Parameters
obj:Object

the reference object with which to compare.

Returns:boolean

true if this object is the same as the obj argument; false otherwise.

Annotations
@Override
getComparatorback to summary
public Comparator<?> getComparator()
Annotations
@VisibleForTesting
hashCodeback to summary
public int hashCode()

Overrides java.lang.Object.hashCode.

Doc from java.lang.Object.hashCode.

Returns a hash code value for this object. This method is supported for the benefit of hash tables such as those provided by java.util.HashMap.

The general contract of hashCode is:

  • Whenever it is invoked on the same object more than once during an execution of a Java application, the hashCode method must consistently return the same integer, provided no information used in equals comparisons on the object is modified. This integer need not remain consistent from one execution of an application to another execution of the same application.
  • If two objects are equal according to the equals method, then calling the hashCode method on each of the two objects must produce the same integer result.
  • It is not required that if two objects are unequal according to the equals method, then calling the hashCode method on each of the two objects must produce distinct integer results. However, the programmer should be aware that producing distinct integer results for unequal objects may improve the performance of hash tables.
Returns:int

a hash code value for this object

Annotations
@Override
isGreaterThanback to summary
private boolean isGreaterThan(Object actual, Object other)
isLessThanback to summary
private <T> boolean isLessThan(Object actual, Object other)
resetFailuresback to summary
pack-priv void resetFailures()
Annotations
@VisibleForTesting
setFailuresback to summary
pack-priv void setFailures(Failures failures)
Annotations
@VisibleForTesting
toStringback to summary
public String toString()

Overrides java.lang.Object.toString.

Doc from java.lang.Object.toString.

Returns a string representation of the object.

Returns:String

a string representation of the object

Annotations
@Override