FileDocCategorySizeDatePackage
MarshalQueryableEnum.javaAPI DocAndroid 5.1 API8108Thu Mar 12 22:22:10 GMT 2015android.hardware.camera2.marshal.impl

MarshalQueryableEnum

public class MarshalQueryableEnum extends Object implements android.hardware.camera2.marshal.MarshalQueryable
Marshal any simple enum (0-arg constructors only) into/from either {@code TYPE_BYTE} or {@code TYPE_INT32}.

Default values of the enum are mapped to its ordinal; this can be overridden by providing a manual value with {@link #registerEnumValues}.

param
the type of {@code Enum}

Fields Summary
private static final String
TAG
private static final boolean
VERBOSE
private static final int
UINT8_MIN
private static final int
UINT8_MAX
private static final int
UINT8_MASK
private static final HashMap
sEnumValues
Constructors Summary
Methods Summary
public android.hardware.camera2.marshal.MarshalercreateMarshaler(android.hardware.camera2.utils.TypeReference managedType, int nativeType)

        return new MarshalerEnum(managedType, nativeType);
    
private static TgetEnumFromValue(java.lang.Class enumType, int value)
Finds the enum corresponding to it's numeric value. Opposite of {@link #getEnumValue} method.

param
enumType Class of the enum we want to find
param
value The numeric value of the enum
return
An instance of the enum

        int ordinal;

        int[] registeredValues = sEnumValues.get(enumType);
        if (registeredValues != null) {
            ordinal = -1;

            for (int i = 0; i < registeredValues.length; ++i) {
                if (registeredValues[i] == value) {
                    ordinal = i;
                    break;
                }
            }
        } else {
            ordinal = value;
        }

        T[] values = enumType.getEnumConstants();

        if (ordinal < 0 || ordinal >= values.length) {
            throw new IllegalArgumentException(
                    String.format(
                            "Argument 'value' (%d) was not a valid enum value for type %s "
                                    + "(registered? %b)",
                            value,
                            enumType, (registeredValues != null)));
        }

        return values[ordinal];
    
private static intgetEnumValue(T enumValue)
Get the numeric value from an enum.

This is usually the same as the ordinal value for enums that have fully sequential values, although for C-style enums the range of values may not map 1:1.

param
enumValue Enum instance
return
Int guaranteed to be ABI-compatible with the C enum equivalent

        int[] values;
        values = sEnumValues.get(enumValue.getClass());

        int ordinal = enumValue.ordinal();
        if (values != null) {
            return values[ordinal];
        }

        return ordinal;
    
public booleanisTypeMappingSupported(android.hardware.camera2.utils.TypeReference managedType, int nativeType)

        if (nativeType == TYPE_INT32 || nativeType == TYPE_BYTE) {
            if (managedType.getType() instanceof Class<?>) {
                Class<?> typeClass = (Class<?>)managedType.getType();

                if (typeClass.isEnum()) {
                    if (VERBOSE) {
                        Log.v(TAG, "possible enum detected for " + typeClass);
                    }

                    // The enum must not take extra arguments
                    try {
                        // match a class like: "public enum Fruits { Apple, Orange; }"
                        typeClass.getDeclaredConstructor(String.class, int.class);
                        return true;
                    } catch (NoSuchMethodException e) {
                        // Skip: custom enum with a special constructor e.g. Foo(T), but need Foo()
                        Log.e(TAG, "Can't marshal class " + typeClass + "; no default constructor");
                    } catch (SecurityException e) {
                        // Skip: wouldn't be able to touch the enum anyway
                        Log.e(TAG, "Can't marshal class " + typeClass + "; not accessible");
                    }
                }
            }
        }

        return false;
    
public static voidregisterEnumValues(java.lang.Class enumType, int[] values)
Register a non-sequential set of values to be used with the marshal/unmarshal functions.

This enables get/set to correctly marshal the enum into a value that is C-compatible.

param
enumType The class for an enum
param
values A list of values mapping to the ordinals of the enum


                                                        
              
        if (enumType.getEnumConstants().length != values.length) {
            throw new IllegalArgumentException(
                    "Expected values array to be the same size as the enumTypes values "
                            + values.length + " for type " + enumType);
        }
        if (VERBOSE) {
            Log.v(TAG, "Registered enum values for type " + enumType + " values");
        }

        sEnumValues.put(enumType, values);