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