blob: 9e1db2bca5dffce55c8f5b5b13064e1a02438389 [file] [log] [blame]
Felipe Leme979013d2017-06-22 10:59:23 -07001/*
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
17package android.service.autofill;
18
19import static android.view.autofill.Helper.sDebug;
20
21import android.annotation.NonNull;
22import android.os.Parcel;
23import android.os.Parcelable;
Felipe Leme3a585a82017-10-18 15:34:26 -070024import android.util.Log;
Felipe Leme979013d2017-06-22 10:59:23 -070025
26import com.android.internal.util.Preconditions;
27
28/**
29 * Compound validator that only returns {@code true} on {@link #isValid(ValueFinder)} if all
30 * of its subvalidators return {@code true} as well.
31 *
32 * <p>Used to implement an {@code AND} logical operation.
33 *
34 * @hide
35 */
36final class RequiredValidators extends InternalValidator {
37
Felipe Leme3a585a82017-10-18 15:34:26 -070038 private static final String TAG = "RequiredValidators";
39
Philip P. Moltmann0e3e6f82017-07-11 17:03:49 -070040 @NonNull private final InternalValidator[] mValidators;
Felipe Leme979013d2017-06-22 10:59:23 -070041
42 RequiredValidators(@NonNull InternalValidator[] validators) {
43 mValidators = Preconditions.checkArrayElementsNotNull(validators, "validators");
44 }
45
46 @Override
47 public boolean isValid(@NonNull ValueFinder finder) {
Felipe Leme979013d2017-06-22 10:59:23 -070048 for (InternalValidator validator : mValidators) {
49 final boolean valid = validator.isValid(finder);
Felipe Leme3a585a82017-10-18 15:34:26 -070050 if (sDebug) Log.d(TAG, "isValid(" + validator + "): " + valid);
Felipe Leme979013d2017-06-22 10:59:23 -070051 if (!valid) return false;
52 }
53 return true;
54 }
55
56 /////////////////////////////////////
57 // Object "contract" methods. //
58 /////////////////////////////////////
59 @Override
60 public String toString() {
61 if (!sDebug) return super.toString();
62
63 return new StringBuilder("RequiredValidators: [validators=").append(mValidators)
64 .append("]")
65 .toString();
66 }
67
68 /////////////////////////////////////
69 // Parcelable "contract" methods. //
70 /////////////////////////////////////
71 @Override
72 public int describeContents() {
73 return 0;
74 }
75
76 @Override
77 public void writeToParcel(Parcel dest, int flags) {
78 dest.writeParcelableArray(mValidators, flags);
79 }
80
81 public static final Parcelable.Creator<RequiredValidators> CREATOR =
82 new Parcelable.Creator<RequiredValidators>() {
83 @Override
84 public RequiredValidators createFromParcel(Parcel parcel) {
85 return new RequiredValidators(parcel
86 .readParcelableArray(null, InternalValidator.class));
87 }
88
89 @Override
90 public RequiredValidators[] newArray(int size) {
91 return new RequiredValidators[size];
92 }
93 };
94}