]> nmode's Git Repositories - signal-cli/blob - lib/src/main/java/org/asamk/signal/manager/storage/recipients/RecipientAddress.java
29e964b0d411d871eb4b2172753691d6e9c0f4b9
[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 if (!uuid.isPresent() && !e164.isPresent()) {
22 throw new AssertionError("Must have either a UUID or E164 number!");
23 }
24
25 this.uuid = uuid;
26 this.e164 = e164;
27 }
28
29 public RecipientAddress(UUID uuid, String e164) {
30 this(Optional.ofNullable(uuid), Optional.ofNullable(e164));
31 }
32
33 public RecipientAddress(SignalServiceAddress address) {
34 this.uuid = Optional.of(address.getUuid());
35 this.e164 = Optional.ofNullable(address.getNumber().orNull());
36 }
37
38 public RecipientAddress(UUID uuid) {
39 this.uuid = Optional.of(uuid);
40 this.e164 = 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 boolean matches(RecipientAddress other) {
62 return (uuid.isPresent() && other.uuid.isPresent() && uuid.get().equals(other.uuid.get())) || (
63 e164.isPresent() && other.e164.isPresent() && e164.get().equals(other.e164.get())
64 );
65 }
66
67 public SignalServiceAddress toSignalServiceAddress() {
68 return new SignalServiceAddress(uuid.orElse(UuidUtil.UNKNOWN_UUID),
69 org.whispersystems.libsignal.util.guava.Optional.fromNullable(e164.orElse(null)));
70 }
71
72 @Override
73 public boolean equals(final Object o) {
74 if (this == o) return true;
75 if (o == null || getClass() != o.getClass()) return false;
76
77 final RecipientAddress that = (RecipientAddress) o;
78
79 if (!uuid.equals(that.uuid)) return false;
80 return e164.equals(that.e164);
81 }
82
83 @Override
84 public int hashCode() {
85 int result = uuid.hashCode();
86 result = 31 * result + e164.hashCode();
87 return result;
88 }
89 }