net.intelie.pipes.MacroModule Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of pipes-api Show documentation
Show all versions of pipes-api Show documentation
Intelie Pipes' API classes and interfaces
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";
}
}