001package org.kuali.common.util.xml.jaxb.adapter; 002 003import java.util.Collection; 004import java.util.Collections; 005 006import javax.xml.bind.annotation.adapters.XmlAdapter; 007 008import org.kuali.common.util.xml.jaxb.wrapper.CollectionWrapper; 009 010public class ImmutableCollectionAdapter<T> extends XmlAdapter<CollectionWrapper<T>, Collection<T>> { 011 012 private final Collection<T> EMPTY_COLLECTION = Collections.emptyList(); 013 private final CollectionWrapper<T> EMPTY_WRAPPER = new CollectionWrapper<T>(EMPTY_COLLECTION); 014 015 @Override 016 public CollectionWrapper<T> marshal(Collection<T> c) { 017 if (isEmpty(c)) { 018 return EMPTY_WRAPPER; 019 } else { 020 return new CollectionWrapper<T>(c); 021 } 022 } 023 024 @Override 025 public Collection<T> unmarshal(CollectionWrapper<T> wrapper) { 026 if (isEmpty(wrapper.getCollection())) { 027 return EMPTY_COLLECTION; 028 } else { 029 return Collections.unmodifiableCollection(wrapper.getCollection()); 030 } 031 } 032 033 protected static boolean isEmpty(Collection<?> c) { 034 return c == null || c.size() == 0; 035 } 036 037}