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.Map;
020
021import javax.xml.bind.annotation.adapters.XmlAdapter;
022
023import org.kuali.common.util.xml.jaxb.wrapper.MapWrapper;
024
025public class ImmutableMapAdapter<K, V> extends XmlAdapter<MapWrapper<K, V>, Map<K, V>> {
026
027        private final Map<K, V> EMPTY_MAP = Collections.emptyMap();
028        private final MapWrapper<K, V> EMPTY_WRAPPER = new MapWrapper<K, V>(EMPTY_MAP);
029
030        @Override
031        public MapWrapper<K, V> marshal(Map<K, V> map) {
032                if (isEmpty(map)) {
033                        return EMPTY_WRAPPER;
034                } else {
035                        return new MapWrapper<K, V>(map);
036                }
037        }
038
039        @Override
040        public Map<K, V> unmarshal(MapWrapper<K, V> wrapper) {
041                if (isEmpty(wrapper.getMap())) {
042                        return EMPTY_MAP;
043                } else {
044                        return Collections.unmodifiableMap(wrapper.getMap());
045                }
046        }
047
048        protected static boolean isEmpty(Map<?, ?> map) {
049                return map == null || map.size() == 0;
050        }
051
052}