Skip to content

Commit

Permalink
Change emoji description save format. Remove Jackson dependency.
Browse files Browse the repository at this point in the history
  • Loading branch information
felldo committed Aug 12, 2024
1 parent 25dc378 commit 95b9cb5
Show file tree
Hide file tree
Showing 643 changed files with 163 additions and 490 deletions.
10 changes: 9 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

# Java Emoji (JEmoji)

JEmoji is a lightweight and fast emoji library for Java with a complete list of all emojis from the unicode consortium.
JEmoji is a lightweight and fast emoji library for Java with a complete list of all emojis from the Unicode consortium.

With many utility methods and **type safe** direct access to Emojis,
JEmoji aims to improve your experience and development when working with Emojis.
Expand Down Expand Up @@ -93,6 +93,14 @@ Set<Emoji> emojis=EmojiManager.getAllEmojisByGroup(EmojiGroup.SMILEYS_AND_EMOTIO
Set<Emoji> emojis=EmojiManager.getAllEmojisBySubGroup(EmojiSubGroup.ANIMAL_BIRD);
```

#### Get emojis grouped / subgrouped

```java
//Commonly used in emoji pickers
Map<EmojiGroup, Set<Emoji>> a = EmojiManager.getAllEmojisGrouped();//{SMILEYS_AND_EMOTION=["😀","😘"...],...}
Map<EmojiSubGroup, Set<Emoji>> b = EmojiManager.getAllEmojisSubGrouped();//{FACE_SMILING=["😀","😄"...],...}
```

#### Check if the provided string is an emoji

```java
Expand Down
2 changes: 1 addition & 1 deletion emoji_source_files/emojiterra-emoji-definition.json

Large diffs are not rendered by default.

27 changes: 22 additions & 5 deletions lib/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ import okhttp3.internal.toHexString
import org.gradle.kotlin.dsl.support.uppercaseFirstChar
import org.jsoup.Connection
import org.jsoup.Jsoup
import java.io.FileOutputStream
import java.io.ObjectOutputStream
import java.util.stream.Collectors
import kotlin.math.ceil

Expand Down Expand Up @@ -78,9 +80,9 @@ repositories {
}

dependencies {
compileOnlyApi("org.jspecify:jspecify:0.3.0")
compileOnlyApi("org.jspecify:jspecify:1.0.0")

implementation("com.fasterxml.jackson.core:jackson-databind:2.17.1")
//implementation("com.fasterxml.jackson.core:jackson-databind:2.17.2")
}

testing {
Expand Down Expand Up @@ -168,7 +170,7 @@ tasks.named("build") {
}

/**
* Startup task to generate the needed source files for this project. Does not generate a new emojis.json.
* Startup task to generate the necessary source files for this project. Does not generate a new emojis.json.
*/
tasks.register("generate") {
dependsOn("generateEmojisDescriptionAndKeywords")
Expand Down Expand Up @@ -314,8 +316,8 @@ buildscript {
mavenCentral()
}
dependencies {
classpath("com.fasterxml.jackson.core:jackson-databind:2.17.1")
classpath("com.fasterxml.jackson.module:jackson-module-kotlin:2.17.1")
classpath("com.fasterxml.jackson.core:jackson-databind:2.17.2")
classpath("com.fasterxml.jackson.module:jackson-module-kotlin:2.17.2")
classpath("com.squareup.okhttp3:okhttp:4.9.3")

classpath("org.jsoup:jsoup:1.17.2")
Expand Down Expand Up @@ -359,12 +361,27 @@ tasks.register("generateEmojisDescriptionAndKeywords") {
}/annotations.json",
client, objectMapper, descriptionNodeOutput, keywordsNodeOutput, directory.get("name").asText()
)
/*
val descriptionFile =
File("$projectDir/src/main/resources/emoji_sources/description/${directory.get("name").asText()}.json")
descriptionFile.writeText(objectMapper.writeValueAsString(descriptionNodeOutput))
val keywordsFile =
File("$projectDir/src/main/resources/emoji_sources/keyword/${directory.get("name").asText()}.json")
keywordsFile.writeText(objectMapper.writeValueAsString(keywordsNodeOutput))
*/

val descriptionMap: Map<String, String> = objectMapper.treeToValue(descriptionNodeOutput, Map::class.java) as Map<String, String>
val descriptionfos: FileOutputStream = FileOutputStream("$projectDir/src/main/resources/emoji_sources/description/${directory.get("name").asText()}")
val descriptionoos: ObjectOutputStream = ObjectOutputStream(descriptionfos)
descriptionoos.writeObject(descriptionMap)
descriptionoos.close()

val keywordMap: Map<String, List<String>> = objectMapper.treeToValue(keywordsNodeOutput, Map::class.java) as Map<String, List<String>>
val keywordfos: FileOutputStream = FileOutputStream("$projectDir/src/main/resources/emoji_sources/keyword/${directory.get("name").asText()}")
val keywordoos: ObjectOutputStream = ObjectOutputStream(keywordfos)
keywordoos.writeObject(keywordMap)
keywordoos.close()

fileNameList.add(directory.get("name").asText())
}

Expand Down
5 changes: 2 additions & 3 deletions lib/src/java9/java/module-info.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
module net.fellbaum.jemoji {
requires com.fasterxml.jackson.databind;

opens net.fellbaum.jemoji to com.fasterxml.jackson.databind;
//requires com.fasterxml.jackson.databind;
//opens net.fellbaum.jemoji to com.fasterxml.jackson.databind;
exports net.fellbaum.jemoji;
}
73 changes: 36 additions & 37 deletions lib/src/main/java/net/fellbaum/jemoji/Emoji.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package net.fellbaum.jemoji;

import com.fasterxml.jackson.annotation.JsonProperty;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
Expand All @@ -13,6 +11,7 @@
/**
* Represents an emoji.
*/
@SuppressWarnings("unused")
public final class Emoji implements Comparable<Emoji> {

private final String emoji;
Expand All @@ -32,19 +31,19 @@ public final class Emoji implements Comparable<Emoji> {
private final List<String> allAliases;

Emoji(
@JsonProperty("emoji") String emoji,
@JsonProperty("unicode") String unicode,
@JsonProperty("discordAliases") List<String> discordAliases,
@JsonProperty("githubAliases") List<String> githubAliases,
@JsonProperty("slackAliases") List<String> slackAliases,
@JsonProperty("hasFitzpatrick") boolean hasFitzpatrick,
@JsonProperty("hasHairStyle") boolean hasHairStyle,
@JsonProperty("version") double version,
@JsonProperty("qualification") Qualification qualification,
@JsonProperty("description") String description,
@JsonProperty("group") EmojiGroup group,
@JsonProperty("subgroup") EmojiSubGroup subgroup,
@JsonProperty("hasVariationSelectors") boolean hasVariationSelectors) {
String emoji,
String unicode,
List<String> discordAliases,
List<String> githubAliases,
List<String> slackAliases,
boolean hasFitzpatrick,
boolean hasHairStyle,
double version,
Qualification qualification,
String description,
EmojiGroup group,
EmojiSubGroup subgroup,
boolean hasVariationSelectors) {
this.emoji = emoji;
this.unicode = unicode;
this.discordAliases = discordAliases;
Expand Down Expand Up @@ -77,7 +76,7 @@ public String getEmoji() {
/**
* Gets the unicode representation of the emoji as a string i.e. \uD83D\uDC4D.
*
* @return The unicode representation of the emoji
* @return The Unicode representation of the emoji
*/
public String getUnicode() {
return unicode;
Expand All @@ -102,7 +101,7 @@ public String getHtmlHexadecimalCode() {
}

/**
* Gets variations of this emoji with different Fitzpatrick or HairStyle modifiers, if there are any.
* Gets variations of this emoji with different Fitzpatrick or HairStyle modifiers if there are any.
* The returned list does not include this emoji itself.
*
* @return Variations of this emoji with different Fitzpatrick or HairStyle modifiers, if there are any.
Expand Down Expand Up @@ -184,9 +183,9 @@ public boolean hasHairStyleComponent() {
}

/**
* Gets the version this emoji was added to the unicode consortium.
* Gets the version this emoji was added to the Unicode consortium.
*
* @return The version this emoji was added to the unicode consortium.
* @return The version this emoji was added to the Unicode consortium.
*/
public double getVersion() {
return version;
Expand Down Expand Up @@ -250,24 +249,6 @@ public EmojiSubGroup getSubgroup() {
return subgroup;
}

/**
* 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());
}

/**
* Returns whether the emoji has text or emoji variations.
* This means the emoji is allowed to be appended with a FE0E or FE0F character to control how the emoji should be displayed.
Expand Down Expand Up @@ -297,6 +278,24 @@ public Optional<String> getEmojiVariation() {
return hasVariationSelectors() ? Optional.of(emoji + EMOJI_VARIATION_CHARACTER) : Optional.empty();
}

/**
* 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 Zero 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 String toString() {
return "Emoji{" +
Expand Down
4 changes: 1 addition & 3 deletions lib/src/main/java/net/fellbaum/jemoji/EmojiGroup.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package net.fellbaum.jemoji;

import com.fasterxml.jackson.annotation.JsonCreator;

import java.util.Arrays;
import java.util.EnumSet;
import java.util.List;
Expand All @@ -10,6 +8,7 @@
/**
* Represents an emoji group.
*/
@SuppressWarnings("unused")
public enum EmojiGroup {

ACTIVITIES("Activities"),
Expand Down Expand Up @@ -63,7 +62,6 @@ public EnumSet<EmojiSubGroup> getEmojiSubGroups() {
* @param name The name of the group.
* @return The emoji group.
*/
@JsonCreator
public static EmojiGroup fromString(String name) {
for (EmojiGroup emojiGroup : EMOJI_GROUPS) {
if (emojiGroup.getName().equals(name)) {
Expand Down
63 changes: 63 additions & 0 deletions lib/src/main/java/net/fellbaum/jemoji/EmojiLoader.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package net.fellbaum.jemoji;

import java.io.*;
import java.nio.charset.StandardCharsets;
import java.util.stream.Collectors;

@SuppressWarnings("unused")
public final class EmojiLoader {

private EmojiLoader() {
}

static Object readFileAsObject(final String filePathName) {
try {
try (final InputStream is = EmojiManager.class.getResourceAsStream(filePathName)) {
if (null == is) throw new IllegalStateException("InputStream is null");
final ObjectInputStream ois = new ObjectInputStream(is);
final Object readObject = ois.readObject();
ois.close();
return readObject;
} catch (final ClassNotFoundException e) {
throw new RuntimeException(e);
}
} catch (final IOException e) {
throw new RuntimeException(e);
}
}

static String readFileAsString(final String filePathName) {
try {
try (final InputStream is = EmojiManager.class.getResourceAsStream(filePathName)) {
if (null == is) throw new IllegalStateException("InputStream is null");
try (final InputStreamReader isr = new InputStreamReader(is, StandardCharsets.UTF_8);
final BufferedReader reader = new BufferedReader(isr)) {
return reader.lines().collect(Collectors.joining(System.lineSeparator()));
}
}
} catch (final IOException e) {
throw new RuntimeException(e);
}
}

/**
* Loads all emoji descriptions into memory to avoid potential description file reads during operation.
* This will most likely be called once on startup of your application.
*/
public static void loadAllEmojiDescriptions() {
for (EmojiLanguage value : EmojiLanguage.values()) {
EmojiManager.getEmojiDescriptionForLanguageAndEmoji(value, Emojis.THUMBS_UP.getEmoji());
}
}

/**
* Loads all emoji descriptions into memory to avoid potential description file reads during operation.
* This will most likely be called once on startup of your application.
*/
public static void loadAllEmojiKeywords() {
for (EmojiLanguage value : EmojiLanguage.values()) {
EmojiManager.getEmojiKeywordsForLanguageAndEmoji(value, Emojis.THUMBS_UP.getEmoji());
}
}

}
Loading

0 comments on commit 95b9cb5

Please sign in to comment.