Coverage Report - org.kuali.rice.core.api.util.jaxb.MapStringStringAdapter
 
Classes in this File Line Coverage Branch Coverage Complexity
MapStringStringAdapter
85%
12/14
70%
7/10
4.5
 
 1  
 /*
 2  
  * Copyright 2007-2010 The Kuali Foundation
 3  
  *
 4  
  * Licensed under the Educational Community License, Version 2.0 (the "License");
 5  
  * you may not use this file except in compliance with the License.
 6  
  * You may obtain a copy of the License at
 7  
  *
 8  
  * http://www.opensource.org/licenses/ecl2.php
 9  
  *
 10  
  * Unless required by applicable law or agreed to in writing, software
 11  
  * distributed under the License is distributed on an "AS IS" BASIS,
 12  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 13  
  * See the License for the specific language governing permissions and
 14  
  * limitations under the License.
 15  
  */
 16  
 package org.kuali.rice.core.api.util.jaxb;
 17  
 
 18  
 import javax.xml.bind.annotation.adapters.XmlAdapter;
 19  
 import java.util.ArrayList;
 20  
 import java.util.Collections;
 21  
 import java.util.HashMap;
 22  
 import java.util.List;
 23  
 import java.util.Map;
 24  
 
 25  
 /**
 26  
  * Do JAXB mapping of Map<String, String> to a format like the following for a
 27  
  * map containing { key1:value1, key2:value2 }:
 28  
  * 
 29  
  * <pre>
 30  
  * {@code
 31  
  * <...>
 32  
  *   <entry key="key1">value1</entry>
 33  
  *   <entry key="key2">value2</entry>
 34  
  * </...>
 35  
  * }
 36  
  * 
 37  
  * @author Kuali Rice Team (rice.collab@kuali.org)
 38  
  *
 39  
  */
 40  55
 public class MapStringStringAdapter extends XmlAdapter<StringMapEntryList, Map<String, String>> {
 41  
 
 42  
         @Override
 43  
         public StringMapEntryList marshal(Map<String, String> map) throws Exception {
 44  14
                 if (map == null) {
 45  0
                         return null;
 46  
                 }
 47  14
                 List<StringMapEntry> entries = new ArrayList<StringMapEntry>();
 48  14
                 for (Map.Entry<String, String> entry : map.entrySet()) {
 49  10
                         entries.add(new StringMapEntry(entry));
 50  
                 }
 51  14
                 return new StringMapEntryList(entries);
 52  
         }
 53  
 
 54  
         @Override
 55  
         public Map<String, String> unmarshal(StringMapEntryList entryList) throws Exception {
 56  25
                 if (entryList == null || entryList.getEntries() == null) {
 57  0
                         return null;
 58  
                 }
 59  25
                 List<StringMapEntry> entries = entryList.getEntries();
 60  25
                 Map<String, String> resultMap = new HashMap<String, String>(entries.size());
 61  25
                 for (StringMapEntry entry : entries) {
 62  20
                         resultMap.put(entry.getKey(), entry.getValue());
 63  
                 }
 64  25
                 return Collections.unmodifiableMap(resultMap);
 65  
         }
 66  
 }