aboutsummaryrefslogtreecommitdiffstats
path: root/libs/emojicon/src/main/java/github/ankushsachdeva/emojicon/Emojicon.java
blob: bf8874b20512fb60869b153d7a5d8119359db672 (plain)
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
package github.ankushsachdeva.emojicon;

import java.nio.charset.Charset;

/**
 * Created by aleksandr.naumov on 13.05.2015.
 */
public class Emojicon {
    private final String mStringRepresentation;

    public Emojicon(String stringRepresentation) {
        byte bytes[] = new byte[stringRepresentation.length() / 2];
        for (int i = 0; i < stringRepresentation.length(); i += 2) {
            try {
                bytes[i / 2] = (byte) Integer.parseInt(stringRepresentation.substring(i, i + 2), 16);
            } catch (NumberFormatException ex) {
                throw new RuntimeException(String.format("Cannot parse '%s' as emojicon code", stringRepresentation), ex);
            }
        }
        this.mStringRepresentation = new String(bytes, Charset.forName("Utf-8"));
    }

    @Override
    public String toString() {
        return mStringRepresentation;
    }

    public String getId() {
        String id = "";
        for (byte b : mStringRepresentation.getBytes()) {
            id += String.format("%x", b);
        }
        return id;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }
        if (o == null || getClass() != o.getClass()) {
            return false;
        }
        Emojicon emojicon = (Emojicon) o;
        return mStringRepresentation.equals(emojicon.mStringRepresentation);

    }

    @Override
    public int hashCode() {
        return mStringRepresentation.hashCode();
    }
}