001    package railo.runtime.functions.arrays;
002    
003    import railo.runtime.PageContext;
004    import railo.runtime.exp.ExpressionException;
005    import railo.runtime.exp.FunctionException;
006    import railo.runtime.exp.PageException;
007    import railo.runtime.functions.BIF;
008    import railo.runtime.op.Caster;
009    import railo.runtime.type.Array;
010    import railo.runtime.type.ArrayImpl;
011    
012    public final class ArrayMid extends BIF {
013    
014            private static final long serialVersionUID = 4996354700884413289L;
015    
016            public static Array call(PageContext pc , Array arr, double start) throws ExpressionException {
017                    return call(pc,arr,start,-1);
018            }
019            
020            public static Array call(PageContext pc , Array arr, double start, double count) throws ExpressionException {
021                    int s=(int) start;
022                    int c=(int) count;
023                    
024                    if(s<1) throw new FunctionException(pc, "ArrayMid", 2, "start", "Parameter which is now ["+s+"] must be a positive integer");
025                    if(c==-1) c=arr.size();
026                    else if(c<-1) throw new FunctionException(pc, "ArrayMid", 3, "count", "Parameter which is now ["+c+"] must be a non-negative integer or -1 (for string length)");
027                    c+=s-1;
028                    if(s>arr.size()) return new ArrayImpl();
029                    
030                    ArrayImpl rtn = new ArrayImpl();
031                    int len = arr.size();
032                    Object value;
033                    for(int i=s;i<=c && i<=len ;i++){
034                            value=arr.get(i, null);
035                            rtn.appendEL(value);
036                    }
037                    return rtn;
038            }
039            
040            @Override
041            public Object invoke(PageContext pc, Object[] args) throws PageException {
042                    if(args.length==2)return call(pc,Caster.toArray(args[0]),Caster.toDoubleValue(args[1]));
043                    return call(pc,Caster.toArray(args[0]),Caster.toDoubleValue(args[1]),Caster.toDoubleValue(args[2]));
044            }
045    }