Skip to content

Commit

Permalink
Rename EmojiTrie to EmojiTree and move Matches and Node classes out
Browse files Browse the repository at this point in the history
  • Loading branch information
wax911 committed Jun 18, 2020
1 parent a6df5fd commit aecf003
Showing 1 changed file with 9 additions and 36 deletions.
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package io.wax911.emojify.util

import java.util.HashMap

import io.wax911.emojify.model.Emoji
import io.wax911.emojify.util.tree.Matches
import io.wax911.emojify.util.tree.Node


class EmojiTrie(emojis: Collection<Emoji>) {
class EmojiTree(emojis: Collection<Emoji>) {
private val root = Node()

init {
Expand All @@ -19,17 +20,16 @@ class EmojiTrie(emojis: Collection<Emoji>) {
}
}


/**
* Checks if sequence of chars contain an emoji.
*
* @param sequence Sequence of char that may contain emoji in full or
* partially.
*
* @return
* - Matches.EXACTLY if char sequence in its entirety is an emoji
* - Matches.POSSIBLY if char sequence matches prefix of an emoji
* - Matches.IMPOSSIBLE if char sequence matches no emoji or prefix of an emoji
* - [Matches.EXACTLY] if char sequence in its entirety is an emoji
* - [Matches.POSSIBLY] if char sequence matches prefix of an emoji
* - [Matches.IMPOSSIBLE] if char sequence matches no emoji or prefix of an emoji
*/
fun isEmoji(sequence: CharArray?): Matches {
if (sequence == null)
Expand All @@ -50,7 +50,9 @@ class EmojiTrie(emojis: Collection<Emoji>) {

/**
* Finds Emoji instance from emoji unicode
*
* @param unicode unicode of emoji to get
*
* @return Emoji instance if unicode matches and emoji, null otherwise.
*/
fun getEmoji(unicode: String): Emoji? {
Expand All @@ -62,33 +64,4 @@ class EmojiTrie(emojis: Collection<Emoji>) {
}
return tree?.emoji
}

enum class Matches {
EXACTLY, POSSIBLY, IMPOSSIBLE;

fun exactMatch() = this == EXACTLY

fun impossibleMatch() = this == IMPOSSIBLE
}

inner class Node {
private val children = HashMap<Char, Node>()

internal var emoji: Emoji? = null

internal val isEndOfEmoji: Boolean
get() = emoji != null

internal fun hasChild(child: Char): Boolean {
return children.containsKey(child)
}

internal fun addChild(child: Char) {
children[child] = Node()
}

internal fun getChild(child: Char): Node? {
return children[child]
}
}
}

0 comments on commit aecf003

Please sign in to comment.