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 listrest
021 */
022package lucee.runtime.functions.list;
023
024import java.util.HashSet;
025import java.util.Iterator;
026import java.util.Set;
027import java.util.TreeSet;
028
029import lucee.runtime.PageContext;
030import lucee.runtime.exp.FunctionException;
031import lucee.runtime.exp.PageException;
032import lucee.runtime.functions.BIF;
033import lucee.runtime.op.Caster;
034import lucee.runtime.type.Array;
035import lucee.runtime.type.util.ListUtil;
036
037public final class ListRemoveDuplicates extends BIF {
038        
039        private static final long serialVersionUID = -6596215135126751629L;
040        
041        public static String call(PageContext pc , String list) throws PageException {
042
043                return call(pc, list, ",", false);
044        }
045
046        public static String call(PageContext pc, String list, String delimiter) throws PageException {
047
048                return call(pc, list, delimiter, false);
049        }
050
051        public static String call(PageContext pc , String list, String delimiter, boolean ignoreCase) throws PageException {
052                if(delimiter==null) delimiter=",";
053                Array array = ListUtil.listToArrayRemoveEmpty(list, delimiter);
054
055                Set<String> existing;
056                if (ignoreCase)
057                        existing = new TreeSet<String>(String.CASE_INSENSITIVE_ORDER);
058                else
059                        existing = new HashSet<String>();
060
061                StringBuilder sb=new StringBuilder();
062                //Key[] keys = array.keys();
063                Iterator<Object> it = array.valueIterator();
064                String value;
065
066                while(it.hasNext()){
067
068                        value=Caster.toString(it.next());
069
070                        if(!existing.contains(value)) {
071
072                                sb.append(value);
073                                existing.add(value);
074
075                                if(it.hasNext())
076                                        sb.append(delimiter);
077                        }
078                }
079
080                return sb.toString();
081        }
082        
083    @Override
084        public Object invoke(PageContext pc, Object[] args) throws PageException {
085        if(args.length==1)
086                        return call(pc, Caster.toString(args[0]));
087        if(args.length==2)
088                        return call(pc, Caster.toString(args[0]), Caster.toString(args[1]));
089        if(args.length==3)
090                        return call(pc, Caster.toString(args[0]), Caster.toString(args[1]), Caster.toBooleanValue(args[2]));
091        
092                throw new FunctionException(pc, "ListRemoveDuplicates", 2, 5, args.length);
093        }
094}