/
EmojiManager.java
193 lines (175 loc) · 5.17 KB
/
EmojiManager.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
package com.vdurmont.emoji;
import java.io.IOException;
import java.io.InputStream;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
/**
* Holds the loaded emojis and provides search functions.
*
* @author Vincent DURMONT [vdurmont@gmail.com]
*/
public class EmojiManager {
private static final String PATH = "/emojis.json";
private static final Map<String, Emoji> EMOJIS_BY_ALIAS =
new HashMap<String, Emoji>();
private static final Map<String, Set<Emoji>> EMOJIS_BY_TAG =
new HashMap<String, Set<Emoji>>();
private static final List<Emoji> ALL_EMOJIS;
static final EmojiTrie EMOJI_TRIE;
static {
try {
InputStream stream = EmojiLoader.class.getResourceAsStream(PATH);
List<Emoji> emojis = EmojiLoader.loadEmojis(stream);
ALL_EMOJIS = emojis;
for (Emoji emoji : emojis) {
for (String tag : emoji.getTags()) {
if (EMOJIS_BY_TAG.get(tag) == null) {
EMOJIS_BY_TAG.put(tag, new HashSet<Emoji>());
}
EMOJIS_BY_TAG.get(tag).add(emoji);
}
for (String alias : emoji.getAliases()) {
EMOJIS_BY_ALIAS.put(alias, emoji);
}
}
EMOJI_TRIE = new EmojiTrie(emojis);
Collections.sort(ALL_EMOJIS, new Comparator<Emoji>() {
public int compare(Emoji e1, Emoji e2) {
return e2.getUnicode().length() - e1.getUnicode().length();
}
});
stream.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
/**
* No need for a constructor, all the methods are static.
*/
private EmojiManager() {}
/**
* Returns all the {@link com.vdurmont.emoji.Emoji}s for a given tag.
*
* @param tag the tag
*
* @return the associated {@link com.vdurmont.emoji.Emoji}s, null if the tag
* is unknown
*/
public static Set<Emoji> getForTag(String tag) {
if (tag == null) {
return null;
}
return EMOJIS_BY_TAG.get(tag);
}
/**
* Returns the {@link com.vdurmont.emoji.Emoji} for a given alias.
*
* @param alias the alias
*
* @return the associated {@link com.vdurmont.emoji.Emoji}, null if the alias
* is unknown
*/
public static Emoji getForAlias(String alias) {
if (alias == null || alias.isEmpty()) {
return null;
}
return EMOJIS_BY_ALIAS.get(trimAlias(alias));
}
private static String trimAlias(String alias) {
int len = alias.length();
return alias.substring(
alias.charAt(0) == ':' ? 1 : 0,
alias.charAt(len - 1) == ':' ? len - 1 : len);
}
/**
* Returns the {@link com.vdurmont.emoji.Emoji} for a given unicode.
*
* @param unicode the the unicode
*
* @return the associated {@link com.vdurmont.emoji.Emoji}, null if the
* unicode is unknown
*/
public static Emoji getByUnicode(String unicode) {
if (unicode == null) {
return null;
}
return EMOJI_TRIE.getEmoji(unicode);
}
/**
* Returns all the {@link com.vdurmont.emoji.Emoji}s
*
* @return all the {@link com.vdurmont.emoji.Emoji}s
*/
public static Collection<Emoji> getAll() {
return ALL_EMOJIS;
}
/**
* Tests if a given String is an emoji.
*
* @param string the string to test
*
* @return true if the string is an emoji's unicode, false else
*/
public static boolean isEmoji(String string) {
if (string == null) return false;
EmojiParser.UnicodeCandidate unicodeCandidate = EmojiParser.getNextUnicodeCandidate(string.toCharArray(), 0);
return unicodeCandidate != null &&
unicodeCandidate.getEmojiStartIndex() == 0 &&
unicodeCandidate.getFitzpatrickEndIndex() == string.length();
}
/**
* Tests if a given String contains an emoji.
*
* @param string the string to test
*
* @return true if the string contains an emoji's unicode, false otherwise
*/
public static boolean containsEmoji(String string) {
if (string == null) return false;
return EmojiParser.getNextUnicodeCandidate(string.toCharArray(), 0) != null;
}
/**
* Tests if a given String only contains emojis.
*
* @param string the string to test
*
* @return true if the string only contains emojis, false else
*/
public static boolean isOnlyEmojis(String string) {
return string != null && EmojiParser.removeAllEmojis(string).isEmpty();
}
/**
* Checks if sequence of chars contain an emoji.
* @param sequence Sequence of char that may contain emoji in full or
* partially.
*
* @return
* <li>
* Matches.EXACTLY if char sequence in its entirety is an emoji
* </li>
* <li>
* Matches.POSSIBLY if char sequence matches prefix of an emoji
* </li>
* <li>
* Matches.IMPOSSIBLE if char sequence matches no emoji or prefix of an
* emoji
* </li>
*/
public static EmojiTrie.Matches isEmoji(char[] sequence) {
return EMOJI_TRIE.isEmoji(sequence);
}
/**
* Returns all the tags in the database
*
* @return the tags
*/
public static Collection<String> getAllTags() {
return EMOJIS_BY_TAG.keySet();
}
}