001 package railo.runtime.functions.arrays; 002 003 004 import railo.runtime.PageContext; 005 import railo.runtime.exp.CasterException; 006 import railo.runtime.exp.FunctionException; 007 import railo.runtime.exp.PageException; 008 import railo.runtime.functions.BIF; 009 import railo.runtime.op.Caster; 010 import railo.runtime.op.Decision; 011 import railo.runtime.op.Operator; 012 import railo.runtime.type.Array; 013 import railo.runtime.type.Closure; 014 import railo.runtime.type.UDF; 015 016 public final class ArrayFind extends BIF { 017 018 private static final long serialVersionUID = -3282048672805234115L; 019 020 public static double call(PageContext pc , Array array, Object value) throws PageException { 021 if(value instanceof UDF) 022 return find(pc,array,(UDF)value); 023 024 025 return find(array,value,true); 026 } 027 028 @Override 029 public Object invoke(PageContext pc, Object[] args) throws PageException { 030 return call(pc,Caster.toArray(args[0]),args[1]); 031 } 032 033 public static int find(PageContext pc ,Array array, UDF udf) throws PageException { 034 int len=array.size(); 035 036 Object[] arr=new Object[1]; 037 Object res; 038 Boolean b; 039 for(int i=1;i<=len;i++) { 040 arr[0]=array.get(i,null); 041 if(arr[0]!=null) { 042 res=udf.call(pc, arr, false); 043 b=Caster.toBoolean(res,null); 044 if(b==null) throw new FunctionException(pc,"ArrayFind",2,"function","return value of the "+(udf instanceof Closure?"closure":"function ["+udf.getFunctionName()+"]")+" cannot be casted to a boolean value.",CasterException.createMessage(res, "boolean")); 045 if(b.booleanValue())return i; 046 } 047 } 048 return 0; 049 } 050 051 public static int find(Array array, Object value, boolean caseSensitive) { 052 int len=array.size(); 053 boolean valueIsSimple=Decision.isSimpleValue(value); 054 Object o; 055 for(int i=1;i<=len;i++) { 056 o=array.get(i,null); 057 if(o!=null && Operator.equalsEL(o, value,caseSensitive,!valueIsSimple)) return i; 058 } 059 return 0; 060 } 061 }