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.Map;
029import lucee.runtime.op.Caster;
030import lucee.runtime.type.Array;
031import lucee.runtime.type.UDF;
032import lucee.runtime.type.util.ListUtil;
033import lucee.runtime.type.util.StringListData;
034
035
036public final class ListMap extends BIF {
037
038        /**
039         * 
040         */
041        private static final long serialVersionUID = 259806095458506715L;
042
043
044        public static String call(PageContext pc , String list, UDF filter) throws PageException {
045                return call(pc, list, filter, ",", false, true,false, 20);
046        }
047
048        public static String call(PageContext pc , String list, UDF filter,String delimiter) throws PageException {
049                return call(pc, list, filter, delimiter, false,true, false, 20);
050        }
051
052        public static String call(PageContext pc , String list, UDF filter,String delimiter
053                        , boolean includeEmptyFields) throws PageException {
054                return call(pc, list, filter, delimiter, includeEmptyFields, true,false, 20);
055        }
056        
057        public static String call(PageContext pc , String list, UDF filter,String delimiter
058                        , boolean includeEmptyFields, boolean multiCharacterDelimiter) throws PageException {
059                return call(pc, list, filter, delimiter, includeEmptyFields, multiCharacterDelimiter,false, 20);
060        }
061        
062        public static String call(PageContext pc , String list, UDF filter,String delimiter
063                        , boolean includeEmptyFields, boolean multiCharacterDelimiter, boolean parallel) throws PageException {
064                return call(pc, list, filter, delimiter, includeEmptyFields,multiCharacterDelimiter, parallel, 20);
065        }
066
067        public static String call(PageContext pc , String list, UDF filter,String delimiter
068                        , boolean includeEmptyFields, boolean multiCharacterDelimiter, boolean parallel, double maxThreads) throws PageException {
069                
070                return ListUtil.arrayToList(
071                                (Array)Map.call(pc, new StringListData(list,delimiter,includeEmptyFields,multiCharacterDelimiter), filter, parallel, maxThreads), delimiter);
072        }
073        
074
075        @Override
076        public Object invoke(PageContext pc, Object[] args) throws PageException {
077
078                if(args.length==2)
079                        return call(pc, Caster.toString(args[0]), Caster.toFunction(args[1]));
080                if(args.length==3)
081                        return call(pc, Caster.toString(args[0]), Caster.toFunction(args[1]),Caster.toString(args[2]));
082                if(args.length==4)
083                        return call(pc, Caster.toString(args[0]), Caster.toFunction(args[1]),Caster.toString(args[2]),Caster.toBooleanValue(args[3]));
084                if(args.length==5)
085                        return call(pc, Caster.toString(args[0]), Caster.toFunction(args[1]),Caster.toString(args[2]),Caster.toBooleanValue(args[3]), Caster.toBooleanValue(args[4]));
086                if(args.length==6)
087                        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]));
088                if(args.length==7)
089                        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]));
090                
091                throw new FunctionException(pc, "ListMap", 2, 7, args.length);
092        }
093}