Felipe Leme | 979013d | 2017-06-22 10:59:23 -0700 | [diff] [blame] | 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 | |
| 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 | * @throws IllegalArgumentException if any element of {@code validators} is an instance of a |
| 37 | * class that is not provided by the Android System. |
| 38 | */ |
| 39 | @NonNull |
| 40 | public static Validator and(@NonNull Validator...validators) { |
| 41 | return new RequiredValidators(getInternalValidators(validators)); |
| 42 | } |
| 43 | |
| 44 | /** |
| 45 | * Creates a validator that is valid if any of the {@code validators} is valid. |
| 46 | * |
| 47 | * @throws IllegalArgumentException if any element of {@code validators} is an instance of a |
| 48 | * class that is not provided by the Android System. |
| 49 | */ |
| 50 | @NonNull |
| 51 | public static Validator or(@NonNull Validator...validators) { |
| 52 | return new OptionalValidators(getInternalValidators(validators)); |
| 53 | } |
| 54 | |
| 55 | private static InternalValidator[] getInternalValidators(Validator[] validators) { |
| 56 | Preconditions.checkArrayElementsNotNull(validators, "validators"); |
| 57 | |
| 58 | final InternalValidator[] internals = new InternalValidator[validators.length]; |
| 59 | |
| 60 | for (int i = 0; i < validators.length; i++) { |
| 61 | Preconditions.checkArgument((validators[i] instanceof InternalValidator), |
| 62 | "element " + i + " not provided by Android System: " + validators[i]); |
| 63 | internals[i] = (InternalValidator) validators[i]; |
| 64 | } |
| 65 | return internals; |
| 66 | } |
| 67 | } |