]> nmode's Git Repositories - signal-cli/blob - lib/src/main/java/org/asamk/signal/manager/storage/recipients/RecipientAddress.java
1e33c47ea86228550695d8a853d47e3dd8ea707f
[signal-cli] / lib / src / main / java / org / asamk / signal / manager / storage / recipients / RecipientAddress.java
1 package org.asamk.signal.manager.storage.recipients;
2
3 import org.whispersystems.signalservice.api.push.ACI;
4 import org.whispersystems.signalservice.api.push.SignalServiceAddress;
5
6 import java.util.Optional;
7 import java.util.UUID;
8
9 public class RecipientAddress {
10
11 public static final UUID UNKNOWN_UUID = ACI.UNKNOWN.uuid();
12
13 private final Optional<UUID> uuid;
14 private final Optional<String> e164;
15
16 /**
17 * Construct a RecipientAddress.
18 *
19 * @param uuid The UUID of the user, if available.
20 * @param e164 The phone number of the user, if available.
21 */
22 public RecipientAddress(Optional<UUID> uuid, Optional<String> e164) {
23 uuid = uuid.isPresent() && uuid.get().equals(UNKNOWN_UUID) ? Optional.empty() : uuid;
24 if (uuid.isEmpty() && e164.isEmpty()) {
25 throw new AssertionError("Must have either a UUID or E164 number!");
26 }
27
28 this.uuid = uuid;
29 this.e164 = e164;
30 }
31
32 public RecipientAddress(UUID uuid, String e164) {
33 this(Optional.ofNullable(uuid), Optional.ofNullable(e164));
34 }
35
36 public RecipientAddress(SignalServiceAddress address) {
37 this(Optional.of(address.getAci().uuid()), Optional.ofNullable(address.getNumber().orNull()));
38 }
39
40 public RecipientAddress(UUID uuid) {
41 this(Optional.of(uuid), Optional.empty());
42 }
43
44 public Optional<String> getNumber() {
45 return e164;
46 }
47
48 public Optional<UUID> getUuid() {
49 return uuid;
50 }
51
52 public String getIdentifier() {
53 if (uuid.isPresent()) {
54 return uuid.get().toString();
55 } else if (e164.isPresent()) {
56 return e164.get();
57 } else {
58 throw new AssertionError("Given the checks in the constructor, this should not be possible.");
59 }
60 }
61
62 public String getLegacyIdentifier() {
63 if (e164.isPresent()) {
64 return e164.get();
65 } else if (uuid.isPresent()) {
66 return uuid.get().toString();
67 } else {
68 throw new AssertionError("Given the checks in the constructor, this should not be possible.");
69 }
70 }
71
72 public boolean matches(RecipientAddress other) {
73 return (uuid.isPresent() && other.uuid.isPresent() && uuid.get().equals(other.uuid.get())) || (
74 e164.isPresent() && other.e164.isPresent() && e164.get().equals(other.e164.get())
75 );
76 }
77
78 public SignalServiceAddress toSignalServiceAddress() {
79 return new SignalServiceAddress(ACI.from(uuid.orElse(UNKNOWN_UUID)),
80 org.whispersystems.libsignal.util.guava.Optional.fromNullable(e164.orElse(null)));
81 }
82
83 @Override
84 public boolean equals(final Object o) {
85 if (this == o) return true;
86 if (o == null || getClass() != o.getClass()) return false;
87
88 final RecipientAddress that = (RecipientAddress) o;
89
90 if (!uuid.equals(that.uuid)) return false;
91 return e164.equals(that.e164);
92 }
93
94 @Override
95 public int hashCode() {
96 int result = uuid.hashCode();
97 result = 31 * result + e164.hashCode();
98 return result;
99 }
100 }