]> nmode's Git Repositories - signal-cli/blob - lib/src/main/java/org/asamk/signal/manager/api/RecipientAddress.java
Update libsignal-service
[signal-cli] / lib / src / main / java / org / asamk / signal / manager / api / RecipientAddress.java
1 package org.asamk.signal.manager.api;
2
3 import org.whispersystems.signalservice.api.push.SignalServiceAddress;
4 import org.whispersystems.signalservice.api.util.UuidUtil;
5
6 import java.util.Optional;
7 import java.util.UUID;
8
9 public record RecipientAddress(Optional<UUID> uuid, Optional<String> number, Optional<String> username) {
10
11 public static final UUID UNKNOWN_UUID = UuidUtil.UNKNOWN_UUID;
12
13 /**
14 * Construct a RecipientAddress.
15 *
16 * @param uuid The UUID of the user, if available.
17 * @param number The phone number of the user, if available.
18 */
19 public RecipientAddress {
20 uuid = uuid.isPresent() && uuid.get().equals(UNKNOWN_UUID) ? Optional.empty() : uuid;
21 if (uuid.isEmpty() && number.isEmpty() && username.isEmpty()) {
22 throw new AssertionError("Must have either a UUID or E164 number!");
23 }
24 }
25
26 public RecipientAddress(UUID uuid, String e164) {
27 this(Optional.ofNullable(uuid), Optional.ofNullable(e164), Optional.empty());
28 }
29
30 public RecipientAddress(UUID uuid, String e164, String username) {
31 this(Optional.ofNullable(uuid), Optional.ofNullable(e164), Optional.ofNullable(username));
32 }
33
34 public RecipientAddress(SignalServiceAddress address) {
35 this(Optional.of(address.getServiceId().getRawUuid()), address.getNumber(), Optional.empty());
36 }
37
38 public RecipientAddress(UUID uuid) {
39 this(Optional.of(uuid), Optional.empty(), Optional.empty());
40 }
41
42 public String getIdentifier() {
43 if (uuid.isPresent()) {
44 return uuid.get().toString();
45 } else if (number.isPresent()) {
46 return number.get();
47 } else if (username.isPresent()) {
48 return username.get();
49 } else {
50 throw new AssertionError("Given the checks in the constructor, this should not be possible.");
51 }
52 }
53
54 public String getLegacyIdentifier() {
55 if (number.isPresent()) {
56 return number.get();
57 } else if (uuid.isPresent()) {
58 return uuid.get().toString();
59 } else if (username.isPresent()) {
60 return username.get();
61 } else {
62 throw new AssertionError("Given the checks in the constructor, this should not be possible.");
63 }
64 }
65
66 public boolean matches(RecipientAddress other) {
67 return (uuid.isPresent() && other.uuid.isPresent() && uuid.get().equals(other.uuid.get()))
68 || (number.isPresent() && other.number.isPresent() && number.get().equals(other.number.get()))
69 || (username.isPresent() && other.username.isPresent() && username.get().equals(other.username.get()));
70 }
71 }