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 androidx.annotation.Nullable;
20 
21 import java.util.Arrays;
22 import java.util.Collection;
23 import java.util.stream.Collectors;
24 
25 public interface Queryable {
26     /**
27      * Generate a string representation of the query.
28      *
29      * <p>For example, if {@code fieldName} was age, we might generate "age > 5, age < 10"
30      */
31     @Nullable
describeQuery(String fieldName)32     String describeQuery(String fieldName);
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 }
54