RequiredValidators.java revision 0e3e6f8274b9d4c1c9646a1d39aed6a9f8a6a28d
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;
24
25import com.android.internal.util.Preconditions;
26
27/**
28 * Compound validator that only returns {@code true} on {@link #isValid(ValueFinder)} if all
29 * of its subvalidators return {@code true} as well.
30 *
31 * <p>Used to implement an {@code AND} logical operation.
32 *
33 * @hide
34 */
35final class RequiredValidators extends InternalValidator {
36
37    @NonNull private final InternalValidator[] mValidators;
38
39    RequiredValidators(@NonNull InternalValidator[] validators) {
40        mValidators = Preconditions.checkArrayElementsNotNull(validators, "validators");
41    }
42
43    @Override
44    public boolean isValid(@NonNull ValueFinder finder) {
45        for (InternalValidator validator : mValidators) {
46            final boolean valid = validator.isValid(finder);
47            if (!valid) return false;
48        }
49        return true;
50    }
51
52    /////////////////////////////////////
53    // Object "contract" methods. //
54    /////////////////////////////////////
55    @Override
56    public String toString() {
57        if (!sDebug) return super.toString();
58
59        return new StringBuilder("RequiredValidators: [validators=").append(mValidators)
60                .append("]")
61                .toString();
62    }
63
64    /////////////////////////////////////
65    // Parcelable "contract" methods. //
66    /////////////////////////////////////
67    @Override
68    public int describeContents() {
69        return 0;
70    }
71
72    @Override
73    public void writeToParcel(Parcel dest, int flags) {
74        dest.writeParcelableArray(mValidators, flags);
75    }
76
77    public static final Parcelable.Creator<RequiredValidators> CREATOR =
78            new Parcelable.Creator<RequiredValidators>() {
79        @Override
80        public RequiredValidators createFromParcel(Parcel parcel) {
81            return new RequiredValidators(parcel
82                .readParcelableArray(null, InternalValidator.class));
83        }
84
85        @Override
86        public RequiredValidators[] newArray(int size) {
87            return new RequiredValidators[size];
88        }
89    };
90}
91