]> nmode's Git Repositories - signal-cli/blob - lib/src/main/java/org/asamk/signal/manager/groups/GroupId.java
Refactor ReceiveCommand in dbus mode and remove ExtendedDbusCommand
[signal-cli] / lib / src / main / java / org / asamk / signal / manager / groups / GroupId.java
1 package org.asamk.signal.manager.groups;
2
3 import java.util.Arrays;
4 import java.util.Base64;
5
6 public abstract sealed class GroupId permits GroupIdV1, GroupIdV2 {
7
8 private final byte[] id;
9
10 public static GroupIdV1 v1(byte[] id) {
11 return new GroupIdV1(id);
12 }
13
14 public static GroupIdV2 v2(byte[] id) {
15 return new GroupIdV2(id);
16 }
17
18 public static GroupId unknownVersion(byte[] id) {
19 if (id.length == 16) {
20 return new GroupIdV1(id);
21 } else if (id.length == 32) {
22 return new GroupIdV2(id);
23 }
24
25 throw new AssertionError("Invalid group id of size " + id.length);
26 }
27
28 public static GroupId fromBase64(String id) throws GroupIdFormatException {
29 try {
30 return unknownVersion(java.util.Base64.getDecoder().decode(id));
31 } catch (Throwable e) {
32 throw new GroupIdFormatException(id, e);
33 }
34 }
35
36 protected GroupId(final byte[] id) {
37 this.id = id;
38 }
39
40 public byte[] serialize() {
41 return id;
42 }
43
44 public String toBase64() {
45 return Base64.getEncoder().encodeToString(id);
46 }
47
48 @Override
49 public boolean equals(final Object o) {
50 if (this == o) return true;
51 if (o == null || getClass() != o.getClass()) return false;
52
53 final var groupId = (GroupId) o;
54
55 return Arrays.equals(id, groupId.id);
56 }
57
58 @Override
59 public int hashCode() {
60 return Arrays.hashCode(id);
61 }
62 }