RequiredValidators.java revision 3a585a8ef14ffe57fe8addc690d06614f10e2033
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
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;
24import android.util.Log;
25
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
38    private static final String TAG = "RequiredValidators";
39
40    @NonNull private final InternalValidator[] mValidators;
41
42    RequiredValidators(@NonNull InternalValidator[] validators) {
43        mValidators = Preconditions.checkArrayElementsNotNull(validators, "validators");
44    }
45
46    @Override
47    public boolean isValid(@NonNull ValueFinder finder) {
48        for (InternalValidator validator : mValidators) {
49            final boolean valid = validator.isValid(finder);
50            if (sDebug) Log.d(TAG, "isValid(" + validator + "): " + valid);
51            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}
95