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

net.intelie.pipes.MacroModule Maven / Gradle / Ivy

There is a newer version: 0.25.5
Show newest version
package net.intelie.pipes;

import net.intelie.pipes.ast.AstNode;
import net.intelie.pipes.ast.RawNode;
import net.intelie.pipes.util.Iterables;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.StreamSupport;

public class MacroModule implements Module {
    private static final long serialVersionUID = 1L;
    private final List functions;

    public MacroModule(Object... args) {
        this(Arrays.stream(args).map(value -> new RawNode(null, value)).collect(Collectors.toList()));
    }

    private MacroModule(Iterable nodes) {
        this.functions = makeFunctions(nodes);
    }

    public static List makeFunctions(Iterable args) {
        List functions = new ArrayList<>();
        int i = 0;
        for (AstNode arg : args) {
            int ii = i++;
            functions.add(new AstFunction("@@" + ii) {
                private static final long serialVersionUID = 1L;

                @Override
                public AstNode getNode(ArgQueue queue) {
                    return arg;
                }
            });

        }
        return functions;
    }

    public static MacroModule fromAstNodes(AstNode... args) {
        return new MacroModule(Arrays.asList(args));
    }

    public static MacroModule fromCompiled(Object... args) {
        return new MacroModule(Arrays.stream(args).map(value -> new RawNode(null, value)).collect(Collectors.toList()));
    }

    public static MacroModule fromAstNodes(Iterable args) {
        return new MacroModule(args);
    }

    public static MacroModule fromCompiledIterable(Iterable objs) {
        return new MacroModule(StreamSupport.stream(objs.spliterator(), false)
                .map(value -> new RawNode(null, value)).collect(Collectors.toList()));
    }

    @Override
    public Iterable functions() {
        return functions;
    }

    @Override
    public String toString() {
        return "macro module with " + functions.size() + " macros";
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy