View Javadoc
1   /*
2    * Copyright 2014 the original author or authors.
3    *
4    * Licensed under the Apache 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.apache.org/licenses/LICENSE-2.0
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.openehealth.ipf.commons.ihe.xds.core.metadata.jaxbadapters;
17  
18  import javax.xml.bind.annotation.adapters.XmlAdapter;
19  import java.util.ArrayList;
20  import java.util.HashMap;
21  import java.util.List;
22  import java.util.Map;
23  
24  /**
25   * @author Dmytro Rud
26   */
27  public class StringMapAdapter extends XmlAdapter<StringMap, Map<String, List<String>>> {
28  
29      @Override
30      public StringMap marshal(Map<String, List<String>> v) throws Exception {
31          if (v == null) {
32              return null;
33          }
34  
35          StringMap result = new StringMap();
36          result.entries = new ArrayList<>();
37          for (Map.Entry<String, List<String>> entry : v.entrySet()) {
38              StringMapEntry extra = new StringMapEntry();
39              extra.setKey(entry.getKey());
40              extra.setValues(new ArrayList<>());
41              extra.getValues().addAll(entry.getValue());
42              result.entries.add(extra);
43          }
44  
45          return result;
46      }
47  
48  
49      @Override
50      public Map<String, List<String>> unmarshal(StringMap v) throws Exception {
51          if ((v == null) || (v.entries == null)) {
52              return null;
53          }
54  
55          HashMap<String, List<String>> result = new HashMap<>();
56          for (StringMapEntry extra : v.entries) {
57              result.put(extra.getKey(), extra.getValues());
58          }
59          return result;
60      }
61  
62  }