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

net.thisptr.jackson.jq.internal.tree.fieldaccess.resolved.ResolvedIndexFieldAccess Maven / Gradle / Ivy

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

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

import net.thisptr.jackson.jq.Scope;
import net.thisptr.jackson.jq.exception.JsonQueryException;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.NullNode;

public class ResolvedIndexFieldAccess extends ResolvedFieldAccess {
	private List indices;

	public ResolvedIndexFieldAccess(final boolean permissive, final List indices) {
		super(permissive);
		this.indices = indices;
	}

	@Override
	public List apply(final Scope scope, final JsonNode in) throws JsonQueryException {
		final List out = new ArrayList<>();
		for (final long _index : indices) {
			if (in.isArray()) {
				final long index = _index < 0 ? _index + in.size() : _index;
				if (0 <= index && index < in.size()) {
					out.add(in.get((int) index));
				} else {
					out.add(NullNode.getInstance());
				}
			} else if (in.isNull()) {
				out.add(NullNode.getInstance());
			} else {
				if (!permissive)
					throw JsonQueryException.format("Cannot index %s with number", in.getNodeType());
			}
		}
		return out;
	}

	public List indices() {
		return Collections.unmodifiableList(indices);
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy