View Javadoc
1 package com.bonevich.util; 2 3 import java.util.*; 4 5 /*** 6 * Utilities for operating on collections (join, reverse, map, 7 * deep-equals (i.e. not identity), and list-to-map transform). 8 * Creation date: (3/20/00 3:46:25 AM) 9 * @author: jbonevic 10 * @version $Id: CollectionUtilities.html,v 1.1 2009/03/07 17:55:37 jbonevic Exp $ 11 */ 12 public class CollectionUtilities 13 { 14 private CollectionUtilities() {} 15 16 /*** 17 * An Object-safe join method. 18 * Creation date: (3/20/00 3:47:57 AM) 19 * @return String 20 * @param strings java.util.Vector 21 * @param joiner java.lang.String 22 */ 23 public static String join(String joiner, Object[] strings) 24 { 25 if (strings == null) 26 { 27 return ""; 28 } 29 30 return join(joiner, java.util.Arrays.asList(strings)); 31 } 32 33 /*** 34 * An Object-safe join method. 35 * Creation date: (3/20/00 3:47:57 AM) 36 * @return String 37 * @param strings java.util.Vector 38 * @param joiner java.lang.String 39 */ 40 public static String join(String joiner, Collection strings) 41 { 42 if (strings == null) 43 { 44 return ""; 45 } 46 47 StringBuffer buf = new StringBuffer(); 48 String str = null; 49 50 // Iterate thru the vector, stringifying each item and adding it to the buffer 51 // and separating each string item with the joiner string 52 for (Iterator itr = strings.iterator(); itr.hasNext(); ) 53 { 54 Object element=itr.next(); 55 str = (element==null) ? "" : element.toString(); 56 buf.append(str); 57 if ( itr.hasNext() ) 58 { 59 buf.append(joiner); 60 } 61 } 62 return buf.toString(); 63 } 64 65 /*** 66 * Danger! This modifies the List! 67 * Creation date: (5/7/00 14:32:20) 68 * @return java.util.Vector 69 * @param v java.util.Vector 70 */ 71 public static List reverse(List v) 72 { 73 if (v == null) 74 { 75 return null; 76 } 77 Collections.reverse(v); 78 return v; 79 } 80 81 /*** 82 * Actual "equality". 83 * Map.equals() is true if the hashMaps have the same keys, values being irrelevant. 84 * Equal if 85 * Both maps have same key=>value's && are same size. 86 * Thus, null==null, empty==empty 87 * NB: uses value.equal(). You've been warned. 88 * 89 * Creation date: (11/17/00 17:26:46) 90 * @return boolean 91 * @param a java.util.HashMap 92 * @param b java.util.HashMap 93 */ 94 public static boolean equals(Map a, Map b) 95 { 96 // null case 97 if (a == null || b == null) 98 { 99 return a==b; 100 } 101 102 // different sizes 103 if (a.size() != b.size()) 104 { 105 return false; 106 } 107 108 // Each key from a has to be in b 109 // and since they have the same size, that's all the keys 110 for (Iterator itor = a.entrySet().iterator(); itor.hasNext();) 111 { 112 Map.Entry e = (Map.Entry) itor.next(); 113 114 // have to check for "has" because get() may return null 115 Object k=e.getKey(); 116 if (!b.containsKey(k)) 117 { 118 return false; 119 } 120 121 // check for value equality 122 Object va=e.getValue(); 123 Object vb=b.get(k); 124 125 // null case 126 if ((va==null || vb==null) && va!=vb) 127 { 128 return false; 129 } 130 131 // special case Map's 132 if (va instanceof Map) 133 { 134 if (!equals((Map)va,(Map)vb)) 135 { 136 return false; 137 } 138 } 139 else 140 { 141 if(! va.equals(vb)) 142 { 143 return false; 144 } 145 } 146 } 147 return true; 148 } 149 150 /*** 151 * Turns an array of (key, value, key, value....) into a hashMap 152 * Drops the last "key" if there is an odd number of elements. 153 * the "key" is NOT toString()'d 154 * Meant as a convenience for constructing hashes inline: 155 * CollectionUtilities.toHashMap(new Object[] {k,v, k2, v2}) 156 * Creation date: (11/17/00 16:11:36) 157 * @return java.util.HashMap 158 * @param keyValueArray java.lang.Object[] 159 */ 160 public static HashMap toHashMap(Object[] keyValueArray) 161 { 162 if (keyValueArray == null) 163 { 164 return null; 165 } 166 167 int s = keyValueArray.length; 168 HashMap theHash = new HashMap(s); 169 170 for (int i=0; i<s/2; i++) 171 { 172 theHash.put(keyValueArray[i*2], keyValueArray[i*2+1]); 173 } 174 return theHash; 175 } 176 177 /*** 178 * Returns a vector, where each element NEW[i] = theFunctor.function(OLD[i]). 179 * I.e. transforms each element, giving a new vector. 180 * Takes any Collection, only uses the iterator() to obtain each object, and size(). 181 * Returns null if iterable is null. 182 * NB: theFunctor.function(Object o) should handle the case of o==null. 183 * Creation date: (3/20/00 3:47:57 AM) 184 * @return String 185 * @param strings java.util.Vector 186 * @param joiner java.lang.String 187 */ 188 public static Vector map(Functor f, Collection iterable) 189 //throws FunctorException 190 { 191 if (iterable == null) 192 { 193 return null; 194 } 195 196 Vector result = new Vector(iterable.size()); 197 for (Iterator itr = iterable.iterator(); itr.hasNext(); ) 198 { 199 Object r = f.function(itr.next()); 200 result.addElement(r); 201 } 202 return result; 203 } 204 205 /*** 206 * Map across an Array 207 * Creation date: (3/20/00 3:47:57 AM) 208 * @return String 209 * @param strings java.util.Vector 210 * @param joiner java.lang.String 211 */ 212 public static Vector map(Functor f, Object[] anArray) 213 //throws FunctorException 214 { 215 if (anArray == null) 216 { 217 return null; 218 } 219 220 return map(f, java.util.Arrays.asList(anArray)); 221 } 222 223 /*** 224 * Map across a Map: simply calls the functor with each Map.EntrySet (see JoinMapEntry for example) 225 * Creation date: (3/20/00 3:47:57 AM) 226 * @return String 227 * @param strings java.util.Vector 228 * @param joiner java.lang.String 229 */ 230 public static Vector map(Functor f, Map aMap) 231 //throws FunctorException 232 { 233 if (aMap == null) 234 { 235 return null; 236 } 237 238 return map(f, aMap.entrySet()); 239 } 240 241 }

This page was automatically generated by Maven