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

com.groupbyinc.common.jackson.jq.internal.tree.Conditional Maven / Gradle / Ivy

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

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

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.JsonNodeUtils;
import net.thisptr.jackson.jq.internal.misc.Pair;

import com.fasterxml.jackson.databind.JsonNode;

public class Conditional extends JsonQuery {
	private JsonQuery otherwise;
	private List> switches;

	public Conditional(final List> switches, final JsonQuery otherwise) {
		this.switches = switches;
		this.otherwise = otherwise;
	}

	private void applyRecursive(final List out, final Scope scope, final List> switches, final JsonNode in) throws JsonQueryException {
		final Pair sw = switches.get(0);
		final List rs = sw._1.apply(scope, in);
		for (final JsonNode r : rs) {
			if (JsonNodeUtils.asBoolean(r)) {
				out.addAll(sw._2.apply(scope, in));
			} else {
				if (switches.size() > 1) {
					applyRecursive(out, scope, switches.subList(1, switches.size()), in);
				} else {
					out.addAll(otherwise.apply(scope, in));
				}
			}
		}
	}

	@Override
	public List apply(final Scope scope, final JsonNode in) throws JsonQueryException {
		final List out = new ArrayList<>();
		applyRecursive(out, scope, switches, in);
		return out;
	}

	@Override
	public String toString() {
		String ifstr = "if";
		final StringBuilder builder = new StringBuilder();
		for (final Pair sw : switches) {
			builder.append(ifstr);
			builder.append(" ");
			builder.append(sw._1 != null ? sw._1 : "null");
			builder.append(" ");
			builder.append("then");
			builder.append(" ");
			builder.append(sw._2 != null ? sw._2 : "null");
			builder.append(" ");
			ifstr = "elif";
		}
		builder.append("else ");
		builder.append(otherwise != null ? otherwise : "null");
		builder.append(" ");
		builder.append("end");
		return builder.toString();
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy