View Javadoc
1   package org.kuali.common.util.xml.jaxb.adapter;
2   
3   import java.util.Collection;
4   import java.util.Collections;
5   
6   import javax.xml.bind.annotation.adapters.XmlAdapter;
7   
8   import org.kuali.common.util.xml.jaxb.wrapper.CollectionWrapper;
9   
10  public class ImmutableCollectionAdapter<T> extends XmlAdapter<CollectionWrapper<T>, Collection<T>> {
11  
12  	private final Collection<T> EMPTY_COLLECTION = Collections.emptyList();
13  	private final CollectionWrapper<T> EMPTY_WRAPPER = new CollectionWrapper<T>(EMPTY_COLLECTION);
14  
15  	@Override
16  	public CollectionWrapper<T> marshal(Collection<T> c) {
17  		if (isEmpty(c)) {
18  			return EMPTY_WRAPPER;
19  		} else {
20  			return new CollectionWrapper<T>(c);
21  		}
22  	}
23  
24  	@Override
25  	public Collection<T> unmarshal(CollectionWrapper<T> wrapper) {
26  		if (isEmpty(wrapper.getCollection())) {
27  			return EMPTY_COLLECTION;
28  		} else {
29  			return Collections.unmodifiableCollection(wrapper.getCollection());
30  		}
31  	}
32  
33  	protected static boolean isEmpty(Collection<?> c) {
34  		return c == null || c.size() == 0;
35  	}
36  
37  }