1 package org
.asamk
.signal
.manager
.storage
.recipients
;
3 import org
.whispersystems
.signalservice
.api
.push
.ACI
;
4 import org
.whispersystems
.signalservice
.api
.push
.SignalServiceAddress
;
6 import java
.util
.Optional
;
9 public class RecipientAddress
{
11 public static final UUID UNKNOWN_UUID
= ACI
.UNKNOWN
.uuid();
13 private final Optional
<UUID
> uuid
;
14 private final Optional
<String
> e164
;
17 * Construct a RecipientAddress.
19 * @param uuid The UUID of the user, if available.
20 * @param e164 The phone number of the user, if available.
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!");
32 public RecipientAddress(UUID uuid
, String e164
) {
33 this(Optional
.ofNullable(uuid
), Optional
.ofNullable(e164
));
36 public RecipientAddress(SignalServiceAddress address
) {
37 this(Optional
.of(address
.getAci().uuid()), Optional
.ofNullable(address
.getNumber().orNull()));
40 public RecipientAddress(UUID uuid
) {
41 this(Optional
.of(uuid
), Optional
.empty());
44 public Optional
<String
> getNumber() {
48 public Optional
<UUID
> getUuid() {
52 public String
getIdentifier() {
53 if (uuid
.isPresent()) {
54 return uuid
.get().toString();
55 } else if (e164
.isPresent()) {
58 throw new AssertionError("Given the checks in the constructor, this should not be possible.");
62 public String
getLegacyIdentifier() {
63 if (e164
.isPresent()) {
65 } else if (uuid
.isPresent()) {
66 return uuid
.get().toString();
68 throw new AssertionError("Given the checks in the constructor, this should not be possible.");
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())
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)));
84 public boolean equals(final Object o
) {
85 if (this == o
) return true;
86 if (o
== null || getClass() != o
.getClass()) return false;
88 final RecipientAddress that
= (RecipientAddress
) o
;
90 if (!uuid
.equals(that
.uuid
)) return false;
91 return e164
.equals(that
.e164
);
95 public int hashCode() {
96 int result
= uuid
.hashCode();
97 result
= 31 * result
+ e164
.hashCode();