diff --git a/lib/src/main/java/net/fellbaum/jemoji/Emoji.java b/lib/src/main/java/net/fellbaum/jemoji/Emoji.java index ad3589b..9a6158a 100644 --- a/lib/src/main/java/net/fellbaum/jemoji/Emoji.java +++ b/lib/src/main/java/net/fellbaum/jemoji/Emoji.java @@ -12,10 +12,12 @@ import java.util.Set; import java.util.stream.Collectors; +import static net.fellbaum.jemoji.EmojiUtils.getCodePointCount; + /** * Represents an emoji. */ -public class Emoji { +public class Emoji implements Comparable { private final String emoji; private final String unicode; @@ -245,8 +247,26 @@ public String toString() { '}'; } + /** + * Compares the emojis based on their codepoint length, + * and if they are equal, compare them lexicographically based on the emoji. + * + * @param o the object to be compared. + * @return the value 0 if they are fully equal, 1 if the emoji has more codepoints + * and has a higher unicode value, otherwise return -1 + */ + @Override + public int compareTo(final Emoji o) { + final int comparedValue = Integer.compare(getCodePointCount(this.getEmoji()), getCodePointCount(o.getEmoji())); + if (comparedValue != 0) { + return comparedValue; + } + + return this.getEmoji().compareTo(o.getEmoji()); + } + @Override - public boolean equals(Object o) { + public boolean equals(final Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; diff --git a/lib/src/main/java/net/fellbaum/jemoji/EmojiManager.java b/lib/src/main/java/net/fellbaum/jemoji/EmojiManager.java index 2330cfe..8181e6c 100644 --- a/lib/src/main/java/net/fellbaum/jemoji/EmojiManager.java +++ b/lib/src/main/java/net/fellbaum/jemoji/EmojiManager.java @@ -15,6 +15,11 @@ import java.util.stream.Collector; import java.util.stream.Collectors; +import static net.fellbaum.jemoji.EmojiUtils.addColonToAlias; +import static net.fellbaum.jemoji.EmojiUtils.findEmojiByEitherAlias; +import static net.fellbaum.jemoji.EmojiUtils.isStringNullOrEmpty; +import static net.fellbaum.jemoji.EmojiUtils.removeColonFromAlias; + @SuppressWarnings("unused") public final class EmojiManager { @@ -30,22 +35,15 @@ public final class EmojiManager { private static Pattern EMOJI_PATTERN; private static final Pattern NOT_WANTED_EMOJI_CHARACTERS = Pattern.compile("[\\p{Alpha}\\p{Z}]"); - private static final Comparator 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; - }; - static { final String fileContent = readFileAsString(); try { final List emojis = new ObjectMapper().readValue(fileContent, new TypeReference>() { }); - EMOJI_UNICODE_TO_EMOJI = Collections.unmodifiableMap( - emojis.stream().collect(Collectors.toMap(Emoji::getEmoji, Function.identity())) - ); + EMOJI_UNICODE_TO_EMOJI = Collections.unmodifiableMap(emojis.stream().collect(Collectors.toMap(Emoji::getEmoji, Function.identity()))); - EMOJIS_LENGTH_DESCENDING = Collections.unmodifiableList(emojis.stream().sorted(EMOJI_CODEPOINT_COMPARATOR).collect(Collectors.toList())); + EMOJIS_LENGTH_DESCENDING = Collections.unmodifiableList(emojis.stream().sorted(Comparator.reverseOrder()).collect(Collectors.toList())); EMOJI_FIRST_CODEPOINT_TO_EMOJIS_ORDER_CODEPOINT_LENGTH_DESCENDING = emojis.stream().collect(getEmojiLinkedHashMapCollector()); } catch (final JsonProcessingException e) { @@ -60,7 +58,7 @@ public final class EmojiManager { Collectors.collectingAndThen( Collectors.toList(), list -> { - list.sort(EMOJI_CODEPOINT_COMPARATOR); + list.sort(Comparator.reverseOrder()); return list; } ) @@ -214,22 +212,6 @@ public static Optional getBySlackAlias(final String alias) { return findEmojiByEitherAlias(getEmojiAliasToEmoji(AliasGroup.SLACK), aliasWithColon, aliasWithoutColon); } - private static String removeColonFromAlias(final String alias) { - return alias.startsWith(":") && alias.endsWith(":") ? alias.substring(1, alias.length() - 1) : alias; - } - - private static String addColonToAlias(final String alias) { - return alias.startsWith(":") && alias.endsWith(":") ? alias : ":" + alias + ":"; - } - - private static Optional findEmojiByEitherAlias(final Map map, final K aliasWithColon, final K aliasWithoutColon) { - final V firstValue = map.get(aliasWithColon); - if (firstValue != null) return Optional.of(firstValue); - final V secondValue = map.get(aliasWithoutColon); - if (secondValue != null) return Optional.of(secondValue); - return Optional.empty(); - } - /** * Gets the pattern checking for all emojis. * @@ -547,11 +529,6 @@ public static String replaceEmojis(final String text, Function re return replaceEmojis(text, replacementFunction, Arrays.asList(emojisToReplace)); } - private static boolean isStringNullOrEmpty(final String string) { - return null == string || string.isEmpty(); - } - - /*public static List testEmojiPattern(final String text) { if (isStringNullOrEmpty(text)) return Collections.emptyList(); diff --git a/lib/src/main/java/net/fellbaum/jemoji/EmojiUtils.java b/lib/src/main/java/net/fellbaum/jemoji/EmojiUtils.java new file mode 100644 index 0000000..b0fb6a7 --- /dev/null +++ b/lib/src/main/java/net/fellbaum/jemoji/EmojiUtils.java @@ -0,0 +1,31 @@ +package net.fellbaum.jemoji; + +import java.util.Map; +import java.util.Optional; + +class EmojiUtils { + + public static int getCodePointCount(String string) { + return string.codePointCount(0, string.length()); + } + + public static boolean isStringNullOrEmpty(final String string) { + return null == string || string.isEmpty(); + } + + public static String removeColonFromAlias(final String alias) { + return alias.startsWith(":") && alias.endsWith(":") ? alias.substring(1, alias.length() - 1) : alias; + } + + public static String addColonToAlias(final String alias) { + return alias.startsWith(":") && alias.endsWith(":") ? alias : ":" + alias + ":"; + } + + public static Optional findEmojiByEitherAlias(final Map map, final K aliasWithColon, final K aliasWithoutColon) { + final V firstValue = map.get(aliasWithColon); + if (firstValue != null) return Optional.of(firstValue); + final V secondValue = map.get(aliasWithoutColon); + if (secondValue != null) return Optional.of(secondValue); + return Optional.empty(); + } +}