Convert parameterized Enum to enumerated annotation in android

I have a question regarding android @IntDef Annotation. I know that with its main use it should replace enum . What if I have a parameterized enumeration with several fixed values, for example

 public enum MyEnum { YES(true, 1), NO(false, 0); private boolean boolState; private boolean intState; MyEnum(boolean boolState, int intState) { this.boolState = boolState; this.intState = intState; } public boolean getBoolState() { return boolState; } public int getIntState() { return intState; } } 

How will it be replaced with Enumerated Annotation in Android?

Is it even possible to think of doing something similar in this case? I searched everywhere, but I did not find any answer for this.

Thank you in advance!

+6
source share
3 answers

I don’t think you can find anything because:

IntDef is a way to replace an integer enumeration, where there is a parameter that should accept only explicit int values.

You can read about it here . The listed annotations are for simple types, you can use it for strings as well StringDef . Use an enumeration when you need its functions. Do not avoid it strictly. For your case, I think that creating a class instead of enum would look like this:

 public class MyEnum { public static final MyEnum YES = new MyEnum(true, 1); public static final MyEnum NO = new MyEnum(false, 0); private boolean boolState; private int intState; MyEnum(boolean boolState, int intState) { this.boolState = boolState; this.intState = intState; } public boolean getBoolState() { return boolState; } public int getIntState() { return intState; } @Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; MyEnum myEnum = (MyEnum) o; return boolState == myEnum.boolState && intState == myEnum.intState; } } 

and you can use constants in your code. But if you use enumerations, you will have a type check (you can only accept the enumerated values) and method overload (each enumeration constant can have its own implementation of the method). If you want to use less space, and this is the only reason why you want to avoid using an enumeration, I would suggest you not to be worth it.

+7
source

I follow the listing rules in Android development:

  • If it has no parameters, use intdef / stringdef,
  • If it has parameters, use enum

If there is a way to use the enumeration, I will definitely consider it where it does not undermine the code.

Much has been done from the Colt Mcanlis video: https://www.youtube.com/watch?v=Hzs6OBcvNQE&feature=youtu.be

however, there were some rather shaky numbers in it, as Jake Wharton pointed out: https://plus.google.com/+JakeWharton/posts/bTtjuFia5wm

The main disadvantage of enumerations is that they use more memory than constants, but if this enumeration helps to improve the code, I say use it, and not micro-optimize. Just don't go overboard using them and don't be aware of their footprint.

+1
source

I'm late, but either way, since intdef introduces the annotation, you can create the annotation using a custom class and then use it in the same way. given the fact that annotations are needed by primitives, you need to pass the interface as a type of annotation class and use subclasses as an array of values.

Example:

 public interface GenericContainer<T, X> { public T getValueOne(); public X getValueTwo(); } 

then the implementation for true / 1

 public class TrueContainer implements GenericContainer<Boolean, Integer> { @Override public Boolean getValueOne() { return true; } @Override public Integer getValueTwo() { return 1; } } 

and others for false / 0

 public class FalseContainer implements GenericContainer<Boolean, Integer> { @Override public Boolean getValueOne() { return false; } @Override public Integer getValueTwo() { return 0; } } 

finally use them:

  @Retention(RetentionPolicy.SOURCE) @GenericDef({TrueContainer.class, FalseContainer.class}) public @interface genericTest{} boolean test = isTest(new FalseContainer()); 
0
source

Source: https://habr.com/ru/post/1015340/


All Articles