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

cn.nukkit.plugin.js.feature.ConvertFeature Maven / Gradle / Ivy

There is a newer version: 1.20.40-r1
Show newest version
package cn.nukkit.plugin.js.feature;

import cn.nukkit.plugin.js.JSFeature;
import org.graalvm.polyglot.Context;
import org.graalvm.polyglot.proxy.Proxy;
import org.graalvm.polyglot.proxy.ProxyExecutable;

import java.util.*;

public class ConvertFeature implements JSFeature {
    @Override
    public String getName() {
        return "Convert";
    }

    @Override
    public Collection availableModuleNames() {
        return List.of("Convertor", "Convert");
    }

    @Override
    public Map generateModule(String moduleName, Context context) {
        if ("Convertor".equals(moduleName) || "Convert".equals(moduleName)) {
            var map = new HashMap(1, 1);
            map.put("convertToJava", (ProxyExecutable) arguments -> {
                if (arguments.length == 2) {
                    //noinspection unchecked
                    return arguments[1].as(arguments[0].as(Class.class));
                } else {
                    throw new IllegalArgumentException("2 arguments are required.");
                }
            });
            map.put("convertToSet", (ProxyExecutable) arguments -> {
                if (arguments.length == 2) {
                    return arguments[1].as(Set.class);
                } else {
                    throw new IllegalArgumentException("1 arguments is required.");
                }
            });
            map.put("convertToList", (ProxyExecutable) arguments -> {
                if (arguments.length == 2) {
                    return arguments[1].as(List.class);
                } else {
                    throw new IllegalArgumentException("1 arguments is required.");
                }
            });
            return map;
        }
        return Collections.emptyMap();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy