]> nmode's Git Repositories - signal-cli/blobdiff - src/main/java/org/asamk/signal/commands/ListContactsCommand.java
Use pattern matching switch cases
[signal-cli] / src / main / java / org / asamk / signal / commands / ListContactsCommand.java
index 997e31dd5b6af41c50f376a736e2e2c0b0ed2db6..42a33a14842b32c6fd54caf492b746c115a7b858 100644 (file)
@@ -51,48 +51,49 @@ public class ListContactsCommand implements JsonRpcLocalCommand {
                 recipientIdentifiers,
                 Optional.ofNullable(name));
 
-        if (outputWriter instanceof PlainTextWriter writer) {
-            for (var r : recipients) {
-                final var contact = r.getContact() == null ? Contact.newBuilder().build() : r.getContact();
-                final var profile = r.getProfile() == null ? Profile.newBuilder().build() : r.getProfile();
-                writer.println(
-                        "Number: {} Name: {} Profile name: {} Username: {} Color: {} Blocked: {} Message expiration: {}",
-                        r.getAddress().getLegacyIdentifier(),
-                        contact.getName(),
-                        profile.getDisplayName(),
-                        r.getAddress().username().orElse(""),
-                        contact.getColor(),
-                        contact.isBlocked(),
-                        contact.getMessageExpirationTime() == 0
-                                ? "disabled"
-                                : contact.getMessageExpirationTime() + "s");
+        switch (outputWriter) {
+            case PlainTextWriter writer -> {
+                for (var r : recipients) {
+                    final var contact = r.getContact() == null ? Contact.newBuilder().build() : r.getContact();
+                    final var profile = r.getProfile() == null ? Profile.newBuilder().build() : r.getProfile();
+                    writer.println(
+                            "Number: {} Name: {} Profile name: {} Username: {} Color: {} Blocked: {} Message expiration: {}",
+                            r.getAddress().getLegacyIdentifier(),
+                            contact.getName(),
+                            profile.getDisplayName(),
+                            r.getAddress().username().orElse(""),
+                            contact.getColor(),
+                            contact.isBlocked(),
+                            contact.getMessageExpirationTime() == 0
+                                    ? "disabled"
+                                    : contact.getMessageExpirationTime() + "s");
+                }
+            }
+            case JsonWriter writer -> {
+                final var jsonContacts = recipients.stream().map(r -> {
+                    final var address = r.getAddress();
+                    final var contact = r.getContact() == null ? Contact.newBuilder().build() : r.getContact();
+                    return new JsonContact(address.number().orElse(null),
+                            address.uuid().map(UUID::toString).orElse(null),
+                            address.username().orElse(null),
+                            contact.getName(),
+                            contact.getColor(),
+                            contact.isBlocked(),
+                            contact.getMessageExpirationTime(),
+                            r.getProfile() == null
+                                    ? null
+                                    : new JsonContact.JsonProfile(r.getProfile().getLastUpdateTimestamp(),
+                                            r.getProfile().getGivenName(),
+                                            r.getProfile().getFamilyName(),
+                                            r.getProfile().getAbout(),
+                                            r.getProfile().getAboutEmoji(),
+                                            r.getProfile().getMobileCoinAddress() == null
+                                                    ? null
+                                                    : Base64.getEncoder()
+                                                            .encodeToString(r.getProfile().getMobileCoinAddress())));
+                }).toList();
+                writer.write(jsonContacts);
             }
-        } else {
-            final var writer = (JsonWriter) outputWriter;
-            final var jsonContacts = recipients.stream().map(r -> {
-                final var address = r.getAddress();
-                final var contact = r.getContact() == null ? Contact.newBuilder().build() : r.getContact();
-                return new JsonContact(address.number().orElse(null),
-                        address.uuid().map(UUID::toString).orElse(null),
-                        address.username().orElse(null),
-                        contact.getName(),
-                        contact.getColor(),
-                        contact.isBlocked(),
-                        contact.getMessageExpirationTime(),
-                        r.getProfile() == null
-                                ? null
-                                : new JsonContact.JsonProfile(r.getProfile().getLastUpdateTimestamp(),
-                                        r.getProfile().getGivenName(),
-                                        r.getProfile().getFamilyName(),
-                                        r.getProfile().getAbout(),
-                                        r.getProfile().getAboutEmoji(),
-                                        r.getProfile().getMobileCoinAddress() == null
-                                                ? null
-                                                : Base64.getEncoder()
-                                                        .encodeToString(r.getProfile().getMobileCoinAddress())));
-            }).toList();
-
-            writer.write(jsonContacts);
         }
     }