Java枚举类妙用

Kent_Sun 23 0 0
  • 上代码:

    @Getter
    public enum NodeIdStatusRef {
    
      /**
       * reference
       */
      NI001_A001_AGREE(NodeIdEnum.NI001.getNodeId(), OrderStatus.A101.getCode(), OrderStatus.A101.getCode()),
      NI001_A001_REJECT(NodeIdEnum.NI002.getNodeId(), OrderStatus.A102.getCode(), OrderStatus.A102.getCode()),
      NI001_A002(NodeIdEnum.NI002.getNodeId(), OrderStatus.A102.getCode(), null),
      ;
    
      /**
       * 节点ID
       */
      private String nodeId;
      /**
       * 审批通过对应的状态
       */
      private String agreeStatus;
      /**
       * 审批拒绝对应的状态
       */
      private String rejectStatus;
    
      NodeIdStatusRef(String nodeId, String agreeStatus, String rejectStatus) {
          this.nodeId = nodeId;
          this.agreeStatus = agreeStatus;
          this.rejectStatus = rejectStatus;
      }
    
      /**
       * 操作类型
       */
      private String opType;
    
      /**
       * 根据nodeId和操作类型查对应状态
       *
       * @param nodeId 节点ID
       * @param opType 操作类型
       * @return 对应状态
       */
      public static String lookupStatusByOpType(String nodeId, String opType) {
          String status = null;
          for (NodeIdStatusRef item : NodeIdStatusRef.values()) {
              if (nodeId.equals(item.getNodeId())) {
                  if (TopcarConstants.APPROVE_RESULT_CODE_AGREE.equals(opType)) {
                      status = item.getAgreeStatus();
                  } else if (TopcarConstants.APPROVE_RESULT_CODE_REJECT.equals(opType)) {
                      status = item.getRejectStatus();
                  }
              }
          }
          return status;
      }

    这个类需要以下两个枚举类:用来做参照,上面这个枚举类的方法是做的参照,提供调用方法【lookupStatusByOpType(根据节点id和操作类型,返回对应状态)】,入参是操作类型和节点ID

    /**
    * Notice: 节点ID枚举类
    */
    @Getter
    @AllArgsConstructor
    public enum NodeIdEnum {
      /**
       * nodeId
       */
      NI001("TaskNode_d8lv3","资管公司初审"),
      NI002("TaskNode_etu76","资管公司复审"),
      NI003("TaskNode_hlmgj","国银初审"),
      NI004("TaskNode_lftzv","国银复审"),
      NI005("TaskNode_apqg5","资管公司请款初审"),
      NI006("TaskNode_r2nxp","资管公司请款复审"),
      NI007("TaskNode_bhbol","国银请款初审"),
      NI008("TaskNode_s48j9","国银请款复审"),
          ;
      /**
       * 节点ID
       */
      private String nodeId;
      /**
       * 描述
       */
      private String desc;
    }
    /**
    * Notice: 订单状态
    */
    @Getter
    @AllArgsConstructor
    public enum OrderStatus {
      /**
       * 枚举定义
       */
      A101("101", "等待风控报告"),
      A102("102", "自动审批通过"),
      A103("103", "自动审批拒绝"),
      A104("104", "资管公司风控审批"),
      A105("105", "国银风控审批"),
      A106("106", "风控审批前取消"),
      A107("107", "等待补充合同信息"),
      A108("108", "风控审批后取消"),
      A109("109", "等待客户签约"),
      A110("110", "客户签约取消"),
      A111("111", "等待渠道请款"),
      A112("112", "资管公司请款审批"),
      A113("113", "国银请款审批"),
      A114("114", "请款取消"),
      A115("115", "取消付款"),
      A116("116", "风控前拒绝"),
      A117("117", "风控后拒绝"),
      ;
      private String code;
      private String desc;
    }

枚举类转Map 参考[原文](http://fokman.iteye.com/blog/1568905)

public enum IdeasCMD {  
    RESERVED(0), PING(1), PING_ACK(2), KEY_EXCHANGE(3), KEY_EXCHANGE_ACK(4), CONNECT(5), CONNECT_ACK(6), DISCONNECT(7), KEEP_ALIVE(  
            8), KEEP_ALIVE_ACK(9)   ;  

    private final int value;  

    /** 
     * enum lookup map 
     */  
    private static final Map<Integer, IdeasCMD> lookup = new HashMap<Integer, IdeasCMD>();  

    static {  
        for (IdeasCMD s : EnumSet.allOf(IdeasCMD.class)) {  
            lookup.put(s.getValue(), s);  
        }  
    }  

    IdeasCMD(int value) {  
        this.value = value;  
    }  

    public int getValue() {  
        return value;  
    }  

    public static IdeasCMD lookup(int value) {  
        return lookup.get(value);  
    }  
}
预览图
收藏
评论区