From: Juergen H. <jho...@us...> - 2006-04-20 19:01:12
|
Update of /cvsroot/springframework/spring/src/org/springframework/core In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv4348/src/org/springframework/core Modified Files: CollectionFactory.java Log Message: fall back to approximate collection/map type if we cannot reinstantiate given collection/map Index: CollectionFactory.java =================================================================== RCS file: /cvsroot/springframework/spring/src/org/springframework/core/CollectionFactory.java,v retrieving revision 1.8 retrieving revision 1.9 diff -C2 -d -r1.8 -r1.9 *** CollectionFactory.java 19 Oct 2005 19:19:02 -0000 1.8 --- CollectionFactory.java 20 Apr 2006 19:00:59 -0000 1.9 *************** *** 1,4 **** /* ! * Copyright 2002-2005 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); --- 1,4 ---- /* ! * Copyright 2002-2006 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); *************** *** 17,20 **** --- 17,22 ---- package org.springframework.core; + import java.util.ArrayList; + import java.util.Collection; import java.util.HashMap; import java.util.HashSet; *************** *** 22,27 **** --- 24,34 ---- import java.util.LinkedHashMap; import java.util.LinkedHashSet; + import java.util.List; import java.util.Map; import java.util.Set; + import java.util.SortedMap; + import java.util.SortedSet; + import java.util.TreeMap; + import java.util.TreeSet; import org.apache.commons.collections.map.CaseInsensitiveMap; *************** *** 33,36 **** --- 40,45 ---- import org.apache.commons.logging.LogFactory; + import org.springframework.util.Assert; + /** * Factory for collections, being aware of JDK 1.4+ extended collections *************** *** 175,178 **** --- 184,256 ---- /** + * Create the most approximate collection for the given collection class. + * <p>Tries to create the given collection class. If that fails, + * an ArrayList, TreeSet or linked Set will be used as fallback for + * a List, SortedSet or Set, respectively. + * @param collectionClass the original collection class + * @param initialCapacity the initial capacity + * @return the new collection instance + * @see java.util.ArrayList + * @see java.util.TreeSet + * @see #createLinkedSetIfPossible + */ + public static Collection createApproximateCollection(Class collectionClass, int initialCapacity) { + Assert.notNull(collectionClass, "Collection class must not be null"); + if (!collectionClass.isInterface()) { + try { + return (Collection) collectionClass.newInstance(); + } + catch (Exception ex) { + if (logger.isDebugEnabled()) { + logger.debug( + "Could not instantiate collection type [" + collectionClass.getName() + "]: " + ex.getMessage()); + } + } + } + if (List.class.isAssignableFrom(collectionClass)) { + return new ArrayList(initialCapacity); + } + else if (SortedSet.class.isAssignableFrom(collectionClass)) { + return new TreeSet(); + } + else { + return createLinkedSetIfPossible(initialCapacity); + } + } + + /** + * Create the most approximate map for the given map class. + * <p>Tries to create the given map class. If that fails, a TreeMap or + * linked Map will be used as fallback for a SortedMap or Map, respectively. + * @param mapClass the original map class + * @param initialCapacity the initial capacity + * @return the new collection instance + * @see java.util.ArrayList + * @see java.util.TreeSet + * @see #createLinkedSetIfPossible + */ + public static Map createApproximateMap(Class mapClass, int initialCapacity) { + Assert.notNull(mapClass, "Map class must not be null"); + if (!mapClass.isInterface()) { + try { + return (Map) mapClass.newInstance(); + } + catch (Exception ex) { + if (logger.isDebugEnabled()) { + logger.debug( + "Could not instantiate map type [" + mapClass.getName() + "]: " + ex.getMessage()); + } + } + } + if (SortedMap.class.isAssignableFrom(mapClass)) { + return new TreeMap(); + } + else { + return createLinkedMapIfPossible(initialCapacity); + } + } + + + /** * Actual creation of JDK 1.4+ Collections. * In separate inner class to avoid runtime dependency on JDK 1.4+. |