Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Enums and android annotation intDef

I have an enum:

public enum AppEnums {     SERVICE_ERROR,     CONNECTION_ERROR; } 

and I want to use it in an intDef of Android Annotation:

@IntDef({AppEnums.CONNECTION_ERROR, AppEnums.SERVICE_ERROR}) public @interface ServiceErrors { } 

error shows:

incompatible types found, required: 'long'

What I can do with this incompatibility?

I don't want to handle values of AppEnum parameters manually, Enum create values automatically ordinarily. AppEnums.CONNECTION_ERROR.ordinal() return int value of enum parameter but don't work here.

like image 656
AliSh Avatar asked Aug 16 '15 06:08

AliSh


People also ask

What is @IntDef?

Kotlin |Java. @Retention(value = AnnotationRetention.SOURCE) @Target(allowedTargets = [AnnotationTarget.ANNOTATION_CLASS]) annotation IntDef. Denotes that the annotated element of integer type, represents a logical type and that its value should be one of the explicitly named constants.

Should I use enum on android?

Enums often require more than twice as much memory as static constants. You should strictly avoid using enums on Android.

What are enums and annotations Java?

Enums could be treated as a special type of classes and annotations as a special type of interfaces. The idea of enums is simple, but quite handy: it represents a fixed, constant set of values. What it means in practice is that enums are often used to design the concepts which have a constant set of possible states.

What is enum Android?

Enum in java is a data type that contains fixed set of constants. When we required predefined set of values which represents some kind of data, we use ENUM. We always use Enums when a variable can only take one out of a small set of possible values.

What is intdef in Android enums?

You should strictly avoid using enums on Android." IntDef is a way of replacing an integer enum where there's a parameter that should only accept explicit int values. For example, suppose we want to record the type of a feed item as shown below:

Should you avoid using enums on Android?

You should strictly avoid using enums on Android." IntDef is a way of replacing an integer enum where there's a parameter that should only accept explicit int values. For example, suppose we want to record the type of a feed item as shown below: Right now there is no validations to ensure that the type passed into the constructor is valid.

What is the difference between stringdef and string enum?

Refer to the official Android annotations guide for more details. StringDef is similarly a way of replacing an string enum where there's a parameter that should only accept explicit string values. For example, suppose we want to record the value for a setting as shown below:

What is the use of method return annotation in Java?

This annotation can then decorate a return value or a method parameter, giving hints to tools you use about the values that are accepted or can be expected. Their main purpose is to enforce type safety for method parameters and/or method return values while writing the source code.


1 Answers

The main idea of IntDef annotation is to use set of int constants like an enum, but without enum. In this case you have to declare all constants manually.

@IntDef({Status.IDLE, Status.PROCESSING, Status.DONE, Status.CANCELLED}) @Retention(RetentionPolicy.SOURCE) @interface Status {     int IDLE = 0;     int PROCESSING = 1;     int DONE = 2;     int CANCELLED = 3; } 

You can see detailed example here.

like image 66
Oleksii K. Avatar answered Oct 05 '22 11:10

Oleksii K.