--- /dev/null
+package io.bytom.http;\r
+\r
+import io.bytom.common.Utils;\r
+import io.bytom.exception.BytomException;\r
+\r
+import java.util.*;\r
+\r
+/**\r
+ * BatchResponse provides a convenient interface for handling the results of\r
+ * batched API calls. The response contains one success or error per outgoing\r
+ * request item in the batch. Errors are always of type BytomException.\r
+ */\r
+public class BatchResponse<T> {\r
+ private Map<Integer, T> successesByIndex = new LinkedHashMap<>();\r
+ private Map<Integer, BytomException> errorsByIndex = new LinkedHashMap<>();\r
+\r
+ public String toJson() {\r
+ return Utils.serializer.toJson(this);\r
+ }\r
+\r
+ /**\r
+ * This constructor is used for synthetically generating a batch response\r
+ * object from a map of successes and a map of errors. It ensures that\r
+ * the successes and errors are stored in an order-preserving fashion.\r
+ */\r
+ public BatchResponse(Map<Integer, T> successes, Map<Integer, BytomException> errors) {\r
+ List<Integer> successIndexes = new ArrayList<>();\r
+ Iterator<Integer> successIter = successes.keySet().iterator();\r
+ while (successIter.hasNext()) {\r
+ successIndexes.add(successIter.next());\r
+ }\r
+ Collections.sort(successIndexes);\r
+\r
+ for (int i : successIndexes) {\r
+ successesByIndex.put(i, successes.get(i));\r
+ }\r
+\r
+ List<Integer> errorIndexes = new ArrayList<>();\r
+ Iterator<Integer> errorIter = errors.keySet().iterator();\r
+ while (errorIter.hasNext()) {\r
+ errorIndexes.add(errorIter.next());\r
+ }\r
+ Collections.sort(errorIndexes);\r
+ for (int i : errorIndexes) {\r
+ errorsByIndex.put(i, errors.get(i));\r
+ }\r
+ }\r
+\r
+ /**\r
+ * Returns the total number of response objects. This should equal the number\r
+ * of request objects in the batch.\r
+ */\r
+ public int size() {\r
+ return successesByIndex.size() + errorsByIndex.size();\r
+ }\r
+\r
+ /**\r
+ * Returns whether the request object at the given index produced a success.\r
+ *\r
+ * @param index the index of the request object\r
+ */\r
+ public boolean isSuccess(int index) {\r
+ return successesByIndex.containsKey(index);\r
+ }\r
+\r
+ /**\r
+ * Returns whether the request object at the given index produced an error.\r
+ *\r
+ * @param index the index of the request object\r
+ */\r
+ public boolean isError(int index) {\r
+ return errorsByIndex.containsKey(index);\r
+ }\r
+\r
+ /**\r
+ * Returns a list of successful response objects in the batch. The order of\r
+ * the list corresponds to the order of the request objects that produced the\r
+ * successes.\r
+ */\r
+ public List<T> successes() {\r
+ List<T> res = new ArrayList<>();\r
+ res.addAll(successesByIndex.values());\r
+ return res;\r
+ }\r
+\r
+ /**\r
+ * Returns a list of error objects in the batch. The order of the list\r
+ * corresponds to the order of the request objects that produced the\r
+ * errors.\r
+ */\r
+ public List<BytomException> errors() {\r
+ List<BytomException> res = new ArrayList<>();\r
+ res.addAll(errorsByIndex.values());\r
+ return res;\r
+ }\r
+\r
+ /**\r
+ * Returns a map of success responses, keyed by the index of the request\r
+ * object that produced the success. The set of this map's keys is mutually\r
+ * exclusive of the keys returned by errorsByIndex.\r
+ */\r
+ public Map<Integer, T> successesByIndex() {\r
+ return successesByIndex;\r
+ }\r
+\r
+ /**\r
+ * Returns a map of error responses, keyed by the index of the request\r
+ * object that produced the error. The set of this map's keys is mutually\r
+ * exclusive of the keys returned by successByIndex.\r
+ */\r
+ public Map<Integer, BytomException> errorsByIndex() {\r
+ return errorsByIndex;\r
+ }\r
+}\r