MarshalQueryableEnumpublic class MarshalQueryableEnum extends Object implements android.hardware.camera2.marshal.MarshalQueryableMarshal 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}. |
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 |
Methods Summary |
---|
public android.hardware.camera2.marshal.Marshaler | createMarshaler(android.hardware.camera2.utils.TypeReference managedType, int nativeType)
return new MarshalerEnum(managedType, nativeType);
| private static T | getEnumFromValue(java.lang.Class enumType, int value)Finds the enum corresponding to it's numeric value. Opposite of {@link #getEnumValue} method.
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 int | getEnumValue(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.
int[] values;
values = sEnumValues.get(enumValue.getClass());
int ordinal = enumValue.ordinal();
if (values != null) {
return values[ordinal];
}
return ordinal;
| public boolean | isTypeMappingSupported(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 void | registerEnumValues(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.
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);
|
|