From b943e2a8c7b8cb21707423de853f4d2db66b21f0 Mon Sep 17 00:00:00 2001 From: DaveTeng0 Date: Tue, 11 Jun 2024 12:46:20 -0700 Subject: [PATCH] Improve code readability and maintainability --- .../org/apache/ratis/shell/cli/RaftUtils.java | 71 +++++++++---------- .../cli/sh/command/AbstractRatisCommand.java | 3 +- 2 files changed, 35 insertions(+), 39 deletions(-) diff --git a/ratis-shell/src/main/java/org/apache/ratis/shell/cli/RaftUtils.java b/ratis-shell/src/main/java/org/apache/ratis/shell/cli/RaftUtils.java index 090ed289d2..0c75a76913 100644 --- a/ratis-shell/src/main/java/org/apache/ratis/shell/cli/RaftUtils.java +++ b/ratis-shell/src/main/java/org/apache/ratis/shell/cli/RaftUtils.java @@ -41,6 +41,7 @@ import java.util.List; import java.util.Optional; import java.util.Properties; +import java.util.function.Supplier; import java.util.stream.Collectors; import java.util.UUID; @@ -104,21 +105,21 @@ public static RaftClient createClient(RaftGroup raftGroup) { .build(); } - /** - * Execute a given function with input parameter from the members of a list. + * Apply the given function to the given parameter a list. * - * @param list the input parameters - * @param function the function to be executed - * @param parameter type - * @param return value type - * @param the exception type thrown by the given function. - * @return the value returned by the given function. + * @param list the input parameter list + * @param function the function to be applied + * @param parameter type + * @param return value type + * @param the exception type thrown by the given function. + * @return the first non-null value returned by the given function applied to the given list. */ - public static K runFunction(Collection list, CheckedFunction function) { - for (T t : list) { + private static RETURN applyFunctionReturnFirstNonNull( + Collection list, CheckedFunction function) { + for (PARAMETER parameter : list) { try { - K ret = function.apply(t); + RETURN ret = function.apply(parameter); if (ret != null) { return ret; } @@ -129,7 +130,6 @@ public static K runFunction(Collection list, Chec return null; } - public static List buildRaftPeersFromStr(String peers) { List addresses = new ArrayList<>(); String[] peersArray = peers.split(","); @@ -146,54 +146,51 @@ public static List buildRaftPeersFromStr(String peers) { } public static RaftGroupId buildRaftGroupIdFromStr(String groupId) { - return (groupId != null && !groupId.equals("")) ? RaftGroupId.valueOf(UUID.fromString(groupId)) + return groupId.isEmpty() ? RaftGroupId.valueOf(UUID.fromString(groupId)) : DEFAULT_RAFT_GROUP_ID; } public static RaftGroupId retrieveRemoteGroupId(RaftGroupId raftGroupIdFromConfig, List peers, RaftClient client, PrintStream printStream) throws IOException { - RaftGroupId remoteGroupId; - if (raftGroupIdFromConfig != DEFAULT_RAFT_GROUP_ID) { + if (!DEFAULT_RAFT_GROUP_ID .equals(raftGroupIdFromConfig)) { return raftGroupIdFromConfig; - } else { - final List groupIds = runFunction(peers, - p -> client.getGroupManagementApi((p.getId())).list().getGroupIds()); - - if (groupIds == null) { - printStream.println("Failed to get group ID from " + peers); - throw new IOException("Failed to get group ID from " + peers); - } else if (groupIds.size() == 1) { - remoteGroupId = groupIds.get(0); - } else { - printStream.println("There are more than one groups, you should specific one. " + groupIds); - throw new IOException("There are more than one groups, you should specific one. " + groupIds); - } } + final RaftGroupId remoteGroupId; + final List groupIds = applyFunctionReturnFirstNonNull(peers, + p -> client.getGroupManagementApi((p.getId())).list().getGroupIds()); + + if (groupIds == null) { + printStream.println("Failed to get group ID from " + peers); + throw new IOException("Failed to get group ID from " + peers); + } else if (groupIds.size() == 1) { + remoteGroupId = groupIds.get(0); + } else { + String message = "Unexpected multiple group IDs " + groupIds + + ". In such case, the target group ID must be specified."; + printStream.println(message); + throw new IOException(message); + } return remoteGroupId; } public static GroupInfoReply retrieveGroupInfoByGroupId(RaftGroupId remoteGroupId, List peers, RaftClient client, PrintStream printStream) throws IOException { - GroupInfoReply groupInfoReply = runFunction(peers, + GroupInfoReply groupInfoReply = applyFunctionReturnFirstNonNull(peers, p -> client.getGroupManagementApi((p.getId())).info(remoteGroupId)); - processReply(groupInfoReply, - printStream::println, "Failed to get group info for group id " + remoteGroupId.getUuid() + " from " + peers); + processReply(groupInfoReply, printStream::println, + () -> "Failed to get group info for group id " + remoteGroupId.getUuid() + " from " + peers); return groupInfoReply; } - public static void processReply(RaftClientReply reply, Consumer printer, String message) throws IOException { - processReplyInternal(reply, () -> printer.accept(message)); - } - - private static void processReplyInternal(RaftClientReply reply, Runnable printer) throws IOException { + public static void processReply(RaftClientReply reply, Consumer printer, Supplier message) throws IOException { if (reply == null || !reply.isSuccess()) { final RaftException e = Optional.ofNullable(reply) .map(RaftClientReply::getException) .orElseGet(() -> new RaftException("Reply: " + reply)); - printer.run(); + printer.accept(message.get()); throw new IOException(e.getMessage(), e); } } diff --git a/ratis-shell/src/main/java/org/apache/ratis/shell/cli/sh/command/AbstractRatisCommand.java b/ratis-shell/src/main/java/org/apache/ratis/shell/cli/sh/command/AbstractRatisCommand.java index 13b3e4a1fa..9d132e9c4b 100644 --- a/ratis-shell/src/main/java/org/apache/ratis/shell/cli/sh/command/AbstractRatisCommand.java +++ b/ratis-shell/src/main/java/org/apache/ratis/shell/cli/sh/command/AbstractRatisCommand.java @@ -148,8 +148,7 @@ protected RaftPeerProto getLeader(RoleInfoProto roleInfo) { } protected void processReply(RaftClientReply reply, Supplier messageSupplier) throws IOException { - RaftUtils.processReply(reply, - getPrintStream()::println, messageSupplier.get()); + RaftUtils.processReply(reply, getPrintStream()::println, messageSupplier); } protected List getIds(String[] optionValues, BiConsumer consumer) {