Skip to content

Insensitive Comparator

plrigaux edited this page May 20, 2016 · 12 revisions

Insensitive Comparator

The insensitive comparator is called insensitive because it can compare strings while not being sensitive to chosen characters. The original intention was to be insensitive to white spaces, but we felt fair to extends this capacity to all chosen characters.

The comparator leverage the Guava's CharMatcher to perform the character matching.

Comparator<CharSequence> insensitiveComparator = InsensitiveComparator.onWhiteSpace().trim();

#Base Factories

Space insensitive

onAllWhiteSpace()

The comparator doesn't take in account any whitespaces in the string.

InsensitiveComparator.onAllWhiteSpace()
//Gives
" a b c " < "Doc  5.doc" == "Doc 5.doc" < "Doc5.doc" < "a b c" == "a  b   c" < "abc"

Note: we use the same whitespace definition as Guava which refer to the latest Unicode standard, as illustrated here. This is not the same definition used by Java APIs.

Space repetition insensitive

onAllWhiteSpace()

The comparator collapse series of continuous white spaces and treat then as one space.

InsensitiveComparator.onAllWhiteSpace()
//Gives
"Doc  5.doc" == "Doc 5.doc" == "Doc5.doc" < "abc" == "a b c" == "a  b   c" == " a b c "

Insensitive to selected characters

onAll(char toIgnore)
onAll(CharMatcher toIgnore)

The comparator can be insensitive to all selected characters.

Insensitive to characters repetitions

onRepetition(char toIgnore)
onRepetition(CharMatcher ignoreOn)

The comparator can be insensitive to selected characters repetitions.

Not Insensitive

onNothing()

Provide a normal ASCII comparator. Useful for to apply the modifiers.

Modifiers

##Space trimming

leftTrim() rightTrim() trim()

The comparator can ignore white spaces at the beginning with leftTrim(), at the end with rightTrim() or at both ends of the string with trim().

Note: we use the same whitespace definition as Guava which refer to the latest Unicode standard, as illustrated here. This is not the same definition used by Java.

Example:

InsensitiveComparator.onNothing().leftTrim()
//Gives: 
" Doc5.doc" < " Doc5.doc " == "Doc5.doc " 
InsensitiveComparator.onNothing().rightTrim()
//Gives: 
" Doc5.doc" == " Doc5.doc " < "Doc5.doc "
InsensitiveComparator.onNothing().trim()
//Gives: 
" Doc5.doc" == " Doc5.doc " == "Doc5.doc "

##Characters trimming

leftTrim(char toTrim) rightTrim(char toTrim) trim(char toTrim)
leftTrim(CharMatcher toTrim) rightTrim(CharMatcher toTrim) trim(CharMatcher toTrim)

Trim on selected characters.

Case insensitive

ignoreCase()

The comparator can be adjusted to compare strings ignoring case difference.

Note: that this method does not take locale into account, and will result in an unsatisfactory ordering for certain locale. The java.text package provides collators to allow locale-sensitive ordering.

InsensitiveComparator.ignoreCase()
//Gives
"Albert Einstein" == "albert einstein" < "Max Planck"