View Javadoc
1   /*
2    * Copyright 2011 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.requests.query;
17  
18  import javax.xml.bind.annotation.XmlAccessType;
19  import javax.xml.bind.annotation.XmlAccessorType;
20  import javax.xml.bind.annotation.XmlTransient;
21  import javax.xml.bind.annotation.XmlType;
22  import javax.xml.bind.annotation.adapters.XmlAdapter;
23  import java.util.List;
24  import java.util.stream.Collectors;
25  
26  /**
27   * A JAXB {@link XmlAdapter} that helps serialize generic lists of lists. This sort of thing is used by the
28   * IPF {@link QueryList} class but is not handled naturally by JAXB. It takes a little effort here to get a
29   * reasonable serialization.
30   *
31   * @param <T> The type of object contained in the inner list
32   */
33  @XmlTransient
34  public class ListOfListAdapter<T> extends XmlAdapter<ListOfListAdapter.ListOfListWrapper<T>, List<List<T>>> {
35      @Override
36      public List<List<T>> unmarshal(ListOfListWrapper<T> v) throws Exception {
37          return v.getInnerList().stream()
38                  .map(ListWrapper::getValue)
39                  .collect(Collectors.toList());
40      }
41  
42      @Override
43      public ListOfListWrapper<T> marshal(List<List<T>> v) throws Exception {
44          List<ListWrapper<T>> outerList = v.stream()
45                  .map(ListWrapper::new)
46                  .collect(Collectors.toList());
47          return new ListOfListWrapper<>(outerList);
48      }
49  
50      @XmlType(name = "ListOfListWrapper", namespace = "http://www.openehealth.org/ipf/xds")
51      @XmlAccessorType(XmlAccessType.FIELD)
52      public static class ListOfListWrapper<T> {
53          private List<ListWrapper<T>> innerList;
54  
55          // Required for JAXB
56          @SuppressWarnings({"UnusedDeclaration"})
57          protected ListOfListWrapper() {
58          }
59  
60          public ListOfListWrapper(List<ListWrapper<T>> list) {
61              this.innerList = list;
62          }
63  
64          public List<ListWrapper<T>> getInnerList() {
65              return innerList;
66          }
67      }
68  
69      @XmlType(name = "ListWrapper", namespace = "http://www.openehealth.org/ipf/xds")
70      @XmlAccessorType(XmlAccessType.FIELD)
71      public static class ListWrapper<T> {
72          private List<T> value;
73  
74          // Required for JAXB
75          @SuppressWarnings({"UnusedDeclaration"})
76          protected ListWrapper() {
77          }
78  
79          public ListWrapper(List<T> list) {
80              this.value = list;
81          }
82  
83          public List<T> getValue() {
84              return value;
85          }
86      }
87  }
88