Add Java style guide entry for using IntDef instead of enum.
Change-Id: Ifa76576d626518f822fcfc3007707ce05fbc8514 Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2927898 Reviewed-by: Carlos Knippschild <carlosk@chromium.org> Reviewed-by: Andrew Grieve <agrieve@chromium.org> Auto-Submit: Carlos Knippschild <carlosk@chromium.org> Commit-Queue: Andrew Grieve <agrieve@chromium.org> Owners-Override: Andrew Grieve <agrieve@chromium.org> Cr-Commit-Position: refs/heads/master@{#888708}
This commit is contained in:

committed by
Chromium LUCI CQ

parent
67e3709d83
commit
f2e58c1dd9
@ -132,6 +132,36 @@ to ensure in debug builds and tests that `destroy()` is called.
|
||||
* Always prefer `androidx.annotation.Nullable`.
|
||||
* It uses `@Retention(SOURCE)` rather than `@Retention(RUNTIME)`.
|
||||
|
||||
### IntDef Instead of Enum
|
||||
|
||||
Java enums generate far more bytecode than integer constants. When integers are
|
||||
sufficient, prefer using an [@IntDef annotation], which will have usage checked
|
||||
by [Android lint].
|
||||
|
||||
Values can be declared outside or inside the `@interface`. We recommend the
|
||||
latter, with constants nested within it as follows:
|
||||
|
||||
```java
|
||||
@IntDef({ContactsPickerAction.CANCEL, ContactsPickerAction.CONTACTS_SELECTED,
|
||||
ContactsPickerAction.SELECT_ALL, ContactsPickerAction.UNDO_SELECT_ALL})
|
||||
@Retention(RetentionPolicy.SOURCE)
|
||||
public @interface ContactsPickerAction {
|
||||
int CANCEL = 0;
|
||||
int CONTACTS_SELECTED = 1;
|
||||
int SELECT_ALL = 2;
|
||||
int UNDO_SELECT_ALL = 3;
|
||||
int NUM_ENTRIES = 4;
|
||||
}
|
||||
// ...
|
||||
void onContactsPickerUserAction(@ContactsPickerAction int action, ...);
|
||||
```
|
||||
|
||||
Values of `Integer` type are also supported, which allows using a sentinel
|
||||
`null` if needed.
|
||||
|
||||
[@IntDef annotation]: https://developer.android.com/studio/write/annotations#enum-annotations
|
||||
[Android lint]: https://chromium.googlesource.com/chromium/src/+/HEAD/build/android/docs/lint.md
|
||||
|
||||
## Tools
|
||||
|
||||
### Automatically formatting edited files
|
||||
|
Reference in New Issue
Block a user