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.ArrayImpl;
014    import railo.runtime.type.Closure;
015    import railo.runtime.type.UDF;
016    
017    public final class ArrayFindAll extends BIF {
018    
019            private static final long serialVersionUID = -1757019034608924098L;
020    
021            public static Array call(PageContext pc , Array array, Object value) throws PageException {
022            if(value instanceof UDF)
023                    return find(pc,array,(UDF)value);
024                    return find(array,value,true);
025        }
026            
027            @Override
028            public Object invoke(PageContext pc, Object[] args) throws PageException {
029                    return call(pc,Caster.toArray(args[0]),args[1]);
030            }
031            
032    
033        public static Array find(PageContext pc , Array array, UDF udf) throws PageException {
034            Array rtn=new ArrayImpl();
035            int len=array.size();
036            
037            Object[] arr=new Object[1];
038            Object res;
039            Boolean b;
040            for(int i=1;i<=len;i++) {
041                arr[0]=array.get(i,null);
042                if(arr[0]!=null) {
043                    res=udf.call(pc, arr, false);
044                    b=Caster.toBoolean(res,null);
045                    if(b==null) throw new FunctionException(pc,"ArrayFindAll",2,"function","return value of the "+(udf instanceof Closure?"closure":"function ["+udf.getFunctionName()+"]")+" cannot be casted to a boolean value.",CasterException.createMessage(res, "boolean"));
046                    if(b.booleanValue()) {
047                            rtn.appendEL(Caster.toDouble(i));
048                    }
049                }
050            }
051            return rtn;
052        }
053            
054        public static Array find(Array array, Object value, boolean caseSensitive) throws PageException {
055            Array rtn=new ArrayImpl();
056            int len=array.size();
057            boolean valueIsSimple=Decision.isSimpleValue(value);
058            Object o;
059            for(int i=1;i<=len;i++) {
060                o=array.get(i,null);
061                if(o!=null && Operator.equals(o, value,caseSensitive,!valueIsSimple)) {
062                    rtn.appendEL(Caster.toDouble(i));
063                }
064            }
065            return rtn;
066        }
067    }