1 16 package org.apache.commons.collections; 17 18 import java.util.ArrayList ; 19 import java.util.Collection ; 20 import java.util.Collections ; 21 import java.util.Iterator ; 22 import java.util.List ; 23 24 import org.apache.commons.collections.list.FixedSizeList; 25 import org.apache.commons.collections.list.LazyList; 26 import org.apache.commons.collections.list.PredicatedList; 27 import org.apache.commons.collections.list.SynchronizedList; 28 import org.apache.commons.collections.list.TransformedList; 29 import org.apache.commons.collections.list.TypedList; 30 import org.apache.commons.collections.list.UnmodifiableList; 31 32 45 public class ListUtils { 46 47 52 public static final List EMPTY_LIST = Collections.EMPTY_LIST; 53 54 57 public ListUtils() { 58 } 59 60 70 public static List intersection(final List list1, final List list2) { 71 final ArrayList result = new ArrayList (); 72 final Iterator iterator = list2.iterator(); 73 74 while (iterator.hasNext()) { 75 final Object o = iterator.next(); 76 77 if (list1.contains(o)) { 78 result.add(o); 79 } 80 } 81 82 return result; 83 } 84 85 100 public static List subtract(final List list1, final List list2) { 101 final ArrayList result = new ArrayList (list1); 102 final Iterator iterator = list2.iterator(); 103 104 while (iterator.hasNext()) { 105 result.remove(iterator.next()); 106 } 107 108 return result; 109 } 110 111 120 public static List sum(final List list1, final List list2) { 121 return subtract(union(list1, list2), intersection(list1, list2)); 122 } 123 124 134 public static List union(final List list1, final List list2) { 135 final ArrayList result = new ArrayList (list1); 136 result.addAll(list2); 137 return result; 138 } 139 140 169 public static boolean isEqualList(final Collection list1, final Collection list2) { 170 if (list1 == list2) { 171 return true; 172 } 173 if (list1 == null || list2 == null || list1.size() != list2.size()) { 174 return false; 175 } 176 177 Iterator it1 = list1.iterator(); 178 Iterator it2 = list2.iterator(); 179 Object obj1 = null; 180 Object obj2 = null; 181 182 while (it1.hasNext() && it2.hasNext()) { 183 obj1 = it1.next(); 184 obj2 = it2.next(); 185 186 if (!(obj1 == null ? obj2 == null : obj1.equals(obj2))) { 187 return false; 188 } 189 } 190 191 return !(it1.hasNext() || it2.hasNext()); 192 } 193 194 206 public static int hashCodeForList(final Collection list) { 207 if (list == null) { 208 return 0; 209 } 210 int hashCode = 1; 211 Iterator it = list.iterator(); 212 Object obj = null; 213 214 while (it.hasNext()) { 215 obj = it.next(); 216 hashCode = 31 * hashCode + (obj == null ? 0 : obj.hashCode()); 217 } 218 return hashCode; 219 } 220 221 244 public static List synchronizedList(List list) { 245 return SynchronizedList.decorate(list); 246 } 247 248 257 public static List unmodifiableList(List list) { 258 return UnmodifiableList.decorate(list); 259 } 260 261 274 public static List predicatedList(List list, Predicate predicate) { 275 return PredicatedList.decorate(list, predicate); 276 } 277 278 287 public static List typedList(List list, Class type) { 288 return TypedList.decorate(list, type); 289 } 290 291 303 public static List transformedList(List list, Transformer transformer) { 304 return TransformedList.decorate(list, transformer); 305 } 306 307 336 public static List lazyList(List list, Factory factory) { 337 return LazyList.decorate(list, factory); 338 } 339 340 350 public static List fixedSizeList(List list) { 351 return FixedSizeList.decorate(list); 352 } 353 354 } 355 | Popular Tags |