Skip to content

Commit

Permalink
Improve EMOJI_CODEPOINT_COMPARATOR
Browse files Browse the repository at this point in the history
  • Loading branch information
freya022 committed Nov 14, 2023
1 parent 8f336b6 commit 3123e1d
Showing 1 changed file with 8 additions and 2 deletions.
10 changes: 8 additions & 2 deletions lib/src/main/java/net/fellbaum/jemoji/EmojiManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,10 @@ public final class EmojiManager {
private static final Pattern NOT_WANTED_EMOJI_CHARACTERS = Pattern.compile("[\\p{Alpha}\\p{Z}]");

private static final Comparator<Emoji> EMOJI_CODEPOINT_COMPARATOR = (final Emoji o1, final Emoji o2) -> {
if (o1.getEmoji().codePoints().toArray().length == o2.getEmoji().codePoints().toArray().length) return 0;
return o1.getEmoji().codePoints().toArray().length > o2.getEmoji().codePoints().toArray().length ? -1 : 1;
final int codePointCount1 = getCodePointCount(o1.getEmoji());
final int codePointCount2 = getCodePointCount(o2.getEmoji());
if (codePointCount1 == codePointCount2) return 0;
return codePointCount1 > codePointCount2 ? -1 : 1;
};

static {
Expand All @@ -53,6 +55,10 @@ public final class EmojiManager {
}
}

private static int getCodePointCount(String string) {
return string.codePointCount(0, string.length());
}

private static Collector<Emoji, ?, LinkedHashMap<Integer, List<Emoji>>> getEmojiLinkedHashMapCollector() {
return Collectors.groupingBy(
emoji -> emoji.getEmoji().codePoints().toArray()[0],
Expand Down

0 comments on commit 3123e1d

Please sign in to comment.