Convert RecipientIdentifier to record

This commit is contained in:
AsamK 2021-10-31 21:24:25 +01:00
parent 31dec5a666
commit b615a4b04d
3 changed files with 15 additions and 82 deletions

View file

@ -587,7 +587,7 @@ public class ManagerImpl implements Manager {
final var result = sendHelper.sendSelfMessage(messageBuilder); final var result = sendHelper.sendSelfMessage(messageBuilder);
results.put(recipient, List.of(result)); results.put(recipient, List.of(result));
} else if (recipient instanceof RecipientIdentifier.Group group) { } else if (recipient instanceof RecipientIdentifier.Group group) {
final var result = sendHelper.sendAsGroupMessage(messageBuilder, group.groupId); final var result = sendHelper.sendAsGroupMessage(messageBuilder, group.groupId());
results.put(recipient, result); results.put(recipient, result);
} }
} }
@ -604,7 +604,7 @@ public class ManagerImpl implements Manager {
final var recipientId = resolveRecipient((RecipientIdentifier.Single) recipient); final var recipientId = resolveRecipient((RecipientIdentifier.Single) recipient);
sendHelper.sendTypingMessage(message, recipientId); sendHelper.sendTypingMessage(message, recipientId);
} else if (recipient instanceof RecipientIdentifier.Group) { } else if (recipient instanceof RecipientIdentifier.Group) {
final var groupId = ((RecipientIdentifier.Group) recipient).groupId; final var groupId = ((RecipientIdentifier.Group) recipient).groupId();
final var message = new SignalServiceTypingMessage(action, timestamp, Optional.of(groupId.serialize())); final var message = new SignalServiceTypingMessage(action, timestamp, Optional.of(groupId.serialize()));
sendHelper.sendGroupTypingMessage(message, groupId); sendHelper.sendGroupTypingMessage(message, groupId);
} }
@ -1334,9 +1334,9 @@ public class ManagerImpl implements Manager {
private RecipientId resolveRecipient(final RecipientIdentifier.Single recipient) throws IOException { private RecipientId resolveRecipient(final RecipientIdentifier.Single recipient) throws IOException {
if (recipient instanceof RecipientIdentifier.Uuid) { if (recipient instanceof RecipientIdentifier.Uuid) {
return account.getRecipientStore().resolveRecipient(((RecipientIdentifier.Uuid) recipient).uuid); return account.getRecipientStore().resolveRecipient(((RecipientIdentifier.Uuid) recipient).uuid());
} else { } else {
final var number = ((RecipientIdentifier.Number) recipient).number; final var number = ((RecipientIdentifier.Number) recipient).number();
return account.getRecipientStore().resolveRecipient(number, () -> { return account.getRecipientStore().resolveRecipient(number, () -> {
try { try {
return getRegisteredUser(number); return getRegisteredUser(number);

View file

@ -9,29 +9,26 @@ import org.whispersystems.signalservice.api.util.UuidUtil;
import java.util.UUID; import java.util.UUID;
public sealed abstract class RecipientIdentifier { public sealed interface RecipientIdentifier {
public static final class NoteToSelf extends RecipientIdentifier { record NoteToSelf() implements RecipientIdentifier {
public static NoteToSelf INSTANCE = new NoteToSelf(); public static NoteToSelf INSTANCE = new NoteToSelf();
private NoteToSelf() {
}
} }
public sealed static abstract class Single extends RecipientIdentifier { sealed interface Single extends RecipientIdentifier {
public static Single fromString(String identifier, String localNumber) throws InvalidNumberException { static Single fromString(String identifier, String localNumber) throws InvalidNumberException {
return UuidUtil.isUuid(identifier) return UuidUtil.isUuid(identifier)
? new Uuid(UUID.fromString(identifier)) ? new Uuid(UUID.fromString(identifier))
: new Number(PhoneNumberFormatter.formatNumber(identifier, localNumber)); : new Number(PhoneNumberFormatter.formatNumber(identifier, localNumber));
} }
public static Single fromAddress(SignalServiceAddress address) { static Single fromAddress(SignalServiceAddress address) {
return new Uuid(address.getUuid()); return new Uuid(address.getUuid());
} }
public static Single fromAddress(RecipientAddress address) { static Single fromAddress(RecipientAddress address) {
if (address.getNumber().isPresent()) { if (address.getNumber().isPresent()) {
return new Number(address.getNumber().get()); return new Number(address.getNumber().get());
} else if (address.getUuid().isPresent()) { } else if (address.getUuid().isPresent()) {
@ -40,31 +37,10 @@ public sealed abstract class RecipientIdentifier {
throw new AssertionError("RecipientAddress without identifier"); throw new AssertionError("RecipientAddress without identifier");
} }
public abstract String getIdentifier(); String getIdentifier();
} }
public static final class Uuid extends Single { record Uuid(UUID uuid) implements Single {
public final UUID uuid;
public Uuid(final UUID uuid) {
this.uuid = uuid;
}
@Override
public boolean equals(final Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
final Uuid uuid1 = (Uuid) o;
return uuid.equals(uuid1.uuid);
}
@Override
public int hashCode() {
return uuid.hashCode();
}
@Override @Override
public String getIdentifier() { public String getIdentifier() {
@ -72,28 +48,7 @@ public sealed abstract class RecipientIdentifier {
} }
} }
public static final class Number extends Single { record Number(String number) implements Single {
public final String number;
public Number(final String number) {
this.number = number;
}
@Override
public boolean equals(final Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
final Number number1 = (Number) o;
return number.equals(number1.number);
}
@Override
public int hashCode() {
return number.hashCode();
}
@Override @Override
public String getIdentifier() { public String getIdentifier() {
@ -101,27 +56,5 @@ public sealed abstract class RecipientIdentifier {
} }
} }
public static final class Group extends RecipientIdentifier { record Group(GroupId groupId) implements RecipientIdentifier {}
public final GroupId groupId;
public Group(final GroupId groupId) {
this.groupId = groupId;
}
@Override
public boolean equals(final Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
final Group group = (Group) o;
return groupId.equals(group.groupId);
}
@Override
public int hashCode() {
return groupId.hashCode();
}
}
} }

View file

@ -582,7 +582,7 @@ public class DbusManagerImpl implements Manager {
final var groupRecipients = recipients.stream() final var groupRecipients = recipients.stream()
.filter(r -> r instanceof RecipientIdentifier.Group) .filter(r -> r instanceof RecipientIdentifier.Group)
.map(RecipientIdentifier.Group.class::cast) .map(RecipientIdentifier.Group.class::cast)
.map(g -> g.groupId) .map(RecipientIdentifier.Group::groupId)
.collect(Collectors.toList()); .collect(Collectors.toList());
for (final var groupId : groupRecipients) { for (final var groupId : groupRecipients) {
timestamp = groupHandler.apply(groupId.serialize()); timestamp = groupHandler.apply(groupId.serialize());