DaveTeng0 commented on code in PR #1098:
URL: https://github.com/apache/ratis/pull/1098#discussion_r1635369219


##########
ratis-shell/src/main/java/org/apache/ratis/shell/cli/RaftUtils.java:
##########
@@ -86,4 +103,111 @@ public static RaftClient createClient(RaftGroup raftGroup) 
{
         .setRetryPolicy(retryPolicy)
         .build();
   }
+
+
+  /**
+   * Execute a given function with input parameter from the members of a list.
+   *
+   * @param list the input parameters
+   * @param function the function to be executed
+   * @param <T> parameter type
+   * @param <K> return value type
+   * @param <E> the exception type thrown by the given function.
+   * @return the value returned by the given function.
+   */
+  public static <T, K, E extends Throwable> K runFunction(Collection<T> list, 
CheckedFunction<T, K, E> function) {
+    for (T t : list) {
+      try {
+        K ret = function.apply(t);
+        if (ret != null) {
+          return ret;
+        }
+      } catch (Throwable e) {
+        e.printStackTrace();
+      }
+    }
+    return null;
+  }
+
+
+  public static List<RaftPeer> buildRaftPeersFromStr(String peers) {
+    List<InetSocketAddress> addresses = new ArrayList<>();
+    String[] peersArray = peers.split(",");
+    for (String peer : peersArray) {
+      addresses.add(parseInetSocketAddress(peer));
+    }
+
+    return addresses.stream()
+        .map(addr -> RaftPeer.newBuilder()
+            .setId(RaftUtils.getPeerId(addr))
+            .setAddress(addr)
+            .build()
+        ).collect(Collectors.toList());
+  }
+
+  public static RaftGroupId buildRaftGroupIdFromStr(String groupId) {
+    return (groupId != null && !groupId.equals("")) ? 
RaftGroupId.valueOf(UUID.fromString(groupId))
+        : DEFAULT_RAFT_GROUP_ID;
+  }
+
+  public static RaftGroupId retrieveRemoteGroupId(RaftGroupId 
raftGroupIdFromConfig,
+                                                  List<RaftPeer> peers,
+                                                  RaftClient client, 
PrintStream printStream) throws IOException {
+    RaftGroupId remoteGroupId;
+    if (raftGroupIdFromConfig != DEFAULT_RAFT_GROUP_ID) {
+      return raftGroupIdFromConfig;
+    } else {
+      final List<RaftGroupId> 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);
+      }
+    }
+
+    return remoteGroupId;
+  }
+
+  public static GroupInfoReply retrieveGroupInfoByGroupId(RaftGroupId 
remoteGroupId, List<RaftPeer> peers,
+                                                          RaftClient client, 
PrintStream printStream)
+      throws IOException {
+    GroupInfoReply groupInfoReply = runFunction(peers,
+        p -> client.getGroupManagementApi((p.getId())).info(remoteGroupId));
+    processReply(groupInfoReply,
+        printStream::println, "Failed to get group info for group id " + 
remoteGroupId.getUuid() + " from " + peers);

Review Comment:
   Just very curious, why we use Supplier to provide the string value instead 
of the pure string here and other similar places in code? Thanks!!



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscr...@ratis.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org

Reply via email to