public void testToString() throws Exception { FunctionImpl func = new FunctionImpl(); Expression param = new LiteralExpressionImpl(42); func.setName("TestFunction"); func.setParameters(Collections.singletonList(param)); String result = func.toString(); assertEquals("TestFunction([42])", result); }
/** * Convenience method for creating a function name. * * @see FunctionImpl#functionName(String, String, String...) */ protected static FunctionName functionName(String name, String ret, String... args) { return FunctionImpl.functionName(name, ret, args); }
LinkedHashMap<String, Object> prepped = new LinkedHashMap<String, Object>(); List<Parameter<?>> args = getFunctionName().getArguments(); List<Expression> expr = getParameters();
/** * Creates a String representation of this Function with the function name and the arguments. * The String created should be good for most subclasses */ // Copied from FunctionExpressionImpl KS public String toString() { StringBuilder sb = new StringBuilder(); sb.append(getName()); sb.append("("); List<org.opengis.filter.expression.Expression> params = getParameters(); if (params != null) { org.opengis.filter.expression.Expression exp; for (Iterator<org.opengis.filter.expression.Expression> it = params.iterator(); it.hasNext(); ) { exp = it.next(); sb.append("["); sb.append(exp); sb.append("]"); if (it.hasNext()) { sb.append(", "); } } } sb.append(")"); return sb.toString(); }
Function newFunction(List<Expression> parameters, Literal fallback) throws Exception { // cache lookup if (FunctionExpression.class.isAssignableFrom(clazz)) { FunctionExpression function = (FunctionExpression) clazz.newInstance(); if (parameters != null) { function.setParameters(parameters); } if (fallback != null) { function.setFallbackValue(fallback); } return function; } if (FunctionImpl.class.isAssignableFrom(clazz)) { FunctionImpl function = (FunctionImpl) clazz.newInstance(); if (parameters != null) { function.setParameters(parameters); } if (fallback != null) { function.setFallbackValue(fallback); } return function; } // Function function = (Function) functionClass.newInstance(); Constructor<Function> constructor = clazz.getConstructor(new Class[] {List.class, Literal.class}); return constructor.newInstance(parameters, fallback); } }
f.setName("lower"); f.setParameters(Arrays.asList(left)); f.accept(this, Arrays.asList(leftContext)); f.setParameters(Arrays.asList(right)); f.accept(this, Arrays.asList(rightContext));
for (Iterator i = functions.iterator(); i.hasNext();) { FunctionImpl function = (FunctionImpl) i.next(); functionImplCache.put(function.getName().toLowerCase(), function.getClass()); FunctionImpl function = (FunctionImpl) clazz.newInstance(); if(parameters != null) function.setParameters(parameters); return function;
public void testTooFewArguments() throws Exception { StringInFunction f = new StringInFunction(); List params = Arrays.asList(ff.literal("foo"), ff.literal(true)); ((FunctionImpl) f).setParameters(params); try { f.evaluate(null); fail(); } catch (IllegalArgumentException e) { } } }
/** * This default implementation just returns {@code this} if the number of expected parameters is * zero, otherwise throws an {@link IllegalArgumentException}. * * <p>A subclass that do expect {@link Expression} parameters shall override this method and * return a new instance of the same kind of InternalFunction configured to work against the * given {@code parameters}. * * @see * org.opengis.filter.expression.InternalFunction#duplicate(org.opengis.filter.expression.Expression[]) */ @Override public InternalFunction duplicate(Expression... parameters) { final int expectedParams = super.getParameters().size(); if (expectedParams > 0) { throw new IllegalArgumentException( "This method must be overriten by subclasses that expect Expression arguments"); } return this; }
Parameter<?> parameter = getFunctionName().getArguments().get(argumentIndex); if (params.size() <= argumentIndex) { if (parameter.getMinOccurs() == 0) {
f.setName("lower"); f.setParameters(Arrays.asList(left)); f.accept(this, Arrays.asList(leftContext)); f.setParameters(Arrays.asList(right)); f.accept(this, Arrays.asList(rightContext));
Function newFunction(List<Expression> parameters, Literal fallback) throws Exception { // cache lookup if (FunctionExpression.class.isAssignableFrom(clazz)) { FunctionExpression function = (FunctionExpression) clazz.newInstance(); if(parameters != null) { function.setParameters(parameters); } if( fallback != null && function instanceof ClassificationFunction){ ClassificationFunction classification = (ClassificationFunction) function; classification.setFallbackValue( fallback ); } return function; } if(FunctionImpl.class.isAssignableFrom(clazz)) { FunctionImpl function = (FunctionImpl) clazz.newInstance(); if(parameters != null){ function.setParameters( (List) parameters ); } if(fallback != null) function.setFallbackValue( fallback ); return function; } //Function function = (Function) functionClass.newInstance(); Constructor<Function> constructor = clazz.getConstructor( new Class[]{ List.class, Literal.class} ); return constructor.newInstance( parameters, fallback ); } }
public void test() throws Exception { StringInFunction f = new StringInFunction(); List params = Arrays.asList( ff.literal("foo"), ff.literal(true), ff.literal("foo"), ff.literal("bar"), ff.literal("baz")); ((FunctionImpl) f).setParameters(params); assertEquals(Boolean.TRUE, f.evaluate(null)); params = Arrays.asList( ff.literal("foo"), ff.literal(true), ff.literal("FOO"), ff.literal("bar"), ff.literal("baz")); ((FunctionImpl) f).setParameters(params); assertEquals(Boolean.FALSE, f.evaluate(null)); }
public synchronized FunctionName getFunctionName() { if (functionName == null) { functionName = new FunctionNameImpl(name, getParameters().size()); } return functionName; }
/** * Validates the structure of arguments, basically enforcing java conventions for variable * level arguments. */ private void validateArguments() throws IllegalArgumentException { List<Parameter<?>> args = getFunctionName().getArguments(); for (int i = 0; i < args.size(); i++) { Parameter<?> arg = args.get(i); if (arg.getMaxOccurs() == 0) { throw new IllegalArgumentException(String.format("Argument %s has zero max")); } if (arg.getMinOccurs() != 1 || arg.getMaxOccurs() != 1) { //this can only happen for the last argument if (i != args.size()-1) { throw new IllegalArgumentException(String.format("Argument %s(%d,%d) invalid." + " Variable arguments must be the last argument of function.", arg.getName(), arg.getMinOccurs(), arg.getMaxOccurs())); } } } }
LinkedHashMap<String, Object> prepped = new LinkedHashMap<String, Object>(); List<Parameter<?>> args = getFunctionName().getArguments(); List<Expression> expr = getParameters();
public synchronized FunctionName getFunctionName() { if( functionName == null ){ functionName = new FunctionNameImpl(name,getParameters().size()); } return functionName; }
/** * Convenience method for creating a function name. * @see FunctionImpl#functionName(String, String, String...) */ protected static FunctionName functionName(String name, String ret, String... args) { return FunctionImpl.functionName(name, ret, args); }
/** Sets the function impl. */ private void setFunctionImpl() { FunctionImpl functionExpression = (FunctionImpl) this.expression; FunctionName functionName = functionExpression.getFunctionName(); TypeManager.getInstance().setDataType(functionName.getReturn().getType()); int maxArgument = Math.abs(functionName.getArgumentCount()); for (int index = 0; index < maxArgument; index++) { ExpressionNode childNode = new ExpressionNode(); Parameter<?> parameter = functionName.getArguments().get(0); childNode.setType(parameter.getType()); childNode.setName(parameter.getName()); if (index < functionExpression.getParameters().size()) { childNode.setExpression(functionExpression.getParameters().get(index)); } this.insert(childNode, this.getChildCount()); } }
public void testFunctionName() throws Exception { FunctionName fn = FunctionImpl.functionName( "foo", "bar:Integer", "a", "x:String:1,1", "y:MultiPolygon", "z:java.util.Date:1,"); assertEquals("foo", fn.getName()); check(fn.getReturn(), "bar", Integer.class, 1, 1); check(fn.getArguments().get(0), "a", Object.class, 1, 1); check(fn.getArguments().get(1), "x", String.class, 1, 1); check(fn.getArguments().get(2), "y", MultiPolygon.class, 1, 1); check(fn.getArguments().get(3), "z", Date.class, 1, -1); fn = FunctionImpl.functionName("foo", "a", "geom::1,1", "b:Object:,"); check(fn.getArguments().get(0), "geom", Geometry.class, 1, 1); check(fn.getArguments().get(1), "b", Object.class, -1, -1); fn = FunctionImpl.functionName("foo", "value", "geom::,"); check(fn.getArguments().get(0), "geom", Geometry.class, -1, -1); }