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