package io.vertx.serviceproxy;
import io.vertx.core.AsyncResult;
import io.vertx.core.Handler;
import io.vertx.core.eventbus.Message;
import io.vertx.core.json.JsonArray;
import java.util.*;
import java.util.stream.Collectors;
@SuppressWarnings({"unchecked", "rawtypes"})
public class HelperUtils {
public static <T> Handler<AsyncResult<T>> createHandler(Message msg) {
return res -> {
if (res.failed()) {
if (res.cause() instanceof ServiceException) {
msg.reply(res.cause());
} else {
msg.reply(new ServiceException(-1, res.cause().getMessage()));
}
} else {
if (res.result() != null && res.result().getClass().isEnum()) {
msg.reply(((Enum) res.result()).name());
} else {
msg.reply(res.result());
}
}
};
}
public static <T> Handler<AsyncResult<List<T>>> createListHandler(Message msg) {
return res -> {
if (res.failed()) {
if (res.cause() instanceof ServiceException) {
msg.reply(res.cause());
} else {
msg.reply(new ServiceException(-1, res.cause().getMessage()));
}
} else {
msg.reply(new JsonArray(res.result()));
}
};
}
public static <T> Handler<AsyncResult<Set<T>>> createSetHandler(Message msg) {
return res -> {
if (res.failed()) {
if (res.cause() instanceof ServiceException) {
msg.reply(res.cause());
} else {
msg.reply(new ServiceException(-1, res.cause().getMessage()));
}
} else {
msg.reply(new JsonArray(new ArrayList<>(res.result())));
}
};
}
public static Handler<AsyncResult<List<Character>>> createListCharHandler(Message msg) {
return res -> {
if (res.failed()) {
if (res.cause() instanceof ServiceException) {
msg.reply(res.cause());
} else {
msg.reply(new ServiceException(-1, res.cause().getMessage()));
}
} else {
JsonArray arr = new JsonArray();
for (Character chr: res.result()) {
arr.add((int) chr);
}
msg.reply(arr);
}
};
}
public static Handler<AsyncResult<Set<Character>>> createSetCharHandler(Message msg) {
return res -> {
if (res.failed()) {
if (res.cause() instanceof ServiceException) {
msg.reply(res.cause());
} else {
msg.reply(new ServiceException(-1, res.cause().getMessage()));
}
} else {
JsonArray arr = new JsonArray();
for (Character chr: res.result()) {
arr.add((int) chr);
}
msg.reply(arr);
}
};
}
public static <T> Map<String, T> convertMap(Map map) {
return (Map<String, T>)map;
}
public static <T> List<T> convertList(List list) {
return (List<T>)list;
}
public static <T> Set<T> convertSet(List list) {
return new HashSet<T>((List<T>)list);
}
}