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