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

com.jdiai.jsproducer.Json Maven / Gradle / Ivy

package com.jdiai.jsproducer;

import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.jdiai.tools.map.MapArray;
import com.jdiai.tools.pairs.Pair;

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

import static com.jdiai.tools.LinqUtils.newList;
import static com.jdiai.tools.PrintUtils.print;
import static com.jdiai.tools.StringUtils.format;

public class Json extends MapArray {
    public Json(List keys, JsonObject json) {
        this(keys, s -> json.get(s).getAsString());
    }
    public Json() {
        pairs = new ArrayList<>();
    }

    public Json(String key, String value) {
        this();
        add(key, value);
    }

    public Json(Pair... pairs) {
        this(newList(pairs));
    }
    public Json(List> pairs) {
        this();
        try {
            for (Pair pair : pairs)
                add(pair.key, pair.value);
        } catch (Exception ex) {
            throw new RuntimeException(format("Can't create MapArray. Exception: %s", ex.getMessage()));
        }
    }

    public Json(Collection collection, Function value) {
        this(collection, k -> k, value::apply);
    }
    public  Json(Collection collection, Function keyFunc, Function valueFunc) {
        this(collection, keyFunc, valueFunc, false);
    }
    public  Json(Collection collection, Function keyFunc, Function valueFunc, boolean ignoreNotUnique) {
        this();
        try {
            for (T t : collection) {
                if (ignoreNotUnique) {
                    addNew(keyFunc.apply(t), valueFunc.apply(t));
                } else {
                    add(keyFunc.apply(t), valueFunc.apply(t));
                }
            }
        } catch (Exception ex) {
            throw new RuntimeException(format("Can't create MapArray. Exception: %s", ex.getMessage()));
        }
    }

    public Json(String[] array, Function value) {
        this(newList(array), value);
    }
    public  Json(T[] array, Function key, Function value) {
        this(array, key, value, false);
    }
    public  Json(T[] array, Function key, Function value, boolean ignoreNotUnique) {
        this(newList(array), key, value, ignoreNotUnique);
    }

    public Json(int count, Function keyFunc, Function value) {
        this();
        try {
            for (int i = 0; i < count; i++)
                add(keyFunc.apply(i), value.apply(i));
        } catch (Exception ex) {
            throw new RuntimeException(format("Can't create MapArray. Exception: %s", ex.getMessage()));
        }
    }
    public Json(int count, Function> pairFunc) {
        this();
        try {
            for (int i = 0; i < count; i++) {
                Pair pair = pairFunc.apply(i);
                add(pair.key, pair.value);
            }
        } catch (Exception ex) {
            throw new RuntimeException(format("Can't create MapArray. Exception: %s", ex.getMessage()));
        }
    }
    public void addUnique(String key, String value) {
        if (keys().contains(key))
            throw new RuntimeException("Duplicated keys " + key + ". Can't create MapArray");
        add(key, value);
    }

    public Json(MapArray mapArray) {
        this();
        addAll(new ArrayList<>(mapArray));
    }
    public Json(Map map) {
        this();
        if (map != null) {
            for (Map.Entry entry : map.entrySet())
                add(entry.getKey(), entry.getValue());
        }
    }

    public Json(Object[][] objects) {
        this();
        if (objects != null) {
            add(objects);
        }
    }
    private static List jsonToList(JsonElement json) {
        List result = new ArrayList<>();
        for (JsonElement js : json.getAsJsonArray()) {
            result.add(js.getAsString());
        }
        return result;
    }
    public Json(JsonElement keys, JsonElement values) {
        this(jsonToList(keys), jsonToList(values));
    }
    public Json(Collection keys, Collection values) {
        this();
        if (keys != null && values != null) {
            if (keys.size() != values.size())
                throw new RuntimeException(format("keys and values has different count (keys:[%s]; values:[%s])",
                        print(keys, Object::toString), print(values, Objects::toString)));
            Iterator ik = keys.iterator();
            Iterator vk = values.iterator();
            for (int i = 0; i < keys.size(); i++) {
                add(ik.next(), vk.next());
            }
        }
    }
    public Json(String[] keys, String[] values) {
        this(newList(keys), newList(values));
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy