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 structkeylist
021 */
022package lucee.runtime.functions.struct;
023
024import java.util.Iterator;
025
026import lucee.runtime.PageContext;
027import lucee.runtime.exp.PageException;
028import lucee.runtime.functions.BIF;
029import lucee.runtime.op.Caster;
030import lucee.runtime.type.Collection.Key;
031import lucee.runtime.type.Struct;
032
033public final class StructKeyList extends BIF {
034        
035        private static final long serialVersionUID = 6256709521354910213L;
036
037        public static String call(PageContext pc , Struct struct) {
038                return call(pc,struct,",");//KeyImpl.toUpperCaseList(struct.keys(), ",");
039        }
040        public static String call(PageContext pc , Struct struct, String delimiter) {
041                //return KeyImpl.toList(CollectionUtil.keys(struct), delimiter);
042                
043                if(struct==null) return "";
044                Iterator<Key> it = struct.keyIterator();
045                
046                // first
047                if(!it.hasNext()) return "";
048                StringBuilder sb=new StringBuilder();
049                sb.append(it.next().getString());
050                
051                // rest
052                if(delimiter.length()==1) {
053                        char c=delimiter.charAt(0);
054                        while(it.hasNext()){
055                                sb.append(c);
056                                sb.append(it.next().getString());
057                        }
058                }
059                else {
060                        while(it.hasNext()){
061                                sb.append(delimiter);
062                                sb.append(it.next().getString());
063                        }
064                }
065                
066                return sb.toString();
067        }
068        
069        @Override
070        public Object invoke(PageContext pc, Object[] args) throws PageException {
071                if(args.length==2) return call(pc,Caster.toStruct(args[0]),Caster.toString(args[1]));
072                return call(pc,Caster.toStruct(args[0]));
073        }
074}