|
|
|
@ -10,6 +10,7 @@ import java.util.concurrent.LinkedBlockingQueue;
|
|
|
|
|
import net.yacy.cora.ranking.ClusteredScoreMap;
|
|
|
|
|
import net.yacy.cora.ranking.ReversibleScoreMap;
|
|
|
|
|
import net.yacy.document.LibraryProvider;
|
|
|
|
|
import net.yacy.document.StringBuilderComparator;
|
|
|
|
|
import net.yacy.kelondro.data.word.Word;
|
|
|
|
|
import net.yacy.kelondro.data.word.WordReference;
|
|
|
|
|
import net.yacy.kelondro.logging.Log;
|
|
|
|
@ -53,17 +54,17 @@ public class DidYouMean {
|
|
|
|
|
private static final char[][] ALPHABETS = {ALPHABET_LATIN, ALPHABET_KANJI};
|
|
|
|
|
private static char[] alphabet = ALPHABET_LATIN;
|
|
|
|
|
|
|
|
|
|
private static final String POISON_STRING = "\n";
|
|
|
|
|
private static final StringBuilder POISON_STRING = new StringBuilder("\n");
|
|
|
|
|
public static final int AVAILABLE_CPU = Runtime.getRuntime().availableProcessors();
|
|
|
|
|
private static final wordLengthComparator WORD_LENGTH_COMPARATOR = new wordLengthComparator();
|
|
|
|
|
|
|
|
|
|
private final IndexCell<WordReference> index;
|
|
|
|
|
private final String word;
|
|
|
|
|
private final StringBuilder word;
|
|
|
|
|
private final int wordLen;
|
|
|
|
|
private final LinkedBlockingQueue<String> guessGen, guessLib;
|
|
|
|
|
private final LinkedBlockingQueue<StringBuilder> guessGen, guessLib;
|
|
|
|
|
private long timeLimit;
|
|
|
|
|
private boolean createGen; // keeps the value 'true' as long as no entry in guessLib is written
|
|
|
|
|
private final SortedSet<String> resultSet;
|
|
|
|
|
private final SortedSet<StringBuilder> resultSet;
|
|
|
|
|
private final indexSizeComparator INDEX_SIZE_COMPARATOR;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@ -71,21 +72,21 @@ public class DidYouMean {
|
|
|
|
|
* @param index a termIndex - most likely retrieved from a switchboard object.
|
|
|
|
|
* @param sort true/false - sorts the resulting TreeSet by index.count(); <b>Warning:</b> this causes heavy i/o.
|
|
|
|
|
*/
|
|
|
|
|
public DidYouMean(final IndexCell<WordReference> index, final String word0) {
|
|
|
|
|
this.resultSet = Collections.synchronizedSortedSet(new TreeSet<String>(new headMatchingComparator(word0, WORD_LENGTH_COMPARATOR)));
|
|
|
|
|
this.word = word0.toLowerCase();
|
|
|
|
|
this.wordLen = word.length();
|
|
|
|
|
public DidYouMean(final IndexCell<WordReference> index, final StringBuilder word0) {
|
|
|
|
|
this.resultSet = Collections.synchronizedSortedSet(new TreeSet<StringBuilder>(new headMatchingComparator(word0, WORD_LENGTH_COMPARATOR)));
|
|
|
|
|
this.word = word0;
|
|
|
|
|
this.wordLen = this.word.length();
|
|
|
|
|
this.index = index;
|
|
|
|
|
this.guessGen = new LinkedBlockingQueue<String>();
|
|
|
|
|
this.guessLib = new LinkedBlockingQueue<String>();
|
|
|
|
|
this.guessGen = new LinkedBlockingQueue<StringBuilder>();
|
|
|
|
|
this.guessLib = new LinkedBlockingQueue<StringBuilder>();
|
|
|
|
|
this.createGen = true;
|
|
|
|
|
this.INDEX_SIZE_COMPARATOR = new indexSizeComparator();
|
|
|
|
|
|
|
|
|
|
// identify language
|
|
|
|
|
if (this.word.length() > 0) {
|
|
|
|
|
char testchar = this.word.charAt(0);
|
|
|
|
|
final char testchar = this.word.charAt(0);
|
|
|
|
|
boolean alphafound = false;
|
|
|
|
|
alphatest: for (char[] alpha: ALPHABETS) {
|
|
|
|
|
alphatest: for (final char[] alpha: ALPHABETS) {
|
|
|
|
|
if (isAlphabet(alpha, testchar)) {
|
|
|
|
|
alphabet = alpha;
|
|
|
|
|
alphafound = true;
|
|
|
|
@ -94,8 +95,8 @@ public class DidYouMean {
|
|
|
|
|
}
|
|
|
|
|
if (!alphafound) {
|
|
|
|
|
// generate generic alphabet using simply a character block of 256 characters
|
|
|
|
|
char firstchar = (char) ((0xff & (testchar / 256)) * 256);
|
|
|
|
|
char lastchar = (char) (firstchar + 255);
|
|
|
|
|
final char firstchar = (char) ((0xff & (testchar / 256)) * 256);
|
|
|
|
|
final char lastchar = (char) (firstchar + 255);
|
|
|
|
|
alphabet = new char[256];
|
|
|
|
|
for (char a = firstchar; a <= lastchar; a++) {
|
|
|
|
|
alphabet[0xff & (a - firstchar)] = a;
|
|
|
|
@ -123,29 +124,29 @@ public class DidYouMean {
|
|
|
|
|
* @param preSortSelection the number of words that participate in the IO-intensive sort
|
|
|
|
|
* @return
|
|
|
|
|
*/
|
|
|
|
|
public SortedSet<String> getSuggestions(final long timeout, final int preSortSelection) {
|
|
|
|
|
public SortedSet<StringBuilder> getSuggestions(final long timeout, final int preSortSelection) {
|
|
|
|
|
if (this.word.length() < MinimumInputWordLength) return this.resultSet; // return nothing if input is too short
|
|
|
|
|
final long startTime = System.currentTimeMillis();
|
|
|
|
|
final long timelimit = startTime + timeout;
|
|
|
|
|
if (this.word.indexOf(' ') > 0) return getSuggestions(this.word.split(" "), timeout, preSortSelection, this.index);
|
|
|
|
|
final SortedSet<String> preSorted = getSuggestions(timeout);
|
|
|
|
|
if (StringBuilderComparator.CASE_INSENSITIVE_ORDER.indexOf(this.word, ' ') > 0) return getSuggestions(StringBuilderComparator.CASE_INSENSITIVE_ORDER.split(this.word, ' '), timeout, preSortSelection, this.index);
|
|
|
|
|
final SortedSet<StringBuilder> preSorted = getSuggestions(timeout);
|
|
|
|
|
if (System.currentTimeMillis() > timelimit) {
|
|
|
|
|
Log.logInfo("DidYouMean", "found and returned " + preSorted.size() + " unsorted suggestions (1); execution time: "
|
|
|
|
|
+ (System.currentTimeMillis() - startTime) + "ms");
|
|
|
|
|
return preSorted;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
final ReversibleScoreMap<String> scored = new ClusteredScoreMap<String>();
|
|
|
|
|
for (final String s: preSorted) {
|
|
|
|
|
final ReversibleScoreMap<StringBuilder> scored = new ClusteredScoreMap<StringBuilder>(StringBuilderComparator.CASE_INSENSITIVE_ORDER);
|
|
|
|
|
for (final StringBuilder s: preSorted) {
|
|
|
|
|
if (System.currentTimeMillis() > timelimit) break;
|
|
|
|
|
if (!(scored.sizeSmaller(2 * preSortSelection))) break;
|
|
|
|
|
scored.inc(s, index.count(Word.word2hash(s)));
|
|
|
|
|
scored.inc(s, this.index.count(Word.word2hash(s)));
|
|
|
|
|
}
|
|
|
|
|
final SortedSet<String> countSorted = Collections.synchronizedSortedSet(new TreeSet<String>(new headMatchingComparator(this.word, this.INDEX_SIZE_COMPARATOR)));
|
|
|
|
|
final int wc = index.count(Word.word2hash(this.word)); // all counts must be greater than this
|
|
|
|
|
final SortedSet<StringBuilder> countSorted = Collections.synchronizedSortedSet(new TreeSet<StringBuilder>(new headMatchingComparator(this.word, this.INDEX_SIZE_COMPARATOR)));
|
|
|
|
|
final int wc = this.index.count(Word.word2hash(this.word)); // all counts must be greater than this
|
|
|
|
|
while (!scored.isEmpty() && countSorted.size() < preSortSelection) {
|
|
|
|
|
final String s = scored.getMaxKey();
|
|
|
|
|
int score = scored.delete(s);
|
|
|
|
|
final StringBuilder s = scored.getMaxKey();
|
|
|
|
|
final int score = scored.delete(s);
|
|
|
|
|
if (s.length() >= MinimumOutputWordLength && score > wc) countSorted.add(s);
|
|
|
|
|
if (System.currentTimeMillis() > timelimit) break;
|
|
|
|
|
}
|
|
|
|
@ -170,13 +171,13 @@ public class DidYouMean {
|
|
|
|
|
* @return
|
|
|
|
|
*/
|
|
|
|
|
@SuppressWarnings("unchecked")
|
|
|
|
|
private static SortedSet<String> getSuggestions(final String[] words, final long timeout, final int preSortSelection, final IndexCell<WordReference> index) {
|
|
|
|
|
final SortedSet<String>[] s = new SortedSet[words.length];
|
|
|
|
|
private static SortedSet<StringBuilder> getSuggestions(final StringBuilder[] words, final long timeout, final int preSortSelection, final IndexCell<WordReference> index) {
|
|
|
|
|
final SortedSet<StringBuilder>[] s = new SortedSet[words.length];
|
|
|
|
|
for (int i = 0; i < words.length; i++) {
|
|
|
|
|
s[i] = new DidYouMean(index, words[i]).getSuggestions(timeout / words.length, preSortSelection);
|
|
|
|
|
}
|
|
|
|
|
// make all permutations
|
|
|
|
|
final SortedSet<String> result = new TreeSet<String>();
|
|
|
|
|
final SortedSet<StringBuilder> result = new TreeSet<StringBuilder>(StringBuilderComparator.CASE_INSENSITIVE_ORDER);
|
|
|
|
|
StringBuilder sb;
|
|
|
|
|
for (int i = 0; i < words.length; i++) {
|
|
|
|
|
if (s[i].isEmpty()) continue;
|
|
|
|
@ -185,7 +186,7 @@ public class DidYouMean {
|
|
|
|
|
if (j > 0) sb.append(' ');
|
|
|
|
|
if (i == j) sb.append(s[j].first()); else sb.append(words[j]);
|
|
|
|
|
}
|
|
|
|
|
result.add(sb.toString());
|
|
|
|
|
result.add(sb);
|
|
|
|
|
}
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
@ -196,8 +197,8 @@ public class DidYouMean {
|
|
|
|
|
* @param timeout execution time in ms.
|
|
|
|
|
* @return a Set<String> with word variations contained in term index.
|
|
|
|
|
*/
|
|
|
|
|
private SortedSet<String> getSuggestions(final long timeout) {
|
|
|
|
|
long startTime = System.currentTimeMillis();
|
|
|
|
|
private SortedSet<StringBuilder> getSuggestions(final long timeout) {
|
|
|
|
|
final long startTime = System.currentTimeMillis();
|
|
|
|
|
this.timeLimit = startTime + timeout;
|
|
|
|
|
|
|
|
|
|
// create one consumer thread that checks the guessLib queue
|
|
|
|
@ -208,12 +209,12 @@ public class DidYouMean {
|
|
|
|
|
consumers[0].start();
|
|
|
|
|
|
|
|
|
|
// get a single recommendation for the word without altering the word
|
|
|
|
|
Set<String> libr = LibraryProvider.dymLib.recommend(this.word);
|
|
|
|
|
for (final String t: libr) {
|
|
|
|
|
final Set<StringBuilder> libr = LibraryProvider.dymLib.recommend(this.word);
|
|
|
|
|
for (final StringBuilder t: libr) {
|
|
|
|
|
if (!t.equals(this.word)) try {
|
|
|
|
|
createGen = false;
|
|
|
|
|
guessLib.put(t);
|
|
|
|
|
} catch (InterruptedException e) {}
|
|
|
|
|
this.createGen = false;
|
|
|
|
|
this.guessLib.put(t);
|
|
|
|
|
} catch (final InterruptedException e) {}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// create and start producers
|
|
|
|
@ -236,23 +237,23 @@ public class DidYouMean {
|
|
|
|
|
// now decide which kind of guess is better
|
|
|
|
|
// we take guessLib entries as long as there is any entry in it
|
|
|
|
|
// to see if this is the case, we must wait for termination of the producer
|
|
|
|
|
for (final Thread t: producers) try { t.join(); } catch (InterruptedException e) {}
|
|
|
|
|
for (final Thread t: producers) try { t.join(); } catch (final InterruptedException e) {}
|
|
|
|
|
|
|
|
|
|
// if there is not any entry in guessLib, then transfer all entries from the
|
|
|
|
|
// guessGen to guessLib
|
|
|
|
|
if (createGen) try {
|
|
|
|
|
if (this.createGen) try {
|
|
|
|
|
this.guessGen.put(POISON_STRING);
|
|
|
|
|
String s;
|
|
|
|
|
StringBuilder s;
|
|
|
|
|
while (!(s = this.guessGen.take()).equals(POISON_STRING)) this.guessLib.put(s);
|
|
|
|
|
} catch (InterruptedException e) {}
|
|
|
|
|
} catch (final InterruptedException e) {}
|
|
|
|
|
|
|
|
|
|
// put poison into guessLib to terminate consumers
|
|
|
|
|
for (@SuppressWarnings("unused") final Consumer c: consumers)
|
|
|
|
|
try { guessLib.put(POISON_STRING); } catch (InterruptedException e) {}
|
|
|
|
|
try { this.guessLib.put(POISON_STRING); } catch (final InterruptedException e) {}
|
|
|
|
|
|
|
|
|
|
// wait for termination of consumer
|
|
|
|
|
for (final Consumer c: consumers)
|
|
|
|
|
try { c.join(); } catch (InterruptedException e) {}
|
|
|
|
|
try { c.join(); } catch (final InterruptedException e) {}
|
|
|
|
|
|
|
|
|
|
// we don't want the given word in the result
|
|
|
|
|
this.resultSet.remove(this.word);
|
|
|
|
@ -261,15 +262,15 @@ public class DidYouMean {
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void test(final String s) throws InterruptedException {
|
|
|
|
|
final Set<String> libr = LibraryProvider.dymLib.recommend(s);
|
|
|
|
|
private void test(final StringBuilder s) throws InterruptedException {
|
|
|
|
|
final Set<StringBuilder> libr = LibraryProvider.dymLib.recommend(s);
|
|
|
|
|
libr.addAll(LibraryProvider.geoLoc.recommend(s));
|
|
|
|
|
if (!libr.isEmpty()) createGen = false;
|
|
|
|
|
for (final String t: libr) {
|
|
|
|
|
guessLib.put(t);
|
|
|
|
|
if (!libr.isEmpty()) this.createGen = false;
|
|
|
|
|
for (final StringBuilder t: libr) {
|
|
|
|
|
this.guessLib.put(t);
|
|
|
|
|
}
|
|
|
|
|
if (createGen) {
|
|
|
|
|
guessGen.put(s);
|
|
|
|
|
if (this.createGen) {
|
|
|
|
|
this.guessGen.put(s);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -283,13 +284,16 @@ public class DidYouMean {
|
|
|
|
|
@Override
|
|
|
|
|
public void run() {
|
|
|
|
|
char m;
|
|
|
|
|
for (int i = 0; i < wordLen; i++) try {
|
|
|
|
|
m = word.charAt(i);
|
|
|
|
|
for (char c: alphabet) {
|
|
|
|
|
if (m != c) test(word.substring(0, i) + c + word.substring(i + 1));
|
|
|
|
|
if (System.currentTimeMillis() > timeLimit) return;
|
|
|
|
|
for (int i = 0; i < DidYouMean.this.wordLen; i++) try {
|
|
|
|
|
m = DidYouMean.this.word.charAt(i);
|
|
|
|
|
for (final char c: alphabet) {
|
|
|
|
|
if (m != c) {
|
|
|
|
|
final StringBuilder ts = new StringBuilder(DidYouMean.this.word.length() + 1).append(DidYouMean.this.word.substring(0, i)).append(c).append(DidYouMean.this.word.substring(i + 1));
|
|
|
|
|
test(ts);
|
|
|
|
|
}
|
|
|
|
|
if (System.currentTimeMillis() > DidYouMean.this.timeLimit) return;
|
|
|
|
|
}
|
|
|
|
|
} catch (InterruptedException e) {}
|
|
|
|
|
} catch (final InterruptedException e) {}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -302,10 +306,11 @@ public class DidYouMean {
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void run() {
|
|
|
|
|
for (int i = 0; i < wordLen; i++) try {
|
|
|
|
|
test(word.substring(0, i) + word.substring(i+1));
|
|
|
|
|
if (System.currentTimeMillis() > timeLimit) return;
|
|
|
|
|
} catch (InterruptedException e) {}
|
|
|
|
|
for (int i = 0; i < DidYouMean.this.wordLen; i++) try {
|
|
|
|
|
final StringBuilder ts = new StringBuilder(DidYouMean.this.word.length() + 1).append(DidYouMean.this.word.substring(0, i)).append(DidYouMean.this.word.substring(i + 1));
|
|
|
|
|
test(ts);
|
|
|
|
|
if (System.currentTimeMillis() > DidYouMean.this.timeLimit) return;
|
|
|
|
|
} catch (final InterruptedException e) {}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
@ -319,12 +324,13 @@ public class DidYouMean {
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void run() {
|
|
|
|
|
for (int i = 0; i <= wordLen; i++) try {
|
|
|
|
|
for (int i = 0; i <= DidYouMean.this.wordLen; i++) try {
|
|
|
|
|
for (final char c: alphabet) {
|
|
|
|
|
test(word.substring(0, i) + c + word.substring(i));
|
|
|
|
|
if (System.currentTimeMillis() > timeLimit) return;
|
|
|
|
|
final StringBuilder ts = new StringBuilder(DidYouMean.this.word.length() + 1).append(DidYouMean.this.word.substring(0, i)).append(c).append(DidYouMean.this.word.substring(i));
|
|
|
|
|
test(ts);
|
|
|
|
|
if (System.currentTimeMillis() > DidYouMean.this.timeLimit) return;
|
|
|
|
|
}
|
|
|
|
|
} catch (InterruptedException e) {}
|
|
|
|
|
} catch (final InterruptedException e) {}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -337,10 +343,11 @@ public class DidYouMean {
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void run() {
|
|
|
|
|
for (int i = 0; i < wordLen - 1; i++) try {
|
|
|
|
|
test(word.substring(0, i) + word.charAt(i + 1) + word.charAt(i) + word.substring(i +2));
|
|
|
|
|
if (System.currentTimeMillis() > timeLimit) return;
|
|
|
|
|
} catch (InterruptedException e) {}
|
|
|
|
|
for (int i = 0; i < DidYouMean.this.wordLen - 1; i++) try {
|
|
|
|
|
final StringBuilder ts = new StringBuilder(DidYouMean.this.word.length() + 1).append(DidYouMean.this.word.substring(0, i)).append(DidYouMean.this.word.charAt(i + 1)).append(DidYouMean.this.word.charAt(i)).append(DidYouMean.this.word.substring(i + 2));
|
|
|
|
|
test(ts);
|
|
|
|
|
if (System.currentTimeMillis() > DidYouMean.this.timeLimit) return;
|
|
|
|
|
} catch (final InterruptedException e) {}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
@ -354,13 +361,13 @@ public class DidYouMean {
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void run() {
|
|
|
|
|
String s;
|
|
|
|
|
StringBuilder s;
|
|
|
|
|
try {
|
|
|
|
|
while ((s = guessLib.take()) != POISON_STRING) {
|
|
|
|
|
if (s.length() >= MinimumOutputWordLength && index.has(Word.word2hash(s))) resultSet.add(s);
|
|
|
|
|
if (System.currentTimeMillis() > timeLimit) return;
|
|
|
|
|
while ((s = DidYouMean.this.guessLib.take()) != POISON_STRING) {
|
|
|
|
|
if (s.length() >= MinimumOutputWordLength && DidYouMean.this.index.has(Word.word2hash(s))) DidYouMean.this.resultSet.add(s);
|
|
|
|
|
if (System.currentTimeMillis() > DidYouMean.this.timeLimit) return;
|
|
|
|
|
}
|
|
|
|
|
} catch (InterruptedException e) {}
|
|
|
|
|
} catch (final InterruptedException e) {}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -368,11 +375,11 @@ public class DidYouMean {
|
|
|
|
|
* indexSizeComparator is used by DidYouMean to order terms by index.count()
|
|
|
|
|
* <b>Warning:</b> this causes heavy i/o
|
|
|
|
|
*/
|
|
|
|
|
private class indexSizeComparator implements Comparator<String> {
|
|
|
|
|
private class indexSizeComparator implements Comparator<StringBuilder> {
|
|
|
|
|
|
|
|
|
|
public int compare(final String o1, final String o2) {
|
|
|
|
|
final int i1 = index.count(Word.word2hash(o1));
|
|
|
|
|
final int i2 = index.count(Word.word2hash(o2));
|
|
|
|
|
public int compare(final StringBuilder o1, final StringBuilder o2) {
|
|
|
|
|
final int i1 = DidYouMean.this.index.count(Word.word2hash(o1));
|
|
|
|
|
final int i2 = DidYouMean.this.index.count(Word.word2hash(o2));
|
|
|
|
|
if (i1 == i2) return WORD_LENGTH_COMPARATOR.compare(o1, o2);
|
|
|
|
|
return (i1 < i2) ? 1 : -1; // '<' is correct, because the largest count shall be ordered to be the first position in the result
|
|
|
|
|
}
|
|
|
|
@ -382,12 +389,12 @@ public class DidYouMean {
|
|
|
|
|
* wordLengthComparator is used by DidYouMean to order terms by the term length
|
|
|
|
|
* This is the default order if the indexSizeComparator is not used
|
|
|
|
|
*/
|
|
|
|
|
private static class wordLengthComparator implements Comparator<String> {
|
|
|
|
|
private static class wordLengthComparator implements Comparator<StringBuilder> {
|
|
|
|
|
|
|
|
|
|
public int compare(final String o1, final String o2) {
|
|
|
|
|
public int compare(final StringBuilder o1, final StringBuilder o2) {
|
|
|
|
|
final int i1 = o1.length();
|
|
|
|
|
final int i2 = o2.length();
|
|
|
|
|
if (i1 == i2) return o1.compareTo(o2);
|
|
|
|
|
if (i1 == i2) return StringBuilderComparator.CASE_INSENSITIVE_ORDER.compare(o1, o2);
|
|
|
|
|
return (i1 < i2) ? 1 : -1; // '<' is correct, because the longest word shall be first
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -396,18 +403,18 @@ public class DidYouMean {
|
|
|
|
|
/**
|
|
|
|
|
* headMatchingComparator is used to sort results in such a way that words that match with the given words are sorted first
|
|
|
|
|
*/
|
|
|
|
|
private static class headMatchingComparator implements Comparator<String> {
|
|
|
|
|
private final String head;
|
|
|
|
|
private final Comparator<String> secondaryComparator;
|
|
|
|
|
public headMatchingComparator(final String head, final Comparator<String> secondaryComparator) {
|
|
|
|
|
this.head = head.toLowerCase();
|
|
|
|
|
private static class headMatchingComparator implements Comparator<StringBuilder> {
|
|
|
|
|
private final StringBuilder head;
|
|
|
|
|
private final Comparator<StringBuilder> secondaryComparator;
|
|
|
|
|
public headMatchingComparator(final StringBuilder head, final Comparator<StringBuilder> secondaryComparator) {
|
|
|
|
|
this.head = head;
|
|
|
|
|
this.secondaryComparator = secondaryComparator;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public int compare(final String o1, final String o2) {
|
|
|
|
|
boolean o1m = o1.toLowerCase().startsWith(head);
|
|
|
|
|
boolean o2m = o2.toLowerCase().startsWith(head);
|
|
|
|
|
if ((o1m && o2m) || (!o1m && !o2m)) return secondaryComparator.compare(o1, o2);
|
|
|
|
|
public int compare(final StringBuilder o1, final StringBuilder o2) {
|
|
|
|
|
final boolean o1m = StringBuilderComparator.CASE_INSENSITIVE_ORDER.startsWith(o1, this.head);
|
|
|
|
|
final boolean o2m = StringBuilderComparator.CASE_INSENSITIVE_ORDER.startsWith(o2, this.head);
|
|
|
|
|
if ((o1m && o2m) || (!o1m && !o2m)) return this.secondaryComparator.compare(o1, o2);
|
|
|
|
|
return o1m ? -1 : 1;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|