1package im.conversations.android.xmpp.model;
 2
 3import androidx.annotation.NonNull;
 4import androidx.annotation.Nullable;
 5import com.google.common.base.CaseFormat;
 6import com.google.common.base.Strings;
 7
 8import eu.siacs.conversations.xml.Namespace;
 9import im.conversations.android.annotation.XmlElement;
10
11@XmlElement(namespace = Namespace.HASHES)
12public class Hash extends Extension {
13    public Hash() {
14        super(Hash.class);
15    }
16
17    public Algorithm getAlgorithm() {
18        return Algorithm.tryParse(this.getAttribute("algo"));
19    }
20
21    public void setAlgorithm(final Algorithm algorithm) {
22        this.setAttribute("algo", algorithm.toString());
23    }
24
25    public enum Algorithm {
26        SHA_1,
27        SHA_256,
28        SHA_512;
29
30        public static Algorithm tryParse(@Nullable final String name) {
31            try {
32                return valueOf(
33                        CaseFormat.LOWER_HYPHEN.to(
34                                CaseFormat.UPPER_UNDERSCORE, Strings.nullToEmpty(name)));
35            } catch (final IllegalArgumentException e) {
36                return null;
37            }
38        }
39
40        @NonNull
41        @Override
42        public String toString() {
43            return CaseFormat.UPPER_UNDERSCORE.to(CaseFormat.LOWER_HYPHEN, super.toString());
44        }
45    }
46}