001/**
002 *
003 * Copyright (c) 2014, the Railo Company Ltd. All rights reserved.
004 *
005 * This library is free software; you can redistribute it and/or
006 * modify it under the terms of the GNU Lesser General Public
007 * License as published by the Free Software Foundation; either 
008 * version 2.1 of the License, or (at your option) any later version.
009 * 
010 * This library is distributed in the hope that it will be useful,
011 * but WITHOUT ANY WARRANTY; without even the implied warranty of
012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
013 * Lesser General Public License for more details.
014 * 
015 * You should have received a copy of the GNU Lesser General Public 
016 * License along with this library.  If not, see <http://www.gnu.org/licenses/>.
017 * 
018 **/
019/**
020 * Implements the CFML Function arrayavg
021 */
022package lucee.runtime.functions.list;
023
024import lucee.runtime.PageContext;
025import lucee.runtime.exp.FunctionException;
026import lucee.runtime.exp.PageException;
027import lucee.runtime.functions.BIF;
028import lucee.runtime.functions.closure.Every;
029import lucee.runtime.op.Caster;
030import lucee.runtime.type.UDF;
031import lucee.runtime.type.util.StringListData;
032
033
034public final class ListEvery extends BIF {
035
036        private static final long serialVersionUID = -7873096972268260607L;
037
038        public static boolean call(PageContext pc , String list, UDF udf) throws PageException {
039                return _call(pc, list, udf,",",false,true, false, 20);
040        }
041
042        public static boolean call(PageContext pc , String list, UDF udf,String delimiter) throws PageException {
043                return _call(pc, list, udf,delimiter,false,true, false, 20);
044        }
045
046        public static boolean call(PageContext pc , String list, UDF udf,String delimiter, boolean includeEmptyFields) throws PageException {
047                return _call(pc, list, udf,delimiter,includeEmptyFields, true,false, 20);
048        }
049
050        public static boolean call(PageContext pc , String list, UDF udf,String delimiter, boolean includeEmptyFields, boolean multiCharacterDelimiter) throws PageException {
051                return _call(pc, list, udf,delimiter,includeEmptyFields, multiCharacterDelimiter,false, 20);
052        }
053        
054        public static boolean call(PageContext pc , String list, UDF udf,String delimiter, boolean includeEmptyFields, boolean multiCharacterDelimiter, boolean parallel) throws PageException {
055                return _call(pc, list, udf,delimiter,includeEmptyFields,multiCharacterDelimiter, parallel, 20);
056        }
057
058        public static boolean call(PageContext pc , String list, UDF udf,String delimiter, boolean includeEmptyFields, boolean multiCharacterDelimiter, boolean parallel, double maxThreads) throws PageException {
059                return _call(pc, list, udf,delimiter,includeEmptyFields,multiCharacterDelimiter, parallel, (int)maxThreads);
060        }
061        private static boolean _call(PageContext pc , String list, UDF udf,String delimiter, boolean includeEmptyFields, boolean multiCharacterDelimiter, boolean parallel, int maxThreads) throws PageException {
062                StringListData data=new StringListData(list,delimiter,includeEmptyFields,multiCharacterDelimiter);
063                
064                return Every.call(pc, data, udf, parallel, maxThreads);
065        }
066
067        @Override
068        public Object invoke(PageContext pc, Object[] args) throws PageException {
069
070                if(args.length==2)
071                        return call(pc, Caster.toString(args[0]), Caster.toFunction(args[1]));
072                if(args.length==3)
073                        return call(pc, Caster.toString(args[0]), Caster.toFunction(args[1]),Caster.toString(args[2]));
074                if(args.length==4)
075                        return call(pc, Caster.toString(args[0]), Caster.toFunction(args[1]),Caster.toString(args[2]),Caster.toBooleanValue(args[3]));
076                if(args.length==5)
077                        return call(pc, Caster.toString(args[0]), Caster.toFunction(args[1]),Caster.toString(args[2]),Caster.toBooleanValue(args[3]), Caster.toBooleanValue(args[4]));
078                if(args.length==6)
079                        return call(pc, Caster.toString(args[0]), Caster.toFunction(args[1]),Caster.toString(args[2]),Caster.toBooleanValue(args[3]), Caster.toBooleanValue(args[4]), Caster.toBooleanValue(args[5]));
080                if(args.length==7)
081                        return call(pc, Caster.toString(args[0]), Caster.toFunction(args[1]),Caster.toString(args[2]),Caster.toBooleanValue(args[3]), Caster.toBooleanValue(args[4]), Caster.toBooleanValue(args[5]), Caster.toDoubleValue(args[6]));
082                
083                throw new FunctionException(pc, "ListEvery", 2, 7, args.length);
084        }
085}