001    /**
002     * Implements the CFML Function listrest
003     */
004    package railo.runtime.functions.list;
005    
006    import java.util.HashSet;
007    import java.util.Iterator;
008    import java.util.Set;
009    
010    import railo.runtime.PageContext;
011    import railo.runtime.exp.PageException;
012    import railo.runtime.ext.function.Function;
013    import railo.runtime.op.Caster;
014    import railo.runtime.type.Array;
015    import railo.runtime.type.util.ListUtil;
016    
017    public final class ListRemoveDuplicates implements Function {
018            
019            private static final long serialVersionUID = -6596215135126751629L;
020            
021            public static String call(PageContext pc , String list) throws PageException {
022                    return call(pc,list, ",");
023            }
024            public static String call(PageContext pc , String list, String delimiter) throws PageException {
025                    if(delimiter==null) delimiter=",";
026                    Array array = ListUtil.listToArrayRemoveEmpty(list, delimiter);
027                    Set<String> existing=new HashSet<String>();
028                    StringBuilder sb=new StringBuilder();
029                    //Key[] keys = array.keys();
030                    Iterator<Object> it = array.valueIterator();
031                    String value;
032                    while(it.hasNext()){
033                            value=Caster.toString(it.next());
034                            if(existing.contains(value)) continue;
035                            
036                            existing.add(value);
037                            if(sb.length()>0) sb.append(delimiter);
038                            sb.append(value);
039                            
040                    }
041                    return sb.toString();
042            }
043    }