1 /*
2  * Copyright (C) 2017 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 package android.service.autofill;
17 
18 import android.annotation.NonNull;
19 
20 import com.android.internal.util.Preconditions;
21 
22 /**
23  * Factory for {@link Validator} operations.
24  *
25  * <p>See {@link SaveInfo.Builder#setValidator(Validator)} for examples.
26  */
27 public final class Validators {
28 
Validators()29     private Validators() {
30         throw new UnsupportedOperationException("contains static methods only");
31     }
32 
33     /**
34      * Creates a validator that is only valid if all {@code validators} are valid.
35      *
36      * <p>Used to represent an {@code AND} boolean operation in a chain of validators.
37      *
38      * @throws IllegalArgumentException if any element of {@code validators} is an instance of a
39      * class that is not provided by the Android System.
40      */
41     @NonNull
and(@onNull Validator...validators)42     public static Validator and(@NonNull Validator...validators) {
43         return new RequiredValidators(getInternalValidators(validators));
44     }
45 
46     /**
47      * Creates a validator that is valid if any of the {@code validators} is valid.
48      *
49      * <p>Used to represent an {@code OR} boolean operation in a chain of validators.
50      *
51      * @throws IllegalArgumentException if any element of {@code validators} is an instance of a
52      * class that is not provided by the Android System.
53      */
54     @NonNull
or(@onNull Validator...validators)55     public static Validator or(@NonNull Validator...validators) {
56         return new OptionalValidators(getInternalValidators(validators));
57     }
58 
59     /**
60      * Creates a validator that is valid when {@code validator} is not, and vice versa.
61      *
62      * <p>Used to represent a {@code NOT} boolean operation in a chain of validators.
63      *
64      * @throws IllegalArgumentException if {@code validator} is an instance of a class that is not
65      * provided by the Android System.
66      */
67     @NonNull
not(@onNull Validator validator)68     public static Validator not(@NonNull Validator validator) {
69         Preconditions.checkArgument(validator instanceof InternalValidator,
70                 "validator not provided by Android System: " + validator);
71         return new NegationValidator((InternalValidator) validator);
72     }
73 
getInternalValidators(Validator[] validators)74     private static InternalValidator[] getInternalValidators(Validator[] validators) {
75         Preconditions.checkArrayElementsNotNull(validators, "validators");
76 
77         final InternalValidator[] internals = new InternalValidator[validators.length];
78 
79         for (int i = 0; i < validators.length; i++) {
80             Preconditions.checkArgument((validators[i] instanceof InternalValidator),
81                     "element " + i + " not provided by Android System: " + validators[i]);
82             internals[i] = (InternalValidator) validators[i];
83         }
84         return internals;
85     }
86 }
87