001 package railo.runtime.type.util; 002 003 import java.util.ArrayList; 004 import java.util.Iterator; 005 import java.util.List; 006 import java.util.Map.Entry; 007 008 import railo.runtime.op.Operator; 009 import railo.runtime.type.Collection; 010 import railo.runtime.type.Collection.Key; 011 012 public class CollectionUtil { 013 014 private static final Object NULL = new Object(); 015 016 017 public static boolean equals(Collection left, Collection right) { 018 if(left.size()!=right.size()) return false; 019 Iterator<Key> it = left.keyIterator(); 020 Key k; 021 Object l,r; 022 while(it.hasNext()){ 023 k=it.next(); 024 r=right.get(k,NULL); 025 if(r==NULL) return false; 026 l=left.get(k,NULL); 027 if(!Operator.equalsEL(r, l, false, true)) return false; 028 } 029 return true; 030 } 031 032 /*public static String[] toStringArray(Key[] keys) { 033 if(keys==null) return null; 034 String[] arr=new String[keys.length]; 035 for(int i=0;i<keys.length;i++){ 036 arr[i]=keys[i].getString(); 037 } 038 return arr; 039 }*/ 040 041 public static String getKeyList(Iterator<Key> it, String delimiter) { 042 StringBuilder sb=new StringBuilder(it.next().getString()); 043 if(delimiter.length()==1) { 044 char c=delimiter.charAt(0); 045 while(it.hasNext()) { 046 sb.append(c); 047 sb.append(it.next().getString()); 048 } 049 } 050 else { 051 while(it.hasNext()) { 052 sb.append(delimiter); 053 sb.append(it.next().getString()); 054 } 055 } 056 057 058 return sb.toString(); 059 } 060 061 062 public static String getKeyList(Collection coll, String delimiter) { 063 if(coll.size()==0) return ""; 064 return getKeyList(coll.keyIterator(), delimiter); 065 } 066 067 public static Key[] keys(Collection coll) { 068 if(coll==null) return new Key[0]; 069 Iterator<Key> it = coll.keyIterator(); 070 List<Key> rtn=new ArrayList<Key>(); 071 if(it!=null)while(it.hasNext()){ 072 rtn.add(it.next()); 073 } 074 return rtn.toArray(new Key[rtn.size()]); 075 } 076 077 public static String[] keysAsString(Collection coll) { 078 if(coll==null) return new String[0]; 079 Iterator<Key> it = coll.keyIterator(); 080 List<String> rtn=new ArrayList<String>(); 081 if(it!=null)while(it.hasNext()){ 082 rtn.add(it.next().getString()); 083 } 084 return rtn.toArray(new String[rtn.size()]); 085 } 086 087 public static int hashCode(Collection coll) { 088 int hashCode = 1; 089 Iterator<Entry<Key, Object>> it = coll.entryIterator(); 090 Entry<Key, Object> e; 091 while(it.hasNext()) { 092 e = it.next(); 093 hashCode = 31*hashCode+ 094 095 ( 096 (e.getKey()==null?0:e.getKey().hashCode()) ^ 097 (e.getValue()==null ? 0 : e.getValue().hashCode()) 098 ); 099 } 100 return hashCode; 101 } 102 }