View Javadoc
1   /*
2    * Copyright 2009 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 lombok.EqualsAndHashCode;
19  import lombok.Getter;
20  import lombok.ToString;
21  
22  import javax.xml.bind.annotation.XmlAccessType;
23  import javax.xml.bind.annotation.XmlAccessorType;
24  import javax.xml.bind.annotation.XmlType;
25  import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
26  import java.io.Serializable;
27  import java.util.ArrayList;
28  import java.util.Collections;
29  import java.util.List;
30  
31  import static org.apache.commons.lang3.Validate.noNullElements;
32  import static org.apache.commons.lang3.Validate.notNull;
33  
34  /**
35   * Represents a list of query parameters.
36   * <p>
37   * The list allows AND and OR semantics via two levels of lists.
38   * The inner lists of parameters have OR semantics. The outer list
39   * contains the inner lists and uses AND semantics. E.g. the query
40   * list <code>(a, b), (c, d)</code> contains two inner lists and the
41   * parameters are evaluated (a OR b) AND (c OR d).
42   * @param <T>
43   *          The type contained in the list.
44   *
45   * @author Jens Riemschneider
46   */
47  @XmlAccessorType(XmlAccessType.FIELD)
48  @XmlType(name = "QueryList")
49  @EqualsAndHashCode(doNotUseGetters = true)
50  @ToString(doNotUseGetters = true)
51  public class QueryList<T> implements Serializable {
52      private static final long serialVersionUID = -2729640243221349924L;
53      
54      @XmlJavaTypeAdapter(ListOfListAdapter.class)
55      @Getter private List<List<T>> outerList = new ArrayList<>();
56  
57      /**
58       * Constructs a query list.
59       */
60      public QueryList() {}
61  
62      /**
63       * Constructs a query list using another list.
64       * <p>
65       * This constructor does not clone the objects in the list.
66       * @param other
67       *          the other list.
68       */
69      public QueryList(QueryList<T> other) {
70          notNull(other, "other cannot be null");
71          noNullElements(other.getOuterList(), "other.getOuterList() cannot contain null elements");
72          for (List<T> innerList : other.getOuterList()) {
73              noNullElements(innerList, "innerList cannot contain null elements");
74              outerList.add(new ArrayList<>(innerList));
75          }
76      }
77  
78      /**
79       * Constructs a query list.
80       * @param singleElement
81       *          the only initial element in the list.
82       */
83      public QueryList(T singleElement) {
84          notNull(singleElement, "singleElement cannot be null");
85          outerList.add(Collections.singletonList(singleElement));
86      }
87  
88  }