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

liqp.nodes.FilterNode Maven / Gradle / Ivy

package liqp.nodes;

import liqp.filters.Filter;

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

public class FilterNode implements LNode {

    private Filter filter;
    private List params;

    public FilterNode(String filterName, Filter filter) {
        if (filter == null) {
            throw new IllegalArgumentException("no filter available  named: " + filterName);
        }
        this.filter = filter;
        this.params = new ArrayList();
    }

    public void add(LNode param) {
        params.add(param);
    }

    public Object apply(Object value, Map variables) {

        List paramValues = new ArrayList();

        for (LNode node : params) {
            paramValues.add(node.render(variables));
        }

        return filter.apply(value, paramValues.toArray(new Object[paramValues.size()]));
    }

    @Override
    public Object render(Map context) {
        throw new RuntimeException("cannot render a filter");
    }
}