1 /*
2  * Copyright (C) 2021 The Android Open Source Project
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 
17 package com.android.queryable;
18 
19 import java.util.Arrays;
20 import java.util.Collection;
21 import java.util.stream.Collectors;
22 
23 public interface Queryable {
24     /**
25      * Generate a string representation of the query.
26      *
27      * <p>For example, if {@code fieldName} was age, we might generate "age > 5, age < 10"
28      */
describeQuery(String fieldName)29     String describeQuery(String fieldName);
30 
31     /** True if this query has not had any filters applied. */
isEmptyQuery()32     boolean isEmptyQuery();
33 
34     /**
35      * Join sub-parts of a query for use in {@link #describeQuery(String)}.
36      *
37      * <p>Queries which are not set should be null.
38      */
joinQueryStrings(String... queryStrings)39     static String joinQueryStrings(String... queryStrings) {
40         return Arrays.stream(queryStrings).filter(i -> i != null && !i.isEmpty())
41                 .collect(Collectors.joining(", "));
42     }
43 
44     /**
45      * Join sub-parts of a query for use in {@link #describeQuery(String)}.
46      *
47      * <p>Queries which are not set should be null.
48      */
joinQueryStrings(Collection<String> queryStrings)49     static String joinQueryStrings(Collection<String> queryStrings) {
50         return queryStrings.stream().filter(i -> i != null && !i.isEmpty())
51                 .collect(Collectors.joining(", "));
52     }
53 
isEmptyQuery(Queryable queryable)54     static boolean isEmptyQuery(Queryable queryable) {
55         return queryable == null || queryable.isEmptyQuery();
56     }
57 }
58