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

com.groupbyinc.common.jackson.jq.internal.JsonArgumentFunction Maven / Gradle / Ivy

There is a newer version: 198
Show newest version
package net.thisptr.jackson.jq.internal;

import java.util.ArrayList;
import java.util.List;
import java.util.Stack;

import com.fasterxml.jackson.databind.JsonNode;

import net.thisptr.jackson.jq.Function;
import net.thisptr.jackson.jq.JsonQuery;
import net.thisptr.jackson.jq.Scope;
import net.thisptr.jackson.jq.exception.JsonQueryException;
import net.thisptr.jackson.jq.internal.misc.Functional;

public abstract class JsonArgumentFunction implements Function {
	protected abstract JsonNode fn(final List args, final JsonNode in) throws JsonQueryException;

	private void combinations(final Functional.Consumer out, final Stack args, final int index, final List> argmat, final JsonNode in) throws JsonQueryException {
		if (index >= argmat.size()) {
			out.accept(fn(args, in));
			return;
		}

		for (final JsonNode arg : argmat.get(index)) {
			args.push(arg);
			combinations(out, args, index + 1, argmat, in);
			args.pop();
		}
	}

	@Override
	public List apply(final Scope scope, final List args, final JsonNode in) throws JsonQueryException {
		final List> _args = new ArrayList<>();
		for (final JsonQuery arg : args)
			_args.add(arg.apply(scope, in));

		final List out = new ArrayList<>();
		combinations(o -> out.add(o), new Stack<>(), 0, _args, in);
		return out;
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy