-
Notifications
You must be signed in to change notification settings - Fork 0
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
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.
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 "onAll(char toIgnore)
onAll(CharMatcher toIgnore)The comparator can be insensitive to all selected characters.
onRepetition(char toIgnore)
onRepetition(CharMatcher ignoreOn)The comparator can be insensitive to selected characters repetitions.
onNothing()Provide a normal ASCII comparator. Useful for to apply the 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.
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"Copyright (C) 2016 by Pier Rigaux plrigaux@gmail.com. Under Apache 2.0 licence.