blob: 7189c886aa069b590942b429a81b39180a2766af [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 returns {@code true} on {@link #isValid(ValueFinder)} if any
30 * of its subvalidators returns {@code true} as well.
31 *
32 * <p>Used to implement an {@code OR} logical operation.
33 *
34 * @hide
35 */
36final class OptionalValidators extends InternalValidator {
37
Felipe Leme3a585a82017-10-18 15:34:26 -070038 private static final String TAG = "OptionalValidators";
39
Philip P. Moltmann0e3e6f82017-07-11 17:03:49 -070040 @NonNull private final InternalValidator[] mValidators;
Felipe Leme979013d2017-06-22 10:59:23 -070041
42 OptionalValidators(@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 true;
52 }
53
54 return false;
55 }
56
57 /////////////////////////////////////
58 // Object "contract" methods. //
59 /////////////////////////////////////
60 @Override
61 public String toString() {
62 if (!sDebug) return super.toString();
63
64 return new StringBuilder("OptionalValidators: [validators=").append(mValidators)
65 .append("]")
66 .toString();
67 }
68
69 /////////////////////////////////////
70 // Parcelable "contract" methods. //
71 /////////////////////////////////////
72 @Override
73 public int describeContents() {
74 return 0;
75 }
76
77 @Override
78 public void writeToParcel(Parcel dest, int flags) {
79 dest.writeParcelableArray(mValidators, flags);
80 }
81
Jeff Sharkey9e8f83d2019-02-28 12:06:45 -070082 public static final @android.annotation.NonNull Parcelable.Creator<OptionalValidators> CREATOR =
Felipe Leme979013d2017-06-22 10:59:23 -070083 new Parcelable.Creator<OptionalValidators>() {
84 @Override
85 public OptionalValidators createFromParcel(Parcel parcel) {
86 return new OptionalValidators(parcel
87 .readParcelableArray(null, InternalValidator.class));
88 }
89
90 @Override
91 public OptionalValidators[] newArray(int size) {
92 return new OptionalValidators[size];
93 }
94 };
95}