com.vdurmont.emoji.EmojiManager Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of emoji-java Show documentation
Show all versions of emoji-java Show documentation
The missing emoji library for Java.
package com.vdurmont.emoji;
import java.io.IOException;
import java.io.InputStream;
import java.util.Collection;
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 [[email protected]]
*/
public class EmojiManager {
private static final String PATH = "/emojis.json";
private static final Map EMOJIS_BY_ALIAS = new HashMap();
private static final Map> EMOJIS_BY_TAG = new HashMap>();
static {
try {
InputStream stream = EmojiLoader.class.getResourceAsStream(PATH);
List emojis = EmojiLoader.loadEmojis(stream);
for (Emoji emoji : emojis) {
for (String tag : emoji.getTags()) {
if (EMOJIS_BY_TAG.get(tag) == null) {
EMOJIS_BY_TAG.put(tag, new HashSet());
}
EMOJIS_BY_TAG.get(tag).add(emoji);
}
for (String alias : emoji.getAliases()) {
EMOJIS_BY_ALIAS.put(alias, emoji);
}
}
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 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) {
return null;
}
return EMOJIS_BY_ALIAS.get(trimAlias(alias));
}
private static String trimAlias(String alias) {
String result = alias;
if (result.startsWith(":")) {
result = result.substring(1, result.length());
}
if (result.endsWith(":")) {
result = result.substring(0, result.length() - 1);
}
return result;
}
/**
* Returns all the {@link com.vdurmont.emoji.Emoji}s
*
* @return all the {@link com.vdurmont.emoji.Emoji}s
*/
public static Collection getAll() {
return EMOJIS_BY_ALIAS.values();
}
/**
* 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) {
for (Emoji emoji : getAll()) {
if (emoji.getUnicode().equals(string)) {
return true;
}
}
}
return false;
}
/**
* Returns all the tags in the database
*
* @return the tags
*/
public static Collection getAllTags() {
return EMOJIS_BY_TAG.keySet();
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy