1 16 17 package org.springframework.beans.propertyeditors; 18 19 import java.beans.PropertyEditorSupport ; 20 import java.lang.reflect.Array ; 21 import java.util.ArrayList ; 22 import java.util.Collection ; 23 import java.util.Iterator ; 24 import java.util.List ; 25 import java.util.SortedSet ; 26 import java.util.TreeSet ; 27 28 import org.springframework.core.CollectionFactory; 29 30 45 public class CustomCollectionEditor extends PropertyEditorSupport { 46 47 private final Class collectionType; 48 49 private final boolean nullAsEmptyCollection; 50 51 52 62 public CustomCollectionEditor(Class collectionType) { 63 this(collectionType, false); 64 } 65 66 84 public CustomCollectionEditor(Class collectionType, boolean nullAsEmptyCollection) { 85 if (collectionType == null) { 86 throw new IllegalArgumentException ("Collection type is required"); 87 } 88 if (!Collection .class.isAssignableFrom(collectionType)) { 89 throw new IllegalArgumentException ( 90 "Collection type [" + collectionType.getName() + "] does not implement [java.util.Collection]"); 91 } 92 this.collectionType = collectionType; 93 this.nullAsEmptyCollection = nullAsEmptyCollection; 94 } 95 96 97 100 public void setAsText(String text) throws IllegalArgumentException { 101 setValue(text); 102 } 103 104 107 public void setValue(Object value) { 108 if (value == null && this.nullAsEmptyCollection) { 109 super.setValue(createCollection(this.collectionType, 0)); 110 } 111 else if (value == null || (this.collectionType.isInstance(value) && !alwaysCreateNewCollection())) { 112 super.setValue(value); 114 } 115 else if (value instanceof Collection ) { 116 Collection source = (Collection ) value; 118 Collection target = createCollection(this.collectionType, source.size()); 119 for (Iterator it = source.iterator(); it.hasNext();) { 120 target.add(convertElement(it.next())); 121 } 122 super.setValue(target); 123 } 124 else if (value.getClass().isArray()) { 125 int length = Array.getLength(value); 127 Collection target = createCollection(this.collectionType, length); 128 for (int i = 0; i < length; i++) { 129 target.add(convertElement(Array.get(value, i))); 130 } 131 super.setValue(target); 132 } 133 else { 134 Collection target = createCollection(this.collectionType, 1); 136 target.add(convertElement(value)); 137 super.setValue(target); 138 } 139 } 140 141 148 protected Collection createCollection(Class collectionType, int initialCapacity) { 149 if (!collectionType.isInterface()) { 150 try { 151 return (Collection ) collectionType.newInstance(); 152 } 153 catch (Exception ex) { 154 throw new IllegalArgumentException ( 155 "Could not instantiate collection class [" + collectionType.getName() + "]: " + ex.getMessage()); 156 } 157 } 158 else if (List .class.equals(collectionType)) { 159 return new ArrayList (initialCapacity); 160 } 161 else if (SortedSet .class.equals(collectionType)) { 162 return new TreeSet (); 163 } 164 else { 165 return CollectionFactory.createLinkedSetIfPossible(initialCapacity); 166 } 167 } 168 169 176 protected boolean alwaysCreateNewCollection() { 177 return false; 178 } 179 180 194 protected Object convertElement(Object element) { 195 return element; 196 } 197 198 199 203 public String getAsText() { 204 return null; 205 } 206 207 } 208 | Popular Tags |