001 /** 002 * Implements the CFML Function arrayavg 003 */ 004 package railo.runtime.functions.list; 005 006 import java.util.Iterator; 007 008 import railo.commons.lang.CFTypes; 009 import railo.runtime.PageContext; 010 import railo.runtime.exp.ExpressionException; 011 import railo.runtime.exp.PageException; 012 import railo.runtime.ext.function.Function; 013 import railo.runtime.op.Caster; 014 import railo.runtime.type.Array; 015 import railo.runtime.type.FunctionArgument; 016 import railo.runtime.type.UDF; 017 import railo.runtime.type.util.ListUtil; 018 019 020 public final class ListFilter implements Function { 021 022 023 public static String call(PageContext pc , String list, UDF filter) throws PageException { 024 return call(pc, list, filter,","); 025 } 026 027 public static String call(PageContext pc , String list, UDF filter, String delimiter) throws PageException { 028 // check UDF return type 029 int type = filter.getReturnType(); 030 if(type!=CFTypes.TYPE_BOOLEAN && type!=CFTypes.TYPE_ANY) 031 throw new ExpressionException("invalid return type ["+filter.getReturnTypeAsString()+"] for UDF Filter, valid return types are [boolean,any]"); 032 033 // check UDF arguments 034 FunctionArgument[] args = filter.getFunctionArguments(); 035 if(args.length>1) 036 throw new ExpressionException("UDF filter has to many arguments ["+args.length+"], should have at maximum 1 argument"); 037 038 if(delimiter==null) delimiter=","; 039 Array array = ListUtil.listToArrayRemoveEmpty(list, delimiter); 040 041 042 StringBuilder sb=new StringBuilder(); 043 Iterator<Object> it = array.valueIterator(); 044 Object value; 045 while(it.hasNext()){ 046 value=it.next(); 047 if(Caster.toBooleanValue(filter.call(pc, new Object[]{value}, true))){ 048 if(sb.length()>0) sb.append(delimiter); 049 sb.append(value); 050 } 051 } 052 return sb.toString(); 053 } 054 }