Package org.assertj.core.internal
Class ComparatorBasedComparisonStrategy
- java.lang.Object
-
- org.assertj.core.internal.AbstractComparisonStrategy
-
- org.assertj.core.internal.ComparatorBasedComparisonStrategy
-
- All Implemented Interfaces:
ComparisonStrategy
public class ComparatorBasedComparisonStrategy extends AbstractComparisonStrategy
ImplementsComparisonStrategy
contract with a comparison strategy based on aComparator
.- Author:
- Joel Costigliola
-
-
Field Summary
Fields Modifier and Type Field Description private Comparator
comparator
private String
comparatorDescription
(package private) static int
NOT_EQUAL
-
Constructor Summary
Constructors Constructor Description ComparatorBasedComparisonStrategy(Comparator comparator)
Creates a new
specifying the comparison strategy with given comparator.ComparatorBasedComparisonStrategy
ComparatorBasedComparisonStrategy(Comparator comparator, String comparatorDescription)
Creates a new
specifying the comparison strategy with given comparator and comparator descriptionComparatorBasedComparisonStrategy
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description boolean
areEqual(Object actual, Object other)
Returns true if actual and other are equal according tocomparator
, false otherwise.
Handles the cases where one of the parameter is null so that internalcomparator
does not have too.String
asText()
Iterable<?>
duplicatesFrom(Iterable<?> iterable)
Returns any duplicate elements from the givenIterable
according tocomparator
.Comparator<?>
getComparator()
String
getComparatorDescription()
boolean
isGreaterThan(Object actual, Object other)
Returns true if actual is greater than other, false otherwise.boolean
isStandard()
Return true if comparison strategy is default/standard, false otherwiseboolean
iterableContains(Iterable<?> iterable, Object value)
Returns true if givenIterable
contains given value according tocomparator
, false otherwise.
If givenIterable
is null or empty, return false.void
iterableRemoves(Iterable<?> iterable, Object value)
Look for given value in givenIterable
according to theComparator
, if value is found it is removed from it.
Does nothing if givenIterable
is null (meaning no exception thrown).void
iterablesRemoveFirst(Iterable<?> iterable, Object value)
Removes the first value initerable
that matches thevalue
according to the implemented comparison strategy.protected Set<Object>
newSetUsingComparisonStrategy()
Returns aSet
honoring the comparison strategy used.boolean
stringContains(String string, String sequence)
Returns true if given string contains given sequence according to the implemented comparison strategy, false otherwise.boolean
stringEndsWith(String string, String suffix)
Returns true if string ends with suffix according to the implemented comparison strategy, false otherwise.boolean
stringStartsWith(String string, String prefix)
Returns true if string starts with prefix according to the implemented comparison strategy, false otherwise.String
toString()
-
Methods inherited from class org.assertj.core.internal.AbstractComparisonStrategy
arrayContains, isGreaterThanOrEqualTo, isLessThan, isLessThanOrEqualTo
-
-
-
-
Field Detail
-
NOT_EQUAL
static final int NOT_EQUAL
- See Also:
- Constant Field Values
-
comparator
private final Comparator comparator
-
comparatorDescription
private final String comparatorDescription
-
-
Constructor Detail
-
ComparatorBasedComparisonStrategy
public ComparatorBasedComparisonStrategy(Comparator comparator)
Creates a new
specifying the comparison strategy with given comparator.ComparatorBasedComparisonStrategy
- Parameters:
comparator
- the comparison strategy to use.
-
ComparatorBasedComparisonStrategy
public ComparatorBasedComparisonStrategy(Comparator comparator, String comparatorDescription)
Creates a new
specifying the comparison strategy with given comparator and comparator descriptionComparatorBasedComparisonStrategy
- Parameters:
comparator
- the comparison strategy to use.comparatorDescription
- the comparator description to use in assertion messages.
-
-
Method Detail
-
iterableContains
public boolean iterableContains(Iterable<?> iterable, Object value)
Returns true if givenIterable
contains given value according tocomparator
, false otherwise.
If givenIterable
is null or empty, return false.- Parameters:
iterable
- theIterable
to search value invalue
- the object to look for in givenIterable
- Returns:
- true if given
Iterable
contains given value according tocomparator
, false otherwise.
-
iterableRemoves
public void iterableRemoves(Iterable<?> iterable, Object value)
Look for given value in givenIterable
according to theComparator
, if value is found it is removed from it.
Does nothing if givenIterable
is null (meaning no exception thrown).
-
iterablesRemoveFirst
public void iterablesRemoveFirst(Iterable<?> iterable, Object value)
Description copied from interface:ComparisonStrategy
Removes the first value initerable
that matches thevalue
according to the implemented comparison strategy. If givenIterable
is null, does nothing.
-
areEqual
public boolean areEqual(Object actual, Object other)
Returns true if actual and other are equal according tocomparator
, false otherwise.
Handles the cases where one of the parameter is null so that internalcomparator
does not have too.- Parameters:
actual
- the object to compare to otherother
- the object to compare to actual- Returns:
- true if actual and other are equal according to
comparator
, false otherwise.
-
duplicatesFrom
public Iterable<?> duplicatesFrom(Iterable<?> iterable)
Returns any duplicate elements from the givenIterable
according tocomparator
.- Specified by:
duplicatesFrom
in interfaceComparisonStrategy
- Overrides:
duplicatesFrom
in classAbstractComparisonStrategy
- Parameters:
iterable
- the givenIterable
we want to extract duplicate elements.- Returns:
- an
Iterable
containing the duplicate elements of the given one. If no duplicates are found, an emptyIterable
is returned.
-
newSetUsingComparisonStrategy
protected Set<Object> newSetUsingComparisonStrategy()
Description copied from class:AbstractComparisonStrategy
Returns aSet
honoring the comparison strategy used.- Specified by:
newSetUsingComparisonStrategy
in classAbstractComparisonStrategy
- Returns:
- a
Set
honoring the comparison strategy used.
-
asText
public String asText()
- Specified by:
asText
in classAbstractComparisonStrategy
-
getComparator
public Comparator<?> getComparator()
-
getComparatorDescription
public String getComparatorDescription()
-
stringStartsWith
public boolean stringStartsWith(String string, String prefix)
Description copied from interface:ComparisonStrategy
Returns true if string starts with prefix according to the implemented comparison strategy, false otherwise.- Parameters:
string
- the String we want to look starting prefixprefix
- the prefix String to look for at string's start- Returns:
- true if string starts with prefix according to the implemented comparison strategy, false otherwise.
-
stringEndsWith
public boolean stringEndsWith(String string, String suffix)
Description copied from interface:ComparisonStrategy
Returns true if string ends with suffix according to the implemented comparison strategy, false otherwise.- Parameters:
string
- the String we want to look starting suffixsuffix
- the suffix String to look for at string's end- Returns:
- true if string ends with suffix according to the implemented comparison strategy, false otherwise.
-
stringContains
public boolean stringContains(String string, String sequence)
Description copied from interface:ComparisonStrategy
Returns true if given string contains given sequence according to the implemented comparison strategy, false otherwise.- Parameters:
string
- the string to search sequence in (must not be null)sequence
- the String to look for in given string- Returns:
- true if given string contains given sequence according to the implemented comparison strategy, false otherwise.
-
isGreaterThan
public boolean isGreaterThan(Object actual, Object other)
Description copied from interface:ComparisonStrategy
Returns true if actual is greater than other, false otherwise.- Parameters:
actual
- the object to compare to otherother
- the object to compare to actual- Returns:
- true if actual is greater than other, false otherwise.
-
isStandard
public boolean isStandard()
Description copied from interface:ComparisonStrategy
Return true if comparison strategy is default/standard, false otherwise- Specified by:
isStandard
in interfaceComparisonStrategy
- Overrides:
isStandard
in classAbstractComparisonStrategy
- Returns:
- true if comparison strategy is default/standard, false otherwise
-
-