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

io.vertx.groovy.ext.mongo.internal.ConversionHelper Maven / Gradle / Ivy

There is a newer version: 5.0.0.CR1
Show newest version
package io.vertx.groovy.ext.mongo.internal;

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

import java.util.ArrayList;
import java.util.Base64;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Function;

/**
 * Internal API.
 */
public class ConversionHelper {

  @SuppressWarnings("unchecked")
  public static Object unwrap(Object obj) {
    if (obj instanceof Map) {
      return toJsonObject((Map) obj);
    } else if (obj instanceof List) {
      return toJsonArray((List) obj);
    } else if (obj instanceof CharSequence) {
      return obj.toString();
    }
    return obj;
  }

  @SuppressWarnings("unchecked")
  private static Object toJsonElement(Object obj) {
    if (obj instanceof Map) {
      return toJsonObject((Map) obj);
    } else if (obj instanceof List) {
      return toJsonArray((List) obj);
    } else if (obj instanceof CharSequence) {
      return obj.toString();
    } else if (obj instanceof Buffer) {
      return Base64.getEncoder().encodeToString(((Buffer)obj).getBytes());
    }
    return obj;
  }

  public static JsonObject toJsonObject(Map map) {
    if (map == null) {
      return null;
    }
    map = new LinkedHashMap<>(map);
    map.entrySet().forEach(e -> e.setValue(toJsonElement(e.getValue())));
    return new JsonObject(map);
  }

  public static JsonArray toJsonArray(List list) {
    if (list == null) {
      return null;
    }
    list = new ArrayList<>(list);
    for (int i = 0;i < list.size();i++) {
      list.set(i, toJsonElement(list.get(i)));
    }
    return new JsonArray(list);
  }

  public static  R applyIfNotNull(T expr, Function function) {
    if (expr != null) {
      return function.apply(expr);
    } else {
      return null;
    }
  }

  public static  R wrap(T t, Function f) {
    if (t != null) {
      return wrap(f.apply(t));
    } else {
      return null;
    }
  }

  @SuppressWarnings("unchecked")
  public static  T wrap(Object obj) {
    if (obj instanceof JsonObject) {
      return (T)fromJsonObject((JsonObject)obj);
    } else if (obj instanceof JsonArray) {
      return (T)fromJsonArray((JsonArray)obj);
    }
    return (T)obj;
  }

  public static Map fromJsonObject(JsonObject json) {
    if (json == null) {
      return null;
    }
    Map map = new LinkedHashMap<>(json.getMap());
    map.entrySet().forEach(entry -> {
      entry.setValue(wrap(entry.getValue()));
    });
    return map;
  }

  public static List fromJsonArray(JsonArray json) {
    if (json == null) {
      return null;
    }
    List list = new ArrayList<>(json.getList());
    for (int i = 0;i < list.size();i++) {
      list.set(i, wrap(list.get(i)));
    }
    return list;
  }
}