| 1 | /* |
| 2 | * Copyright 2018 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 | |
| 17 | package android.view.inspector; |
| 18 | |
| 19 | import android.annotation.NonNull; |
| 20 | |
| 21 | import java.util.ArrayList; |
| 22 | import java.util.Collections; |
| 23 | import java.util.HashSet; |
| 24 | import java.util.List; |
| 25 | import java.util.Objects; |
| 26 | import java.util.Set; |
| 27 | |
| 28 | /** |
| 29 | * Maps the values of an {@code int} property to sets of string for properties that encode flags. |
| 30 | * |
| 31 | * An {@link InspectionCompanion} may provide an instance of this class to a {@link PropertyMapper} |
| 32 | * for flag values packed into primitive {@code int} properties. |
| 33 | * |
| 34 | * Each flag has a mask and a target value, for non-exclusive flags, the target can also be used as |
| 35 | * the mask. A given integer value is compared against each flag to find what flags are active for |
| 36 | * it by bitwise anding it with the mask and comparing the result against the target, that is, |
| 37 | * {@code (value & mask) == target}. |
| 38 | * |
| 39 | * @see PropertyMapper#mapIntFlag(String, int, java.util.function.IntFunction) |
| 40 | */ |
| 41 | public final class IntFlagMapping { |
| 42 | private final List<Flag> mFlags = new ArrayList<>(); |
| 43 | |
| 44 | /** |
| 45 | * Get a set of the names of enabled flags for a given property value. |
| 46 | * |
| 47 | * @param value The value of the property |
| 48 | * @return The names of the enabled flags, empty if no flags enabled |
| 49 | */ |
| 50 | @NonNull |
| 51 | public Set<String> get(int value) { |
| 52 | final Set<String> enabledFlagNames = new HashSet<>(mFlags.size()); |
| 53 | |
| 54 | for (Flag flag : mFlags) { |
| 55 | if (flag.isEnabledFor(value)) { |
| 56 | enabledFlagNames.add(flag.mName); |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | return Collections.unmodifiableSet(enabledFlagNames); |
| 61 | } |
| 62 | |
| 63 | /** |
| 64 | * Add a mutually exclusive flag to the map. |
| 65 | * |
| 66 | * @param mask The bit mask to compare to and with a value |
| 67 | * @param target The target value to compare the masked value with |
| 68 | * @param name The name of the flag to include if enabled |
| 69 | */ |
| 70 | public void add(int mask, int target, @NonNull String name) { |
| 71 | mFlags.add(new Flag(mask, target, name)); |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * Inner class that holds the name, mask, and target value of a flag |
| 76 | */ |
| 77 | private static final class Flag { |
| 78 | @NonNull private final String mName; |
| 79 | private final int mTarget; |
| 80 | private final int mMask; |
| 81 | |
| 82 | private Flag(int mask, int target, @NonNull String name) { |
| 83 | mTarget = target; |
| 84 | mMask = mask; |
| 85 | mName = Objects.requireNonNull(name); |
| 86 | } |
| 87 | |
| 88 | /** |
| 89 | * Compare the supplied property value against the mask and target. |
| 90 | * |
| 91 | * @param value The value to check |
| 92 | * @return True if this flag is enabled |
| 93 | */ |
| 94 | private boolean isEnabledFor(int value) { |
| 95 | return (value & mMask) == mTarget; |
| 96 | } |
| 97 | } |
| 98 | } |