public Object mergeResults(Map<String, Object> results) throws Exception { if (results.isEmpty()) return null; // TODO: DELEGATE MERGE AT EVERY COMMAND final ArrayList<Object> mergedResult = new ArrayList<Object>(); final Object firstResult = results.values().iterator().next(); for (Map.Entry<String, Object> entry : results.entrySet()) { final String nodeName = entry.getKey(); final Object nodeResult = entry.getValue(); if (nodeResult instanceof Collection) mergedResult.addAll((Collection<?>) nodeResult); else if (nodeResult instanceof Exception) // RECEIVED EXCEPTION throw (Exception) nodeResult; else mergedResult.add(nodeResult); } Object result = null; if (firstResult instanceof OResultSet) { // REUSE THE SAME RESULTSET TO AVOID DUPLICATES ((OResultSet) firstResult).clear(); ((OResultSet) firstResult).addAll(mergedResult); result = firstResult; } else result = new ArrayList<Object>(mergedResult); return result; }