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