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

Commit a61e7cd0 authored by Ember Rose's avatar Ember Rose
Browse files

Add IntEnumMapping

It's just a wapper for SparseArray, but the builder makes it easier to
work with in code-gen, and provide an immutability guarantee.

Test: atest android.view.inspector.cts
Bug: 123295401
Change-Id: I479d7d3c5ebf4f7e3d82aa05f4e566db844d43ba
parent a47310fd
Loading
Loading
Loading
Loading
+11 −1
Original line number Diff line number Diff line
@@ -53005,6 +53005,16 @@ package android.view.inspector {
    ctor public InspectionCompanion.UninitializedPropertyMapException();
  }
  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 {
    method @NonNull public java.util.Set<java.lang.String> get(int);
  }
@@ -53025,7 +53035,7 @@ 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.util.SparseArray<java.lang.String>);
    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 mapLong(@NonNull String, @AttrRes int);
    method public int mapObject(@NonNull String, @AttrRes int);
+1 −0
Original line number Diff line number Diff line
@@ -106,6 +106,7 @@ public @interface InspectableProperty {
    /**
     * One entry in an enumeration packed into a primitive {int}.
     *
     * @see IntEnumMapping
     * @hide
     */
    @Target({TYPE})
+102 −0
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);
        }
    }
}
+4 −3
Original line number Diff line number Diff line
@@ -21,10 +21,11 @@ import android.annotation.NonNull;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.Objects;
import java.util.Set;

/**
 * Maps the values of an {@code int} property to arrays of string for properties that encode flags.
 * Maps the values of an {@code int} property to sets of string for properties that encode flags.
 *
 * An {@link InspectionCompanion} may provide an instance of this class to a {@link PropertyMapper}
 * for flag values packed into primitive {@code int} properties.
@@ -45,7 +46,7 @@ public final class IntFlagMapping {
     * Get an array 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
     * @return The names of the enabled flags, empty if no flags enabled
     */
    @NonNull
    public Set<String> get(int value) {
@@ -136,7 +137,7 @@ public final class IntFlagMapping {
        private final int mMask;

        private Flag(@NonNull String name, int target, int mask) {
            mName = name;
            mName = Objects.requireNonNull(name);
            mTarget = target;
            mMask = mask;
        }
+2 −3
Original line number Diff line number Diff line
@@ -18,7 +18,6 @@ package android.view.inspector;

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

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

    /**
     * Map a string name to an integer ID for a flag set packed into an int property.
     *
     * @param name The name of the property
     * @param attributeId If the property is from an XML attribute, the resource ID of the property
     * @param mapping A mapping from int to an array of strings
     * @param mapping A mapping from int to a set of strings
     * @return An integer ID for the property
     * @throws PropertyConflictException If the property name is already mapped as another type.
     */