Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 0fb1f1e0 authored by TreeHugger Robot's avatar TreeHugger Robot Committed by Android (Google) Code Review
Browse files

Merge "Use IntFunction for inspector flag and enum mapping"

parents 932d7b50 83bac9f3
Loading
Loading
Loading
Loading
+4 −19
Original line number Diff line number Diff line
@@ -53491,27 +53491,12 @@ package android.view.inspector {
    method @Nullable public <T> android.view.inspector.InspectionCompanion<T> provide(@NonNull Class<T>);
  }
  public final class IntEnumMapping {
    method @Nullable public String get(int);
  }
  public static final class IntEnumMapping.Builder {
    ctor public IntEnumMapping.Builder();
    method @NonNull public android.view.inspector.IntEnumMapping.Builder addValue(@NonNull String, int);
    method @NonNull public android.view.inspector.IntEnumMapping build();
  }
  public final class IntFlagMapping {
    ctor public IntFlagMapping();
    method public void add(int, int, @NonNull String);
    method @NonNull public java.util.Set<java.lang.String> get(int);
  }
  public static final class IntFlagMapping.Builder {
    ctor public IntFlagMapping.Builder();
    method @NonNull public android.view.inspector.IntFlagMapping.Builder addFlag(@NonNull String, int);
    method @NonNull public android.view.inspector.IntFlagMapping.Builder addFlag(@NonNull String, int, int);
    method @NonNull public android.view.inspector.IntFlagMapping build();
  }
  public interface PropertyMapper {
    method public int mapBoolean(@NonNull String, @AttrRes int);
    method public int mapByte(@NonNull String, @AttrRes int);
@@ -53521,8 +53506,8 @@ package android.view.inspector {
    method public int mapFloat(@NonNull String, @AttrRes int);
    method public int mapGravity(@NonNull String, @AttrRes int);
    method public int mapInt(@NonNull String, @AttrRes int);
    method public int mapIntEnum(@NonNull String, @AttrRes int, @NonNull android.view.inspector.IntEnumMapping);
    method public int mapIntFlag(@NonNull String, @AttrRes int, @NonNull android.view.inspector.IntFlagMapping);
    method public int mapIntEnum(@NonNull String, @AttrRes int, @NonNull java.util.function.IntFunction<java.lang.String>);
    method public int mapIntFlag(@NonNull String, @AttrRes int, @NonNull java.util.function.IntFunction<java.util.Set<java.lang.String>>);
    method public int mapLong(@NonNull String, @AttrRes int);
    method public int mapObject(@NonNull String, @AttrRes int);
    method public int mapResourceId(@NonNull String, @AttrRes int);
+0 −102
Original line number Diff line number Diff line
/*
 * Copyright 2019 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package android.view.inspector;

import android.annotation.NonNull;
import android.annotation.Nullable;
import android.util.SparseArray;

import java.util.Objects;

/**
 * Maps the values of an {@code int} property to strings for properties that encode an enumeration.
 *
 * An {@link InspectionCompanion} may provide an instance of this class to a {@link PropertyMapper}
 * for flag values packed into primitive {@code int} properties.
 *
 * This class is an immutable wrapper for {@link SparseArray}, and must be constructed by a
 * {@link Builder}.
 *
 * @see PropertyMapper#mapIntEnum(String, int, IntEnumMapping)
 */
public final class IntEnumMapping {
    private final SparseArray<String> mValues;

    /**
     * Get the name for the given property value
     *
     * @param value The value of the property
     * @return The name of the value in the enumeration, or null if no value is defined
     */
    @Nullable
    public String get(int value) {
        return mValues.get(value);
    }

    /**
     * Create a new instance from a builder.
     *
     * This constructor is private, use {@link Builder#build()} instead.
     *
     * @param builder A builder to create from
     */
    private IntEnumMapping(Builder builder) {
        mValues = builder.mValues.clone();
    }

    /**
     * A builder for {@link IntEnumMapping}.
     */
    public static final class Builder {
        @NonNull
        private SparseArray<String> mValues;
        private boolean mMustCloneValues = false;

        public Builder() {
            mValues = new SparseArray<>();
        }

        /**
         * Add a new enumerated value.
         *
         * @param name The string name of the enumeration value
         * @param value The {@code int} value of the enumeration value
         * @return This builder
         */
        @NonNull
        public Builder addValue(@NonNull String name, int value) {
            // Save an allocation, only re-clone if the builder is used again after building
            if (mMustCloneValues) {
                mValues = mValues.clone();
            }

            mValues.put(value, Objects.requireNonNull(name));
            return this;
        }

        /**
         * Build a new {@link IntEnumMapping} from this builder.
         *
         * @return A new mapping
         */
        @NonNull
        public IntEnumMapping build() {
            mMustCloneValues = true;
            return new IntEnumMapping(this);
        }
    }
}
+13 −70
Original line number Diff line number Diff line
@@ -21,6 +21,7 @@ import android.annotation.NonNull;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Objects;
import java.util.Set;

@@ -35,22 +36,20 @@ import java.util.Set;
 * it by bitwise anding it with the mask and comparing the result against the target, that is,
 * {@code (value & mask) == target}.
 *
 * This class is immutable, and must be constructed by a {@link Builder}.
 *
 * @see PropertyMapper#mapIntFlag(String, int, IntFlagMapping)
 * @see PropertyMapper#mapIntFlag(String, int, java.util.function.IntFunction)
 */
public final class IntFlagMapping {
    private final Flag[] mFlags;
    private final List<Flag> mFlags = new ArrayList<>();

    /**
     * Get an array of the names of enabled flags for a given property value.
     * Get a set of the names of enabled flags for a given property value.
     *
     * @param value The value of the property
     * @return The names of the enabled flags, empty if no flags enabled
     */
    @NonNull
    public Set<String> get(int value) {
        final Set<String> enabledFlagNames = new HashSet<>(mFlags.length);
        final Set<String> enabledFlagNames = new HashSet<>(mFlags.size());

        for (Flag flag : mFlags) {
            if (flag.isEnabledFor(value)) {
@@ -62,70 +61,14 @@ public final class IntFlagMapping {
    }

    /**
     * Create a new instance from a builder.
     *
     * This constructor is private, use {@link Builder#build()} instead.
     *
     * @param builder A builder to create from
     */
    private IntFlagMapping(Builder builder) {
        mFlags = builder.mFlags.toArray(new Flag[builder.mFlags.size()]);
    }

    /**
     * A builder for {@link IntFlagMapping}.
     */
    public static final class Builder {
        private ArrayList<Flag> mFlags;

        public Builder() {
            mFlags = new ArrayList<>();
        }

        /**
         * Add a new flag without a mask.
         *
         * The target value will be used as a mask, to handle the common case where flag values
         * are not mutually exclusive. The flag will be considered enabled for a property value if
         * the result of bitwise anding the target and the value equals the target, that is:
         * {@code (value & target) == target}.
         *
         * @param name The name of the flag
         * @param target The value to compare against
         * @return This builder
         */
        @NonNull
        public Builder addFlag(@NonNull String name, int target) {
            mFlags.add(new Flag(name, target, target));
            return this;
        }

        /**
         * Add a new flag with a mask.
     * Add a mutually exclusive flag to the map.
     *
         * The flag will be considered enabled for a property value if the result of bitwise anding
         * the value and the mask equals the target, that is: {@code (value & mask) == target}.
         *
         * @param name The name of the flag
         * @param target The value to compare against
         * @param mask A bit mask
         * @return This builder
     * @param mask The bit mask to compare to and with a value
     * @param target The target value to compare the masked value with
     * @param name The name of the flag to include if enabled
     */
        @NonNull
        public Builder addFlag(@NonNull String name, int target, int mask) {
            mFlags.add(new Flag(name, target, mask));
            return this;
        }

        /**
         * Build a new {@link IntFlagMapping} from this builder.
         *
         * @return A new mapping
         */
        @NonNull
        public IntFlagMapping build() {
            return new IntFlagMapping(this);
        }
    public void add(int mask, int target, @NonNull String name) {
        mFlags.add(new Flag(mask, target, name));
    }

    /**
@@ -136,10 +79,10 @@ public final class IntFlagMapping {
        private final int mTarget;
        private final int mMask;

        private Flag(@NonNull String name, int target, int mask) {
            mName = Objects.requireNonNull(name);
        private Flag(int mask, int target, @NonNull String name) {
            mTarget = target;
            mMask = mask;
            mName = Objects.requireNonNull(name);
        }

        /**
+5 −2
Original line number Diff line number Diff line
@@ -19,6 +19,9 @@ package android.view.inspector;
import android.annotation.AttrRes;
import android.annotation.NonNull;

import java.util.Set;
import java.util.function.IntFunction;

/**
 * An interface for mapping the string names of inspectable properties to integer identifiers.
 *
@@ -154,7 +157,7 @@ public interface PropertyMapper {
    int mapIntEnum(
            @NonNull String name,
            @AttrRes int attributeId,
            @NonNull IntEnumMapping mapping);
            @NonNull IntFunction<String> mapping);

    /**
     * Map a string name to an integer ID for an attribute that contains resource IDs.
@@ -178,7 +181,7 @@ public interface PropertyMapper {
    int mapIntFlag(
            @NonNull String name,
            @AttrRes int attributeId,
            @NonNull IntFlagMapping mapping);
            @NonNull IntFunction<Set<String>> mapping);
    /**
     * Thrown from a map method if a property name is already mapped as different type.
     */
+1 −0
Original line number Diff line number Diff line
@@ -8,6 +8,7 @@ java_plugin {

    static_libs: [
        "javapoet",
        "stub-annotations",
    ],

    use_tools_jar: true,
Loading