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

Lazy initialize the emoji pattern #13

Merged
merged 1 commit into from
Nov 9, 2023
Merged
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
10 changes: 6 additions & 4 deletions lib/src/main/java/net/fellbaum/jemoji/EmojiManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public final class EmojiManager {
private static final Map<Integer, List<Emoji>> EMOJI_FIRST_CODEPOINT_TO_EMOJIS_ORDER_CODEPOINT_LENGTH_DESCENDING;
private static final List<Emoji> EMOJIS_LENGTH_DESCENDING;

private static final Pattern EMOJI_PATTERN;
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) -> {
Expand All @@ -45,9 +45,6 @@ public final class EmojiManager {
EMOJIS_LENGTH_DESCENDING = Collections.unmodifiableList(emojis.stream().sorted(EMOJI_CODEPOINT_COMPARATOR).collect(Collectors.toList()));

EMOJI_FIRST_CODEPOINT_TO_EMOJIS_ORDER_CODEPOINT_LENGTH_DESCENDING = emojis.stream().collect(getEmojiLinkedHashMapCollector());

EMOJI_PATTERN = Pattern.compile(EMOJIS_LENGTH_DESCENDING.stream()
.map(s -> "(" + Pattern.quote(s.getEmoji()) + ")").collect(Collectors.joining("|")));
} catch (final JsonProcessingException e) {
throw new RuntimeException(e);
}
Expand Down Expand Up @@ -223,6 +220,11 @@ private static String addColonToAlias(final String alias) {
* @return The pattern for all emojis.
*/
public static Pattern getEmojiPattern() {
if (EMOJI_PATTERN == null) {
EMOJI_PATTERN = Pattern.compile(EMOJIS_LENGTH_DESCENDING.stream()
.map(s -> "(" + Pattern.quote(s.getEmoji()) + ")").collect(Collectors.joining("|")));
}

return EMOJI_PATTERN;
}

Expand Down