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 **/ 019package lucee.runtime.type.util; 020 021import java.util.ArrayList; 022import java.util.Iterator; 023import java.util.List; 024import java.util.Map; 025import java.util.Set; 026 027import lucee.commons.lang.ExceptionUtil; 028import lucee.runtime.op.Operator; 029import lucee.runtime.type.Collection; 030import lucee.runtime.type.Collection.Key; 031import lucee.runtime.type.KeyImpl; 032 033public class CollectionUtil { 034 035 private static final Object NULL = new Object(); 036 037 038 public static boolean equals(Collection left, Collection right) { 039 if(left.size()!=right.size()) return false; 040 Iterator<Key> it = left.keyIterator(); 041 Key k; 042 Object l,r; 043 while(it.hasNext()){ 044 k=it.next(); 045 r=right.get(k,NULL); 046 if(r==NULL) return false; 047 l=left.get(k,NULL); 048 if(!Operator.equalsEL(r, l, false, true)) return false; 049 } 050 return true; 051 } 052 053 /*public static String[] toStringArray(Key[] keys) { 054 if(keys==null) return null; 055 String[] arr=new String[keys.length]; 056 for(int i=0;i<keys.length;i++){ 057 arr[i]=keys[i].getString(); 058 } 059 return arr; 060 }*/ 061 062 public static String getKeyList(Iterator<Key> it, String delimiter) { 063 StringBuilder sb=new StringBuilder(it.next().getString()); 064 if(delimiter.length()==1) { 065 char c=delimiter.charAt(0); 066 while(it.hasNext()) { 067 sb.append(c); 068 sb.append(it.next().getString()); 069 } 070 } 071 else { 072 while(it.hasNext()) { 073 sb.append(delimiter); 074 sb.append(it.next().getString()); 075 } 076 } 077 return sb.toString(); 078 } 079 080 081 public static String getKeyList(Collection coll, String delimiter) { 082 if(coll.size()==0) return ""; 083 return getKeyList(coll.keyIterator(), delimiter); 084 } 085 086 public static Key[] keys(Collection coll) { 087 if(coll==null) return new Key[0]; 088 Iterator<Key> it = coll.keyIterator(); 089 List<Key> rtn=new ArrayList<Key>(); 090 if(it!=null)while(it.hasNext()){ 091 rtn.add(it.next()); 092 } 093 return rtn.toArray(new Key[rtn.size()]); 094 } 095 096 public static String[] keysAsString(Collection coll) { 097 if(coll==null) return new String[0]; 098 Iterator<Key> it = coll.keyIterator(); 099 List<String> rtn=new ArrayList<String>(); 100 if(it!=null)while(it.hasNext()){ 101 rtn.add(it.next().getString()); 102 } 103 return rtn.toArray(new String[rtn.size()]); 104 } 105 106 public static boolean isEmpty(Map<?, ?> map) { 107 return map==null || map.size()==0; 108 } 109 110 /*public static int hashCode(Collection coll) { produce infiniti loop when there is a refrerence to itself or a anchestor 111 112 int hashCode = 1; 113 Iterator<Entry<Key, Object>> it = coll.entryIterator(); 114 Entry<Key, Object> e; 115 while(it.hasNext()) { 116 e = it.next(); 117 hashCode = 31*hashCode+ 118 119 ( 120 (e.getKey()==null?0:e.getKey().hashCode()) ^ 121 (e.getValue()==null ? 0 : e.getValue().hashCode()) 122 ); 123 } 124 return hashCode; 125 }*/ 126 127 public static Collection.Key[] toKeys(String[] strArr, boolean trim) { 128 Collection.Key[] keyArr=new Collection.Key[strArr.length]; 129 for(int i=0 ;i<keyArr.length;i++) { 130 keyArr[i]=KeyImpl.init(trim?strArr[i].trim():strArr[i]); 131 } 132 return keyArr; 133 } 134 135 public static Collection.Key[] toKeys(Set<String> set) { 136 Collection.Key[] keyArr=new Collection.Key[set.size()]; 137 Iterator<String> it = set.iterator(); 138 int index=0; 139 while(it.hasNext()){ 140 keyArr[index++]=KeyImpl.init(it.next()); 141 } 142 return keyArr; 143 } 144 145 public static <T> T remove(List<T> list,int index, T defaultValue) { 146 try{ 147 return list.remove(index); 148 } 149 catch(Throwable t){ 150 ExceptionUtil.rethrowIfNecessary(t); 151 return defaultValue; 152 } 153 } 154}