View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.search.api.request;
20  
21  import static java.util.Objects.requireNonNull;
22  
23  /**
24   * Boolean query.
25   */
26  public abstract class BooleanQuery extends Query {
27      private final Query left;
28  
29      private final Query right;
30  
31      protected BooleanQuery(Query left, String op, Query right) {
32          super(op);
33          this.left = requireNonNull(left);
34          this.right = requireNonNull(right);
35      }
36  
37      /**
38       * Returns left term of this boolean query, never {@code null}.
39       */
40      public Query getLeft() {
41          return left;
42      }
43  
44      /**
45       * Returns right term of this boolean query, never {@code null}.
46       */
47      public Query getRight() {
48          return right;
49      }
50  
51      @Override
52      public String toString() {
53          return getLeft() + " " + getValue() + " " + getRight();
54      }
55  
56      public static final class And extends BooleanQuery {
57          private And(Query left, Query right) {
58              super(left, "AND", right);
59          }
60      }
61  
62      /**
63       * Creates Logical AND query (requires presence of all queries) out of passed in queries (at least 2 or more
64       * should be given).
65       */
66      public static BooleanQuery and(Query left, Query... rights) {
67          if (rights.length == 0) {
68              throw new IllegalArgumentException("one or more on right needed");
69          }
70          BooleanQuery result = null;
71          for (Query right : rights) {
72              result = new And(result == null ? left : result, right);
73          }
74          return result;
75      }
76  }