Skip to content

Commit

Permalink
Unicode Emoji Methods
Browse files Browse the repository at this point in the history
  • Loading branch information
ibrahimatay committed Nov 21, 2023
1 parent d92656b commit da2f152
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 0 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,14 @@ This repository contains Java examples that are designed to track and document t
* [JEP 430](java-21/src/main/java/com/ibrahimatay/JEP430StringTemplates.java): String Templates
* [JEP 431](java-21/src/main/java/com/ibrahimatay/JEP431SequencedCollections.java): Sequenced Collections
* [JEP 444](java-21/src/main/java/com/ibrahimatay/JEP444VirtualThreads.java): Virtual Threads
* API Improvements
* [Unicode Emoji Methods](java-21/src/main/java/com/ibrahimatay/UnicodeEmojiMethods.java)
* `isEmoji(int codePoint)`
* `isEmojiPresentation(int codePoint)`
* `isEmojiModifier(int codePoint)`
* `isEmojiModifierBase(int codePoint)`
* `isEmojiComponent(int codePoint)`
* `isExtendedPictographic(int codePoint)`

* [Java 17](java-17/) (September, 2021)
* [JEP 409](java-17/src/main/java/com/ibrahimatay/JEP409SealedClasses.java): Sealed Classes
Expand Down
52 changes: 52 additions & 0 deletions java-21/src/main/java/com/ibrahimatay/UnicodeEmojiMethods.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package com.ibrahimatay;

import java.util.OptionalInt;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class UnicodeEmojiMethods {

// Unicode Emoji Properties
// https://bugs.openjdk.org/browse/JDK-8303018

// JDK 21 JavaDoc for java.lang.Character
// https://docs.oracle.com/en/java/javase/21/docs/api/java.base/java/lang/Character.html

// Unicode® Technical Standard #Emoji Character Properties
// https://unicode.org/reports/tr51/#Emoji_Properties_and_Data_Files

public static void main(String[] args) {
String welcomeMsg = "Hey Java Developers! ☕️";

if(welcomeMsg.codePoints().anyMatch(Character::isEmoji)) {
System.out.println("Message contains an emoji!");
}

Matcher matcher = Pattern.compile("\\p{IsEmoji}").matcher(welcomeMsg);
if(matcher.find()) {
System.out.println("Message contains an emoji!");
}

Matcher matcher_modifier_base = Pattern.compile("\\p{IsEmoji_Modifier_Base}").matcher(welcomeMsg);
if(matcher_modifier_base.find()) {
System.out.println("Message contains an emoji modifier base!");
}

OptionalInt emojiOptional = welcomeMsg.codePoints().filter(Character::isEmoji).findFirst();
if (emojiOptional.isPresent()) {
int emojiCodePoint = emojiOptional.getAsInt();
if (Character.isEmojiModifierBase(emojiCodePoint)) {
System.out.println("Emoji can be modified");
if (Character.isEmojiModifier(emojiCodePoint)) {
System.out.println("Emoji is modified");
} else {
System.out.println("Emoji has not been modified");
}
} else {
System.out.println("Emoji cannot be modified");
}
} else {
System.out.println("No emoji");
}
}
}

0 comments on commit da2f152

Please sign in to comment.