Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve EMOJI_CODEPOINT_COMPARATOR #18

Merged
merged 4 commits into from
Nov 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 22 additions & 2 deletions lib/src/main/java/net/fellbaum/jemoji/Emoji.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<Emoji> {

private final String emoji;
private final String unicode;
Expand Down Expand Up @@ -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;

Expand Down
39 changes: 8 additions & 31 deletions lib/src/main/java/net/fellbaum/jemoji/EmojiManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand All @@ -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> 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<Emoji> emojis = new ObjectMapper().readValue(fileContent, new TypeReference<List<Emoji>>() {
});

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) {
Expand All @@ -60,7 +58,7 @@ public final class EmojiManager {
Collectors.collectingAndThen(
Collectors.toList(),
list -> {
list.sort(EMOJI_CODEPOINT_COMPARATOR);
list.sort(Comparator.reverseOrder());
return list;
}
)
Expand Down Expand Up @@ -214,22 +212,6 @@ public static Optional<Emoji> 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 <K, V> Optional<V> findEmojiByEitherAlias(final Map<K, V> 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.
*
Expand Down Expand Up @@ -547,11 +529,6 @@ public static String replaceEmojis(final String text, Function<Emoji, String> re
return replaceEmojis(text, replacementFunction, Arrays.asList(emojisToReplace));
}

private static boolean isStringNullOrEmpty(final String string) {
return null == string || string.isEmpty();
}


/*public static List<Emoji> testEmojiPattern(final String text) {
if (isStringNullOrEmpty(text)) return Collections.emptyList();

Expand Down
31 changes: 31 additions & 0 deletions lib/src/main/java/net/fellbaum/jemoji/EmojiUtils.java
Original file line number Diff line number Diff line change
@@ -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 <K, V> Optional<V> findEmojiByEitherAlias(final Map<K, V> 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();
}
}