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

io.tiler.internal.queries.expressions.functions.ListFunction Maven / Gradle / Ivy

There is a newer version: 0.1.28
Show newest version
package io.tiler.internal.queries.expressions.functions;

import io.tiler.internal.queries.EvaluationException;
import io.tiler.internal.queries.EvaluationContext;
import io.tiler.internal.queries.QueryContext;
import io.tiler.internal.queries.expressions.Expression;

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

public abstract class ListFunction extends Function {
  private final Expression list;
  private final Class itemClass;

  public ListFunction(QueryContext queryContext, Expression list, Class itemClass) {
    super(queryContext);
    this.list = list;
    this.itemClass = itemClass;
  }

  public Expression list() {
    return list;
  }

  @Override
  public Object evaluate(EvaluationContext context) throws EvaluationException {
    Object list = this.list.evaluate(context);

    if (!(list instanceof List)) {
      throw new EvaluationException(queryContext(), "list must evaluate to a List");
    }

    List list2 = (List) list;
    List typedList = new ArrayList<>(list2.size());

    for (Object item : list2) {
      if (item != null && !itemClass.isInstance(item)) {
        throw new EvaluationException(queryContext(), "Each item in list must be a '" + itemClass.getName() + "'");
      }

      typedList.add((T) item);
    }

    return applyToList(typedList);
  }

  public abstract Object applyToList(List list) throws EvaluationException;
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy