001/**
002 * Copyright 2010-2014 The Kuali Foundation
003 *
004 * Licensed under the Educational Community License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.opensource.org/licenses/ecl2.php
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package org.kuali.common.util.xml.jaxb.adapter;
017
018import java.util.Collections;
019import java.util.List;
020
021import javax.xml.bind.annotation.adapters.XmlAdapter;
022
023import org.kuali.common.util.xml.jaxb.wrapper.ListWrapper;
024
025import com.google.common.collect.ImmutableList;
026
027public class ImmutableListAdapter<T> extends XmlAdapter<ListWrapper<T>, List<T>> {
028
029        private final List<T> EMPTY_LIST = Collections.emptyList();
030        private final ListWrapper<T> EMPTY_WRAPPER = new ListWrapper<T>(EMPTY_LIST);
031
032        @Override
033        public ListWrapper<T> marshal(List<T> list) {
034                if (isEmpty(list)) {
035                        return EMPTY_WRAPPER;
036                } else {
037                        return new ListWrapper<T>(list);
038                }
039        }
040
041        @Override
042        public List<T> unmarshal(ListWrapper<T> wrapper) {
043                if (isEmpty(wrapper.getList())) {
044                        return EMPTY_LIST;
045                } else {
046                        return ImmutableList.copyOf(wrapper.getList());
047                }
048        }
049
050        protected static boolean isEmpty(List<?> list) {
051                return list == null || list.size() == 0;
052        }
053
054}