
com.mitchellbosecke.pebble.extension.core.RangeFunction Maven / Gradle / Ivy
/*******************************************************************************
* This file is part of Pebble.
*
* Copyright (c) 2014 by Mitchell Bösecke
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
******************************************************************************/
package com.mitchellbosecke.pebble.extension.core;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import com.mitchellbosecke.pebble.extension.Function;
/**
* Range function to iterate over long or a string with a length of 1.
*
* @author Eric Bussieres
*/
public class RangeFunction implements Function {
public static final String FUNCTION_NAME = "range";
private static final String PARAM_END = "end";
private static final String PARAM_INCREMENT = "increment";
private static final String PARAM_START = "start";
private final List argumentNames = new ArrayList<>();
public RangeFunction() {
this.argumentNames.add(PARAM_START);
this.argumentNames.add(PARAM_END);
this.argumentNames.add(PARAM_INCREMENT);
}
@Override
public Object execute(Map args) {
Object start = args.get(PARAM_START);
Object end = args.get(PARAM_END);
Object increment = (Object) args.get(PARAM_INCREMENT);
if (increment == null) {
increment = 1L;
}
else if (!(increment instanceof Number)) {
throw new IllegalArgumentException("The increment of the range function must be a number " + increment);
}
Long incrementNum = ((Number) increment).longValue();
List
© 2015 - 2025 Weber Informatics LLC | Privacy Policy