用注解替换枚举

作者: 道阻且长_砥砺前行 | 来源:发表于2017-01-03 16:56 被阅读0次

概述


Android中的注解(Annotations)可以修饰返回值或者方法参数,加强编写代码时候的类型安全,在进行代码检查的时候就能提示代码潜在的问题。最常用的例子便是使用注解替换枚举类型。相关资料可以参考Google官方文档,或者我的翻译文章

Google在Android应用开发人员指南中不鼓励使用枚举类型:

枚举类型比起静态常量,需要消耗两倍以上的内存,请严格避免在Android上使用枚举类型。

@IntDef


@IntDef是一种替换整数类型枚举的方法,比如下面这个例子,我们记录数据类型:

public class ItemTypeDescriptor {
    public static final int TYPE_MUSIC = 0;
    public static final int TYPE_PHOTO = 1;
    public static final int TYPE_TEXT = 2;

    public final int itemType;

    public ItemTypeDescriptor(int itemType) {
        this.itemType = itemType;
    }
}

在上面代码中,没有任何的验证手段来确保传给构造函数的参数是符合要求的。这时候我们就可以使用@IntDef注解来确保传递数值的合法性。

public class ItemTypeDescriptor {
    // ... type definitions
    // Describes when the annotation will be discarded
    @Retention(RetentionPolicy.SOURCE)
    // Enumerate valid values for this interface
    @IntDef({ TYPE_MUSIC, TYPE_PHOTO, TYPE_TEXT })
    // Create an interface for validating int types
    public @interface ItemTypeDef { }
    // Mark the argument as restricted to these enumerated types
    public ItemTypeDescriptor(@ItemTypeDef int itemType) {
        this.itemType = itemType;
    }
    
    // get data
      @ItemTypeDef
      public int getItemType() {
        return itemType;
      }
}

现在用非法的参数调用构造函数,就会在Android Studio中提示错误,因为编译器明确的知道了所需要的数据类型。

@StringDef


@StringDef@IntDef类似,用来替换字符串类型的枚举。

public class FilterColorDescriptor {
    public static final String FILTER_BLUE = "blue";
    public static final String FILTER_RED = "red";
    public static final String FILTER_GRAY = "gray";

    public final String filterColor;

    public FilterColorDescriptor(String filterColor) {
        this.filterColor = filterColor;
    }
}

同上面的一样,我们这里也用@StringDef来限定所传入的数据类型

public class FilterColorDescriptor {
    // ... type definitions
    // Describes when the annotation will be discarded
    @Retention(RetentionPolicy.SOURCE)
    // Enumerate valid values for this interface
    @StringDef({ FILTER_BLUE, FILTER_RED, FILTER_GRAY })
    // Create an interface for validating int types
    public @interface FilterColorDef { }
    // Mark the argument as restricted to these enumerated types
    public FilterColorDescriptor(@FilterColorDef String filterColor) {
        this.filterColor = filterColor;
    }
    
    // get data
      @FilterColorDef
      public String getFilterColor() {
        return filterColor;
      }
}

相关文章

  • 用注解替换枚举

    概述 Android中的注解(Annotations)可以修饰返回值或者方法参数,加强编写代码时候的类型安全,在进...

  • 优雅编程之这样使用枚举和注解,你就“正常”了(二十九)

    开心一笑 提出问题 项目中如何使用枚举和注解??? 解决问题 用enum替换int常量 例如:下面是公司项目的一个...

  • Effective Java 2

    五、枚举和注解 30、用enum代替int常量 132,135 31、用实例域代替序数 不要依赖于枚举的顺序编程 ...

  • effective java 第三周

    第6章 枚举和注解 第30条:用 enum 代替 int 常量 在没有 enum 之前表示枚举类型的常用模式时声...

  • 210219:用注解翻译码表-Java中16进制与字符串之间的相

    一. 用注解翻译码表 1. 定义注解 使用:把value=codeID 2. 反射翻译工具类 3. 定义枚举 4....

  • 第六章

    主要讨论枚举和注解 第二十六条 用enum代替int常量1.枚举没有公有的可以访问的构造器,客户端不能创建枚举类型...

  • 实现validation注解进行校验枚举值

    背景: validation注解官方没有定义枚举类型的注解,于是这里自定义一个枚举类型注解,来实现对象的验证。 不...

  • 2020-01-31 关于枚举与数据字典

    说说什么时候使用了枚举:框架中,使用了枚举类的注解,即数据持久化时使用注解,当参数为枚举时,存入库的信息为枚举上注...

  • 枚举、注解

    枚举 类的对象只有有限个,确定的;当需要定义一组常量时,最好使用枚举类1.如何定义枚举类方式一:jdk5.0之前,...

  • Android注解笔记

    注解(Annotation) 元注解 @Target 表明我们注解可以出现的地方。是一个ElementType枚举...

网友评论

    本文标题:用注解替换枚举

    本文链接:https://www.haomeiwen.com/subject/wzrnvttx.html