All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.vertx.serviceproxy.ProxyUtils Maven / Gradle / Ivy

package io.vertx.serviceproxy;

import io.vertx.core.json.JsonArray;
import io.vertx.core.json.JsonObject;

import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * @author Francesco Guardiani @slinkydeveloper
 */
@SuppressWarnings({"unchecked", "rawtypes"})
public class ProxyUtils {

  public static List convertToListChar(JsonArray arr) {
    List list = new ArrayList<>();
    for (Object obj: arr) {
      Integer jobj = (Integer)obj;
      list.add((char)(int)jobj);
    }
    return list;
  }

  public static Set convertToSetChar(JsonArray arr) {
    Set set = new HashSet<>();
    for (Object obj: arr) {
      Integer jobj = (Integer)obj;
      set.add((char)(int)jobj);
    }
    return set;
  }

  public static  Map convertMap(Map map) {
    if (map.isEmpty()) {
      return (Map) map;
    }

    Object elem = map.values().stream().findFirst().get();
    if (!(elem instanceof Map) && !(elem instanceof List)) {
      return (Map) map;
    } else {
      Function converter;
      if (elem instanceof List) {
        converter = object -> (T) new JsonArray((List) object);
      } else {
        converter = object -> (T) new JsonObject((Map) object);
      }
      return ((Map) map).entrySet()
        .stream()
        .collect(Collectors.toMap(Map.Entry::getKey, converter::apply));
    }
  }

  public static  List convertList(List list) {
    if (list.isEmpty()) {
      return (List) list;
    }

    Object elem = list.get(0);
    if (!(elem instanceof Map) && !(elem instanceof List)) {
      return (List) list;
    } else {
      Function converter;
      if (elem instanceof List) {
        converter = object -> (T) new JsonArray((List) object);
      } else {
        converter = object -> (T) new JsonObject((Map) object);
      }
      return (List) list.stream().map(converter).collect(Collectors.toList());
    }
  }

  public static  Set convertSet(List list) {
    return new HashSet(convertList(list));
  }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy