zoukankan      html  css  js  c++  java
  • 枚举类工具

    枚举类是程序中常用的一种类型,当经常面对说明与标识混乱的情况。在此记录一个枚举类的写法,方便标识与说明的绑定

    基类:

    import java.util.ArrayList;
    import java.util.HashMap;
    import java.util.List;
    import java.util.Map;
    
    public abstract class StandardType {
        private String key;
        private String title;
    
        public StandardType(String key, String title) {
            this.title = title;
            this.key = key;
        }
    
        public String getTitle() {
            return title;
        }
    
        public static String getTitle(StandardType[] values, String key) {
            StandardType val = StandardType.getEnum(values, key);
            if (val == null) {
                return null;
            } else {
                return val.getTitle();
            }
        }
    
        public String getKey() {
            return key;
        }
    
        public static String getKey(StandardType[] values, String title) {
            for (StandardType val : values) {
                if (val.getTitle().equals(title)) {
                    return val.getKey();
                }
            }
            return null;
        }
    
        public static StandardType getEnum(StandardType[] values, String key) {
            for (StandardType val : values) {
                if (val.getKey().equals(key)) {
                    return val;
                }
            }
            return null;
        }
    
        public String toString() {
            return key;
        }
    
        public boolean equals(StandardType type) {
            if (type == null) {
                return false;
            }
            return this.key.equals(type.getKey());
        }
    
        /**
         * get value options based on values
         * 
         * @param values
         * @return
         */
        public static List<Map<String, String>> getOpts(StandardType[] values) {
            List<Map<String, String>> opts = new ArrayList<Map<String, String>>();
            Map<String, String> opt = null;
            for (StandardType val : values) {
                opt = new HashMap<String, String>();
                opt.put("key", val.getKey());
                opt.put("title", val.getTitle());
                opts.add(opt);
            }
            return opts;
        }
    
        /**
         * get value options based on values(Fuzzy query)
         * 
         * @param values
         * @return
         */
        public static List<Map<String, String>> getSimilarOpts(StandardType[] values, String input) {
            List<Map<String, String>> opts = new ArrayList<Map<String, String>>();
            Map<String, String> opt = null;
            for (StandardType val : values) {
                if (val.getTitle().contains(input)) {
                    opt = new HashMap<String, String>();
                    opt.put("key", val.getKey());
                    opt.put("title", val.getTitle());
                    opts.add(opt);
                }
            }
            return opts;
        }
    }

    枚举类:

        /**
         * 支付方式
         */
        public static class PayWay extends StandardType {
            public final static PayWay WeChatPay = new PayWay("WeChatPay", "微信支付");
            public final static PayWay AliPay = new PayWay("AliPay", "支付宝支付");
            public final static PayWay UnionPay = new PayWay("UnionPay", "银联支付");
            public final static PayWay CashPay = new PayWay("CashPay", "现金支付");
            public final static PayWay[] values = { WeChatPay, AliPay, UnionPay, CashPay };
    
            public PayWay(String key, String title) {
                super(key, title);
            }
        }

    使用:

        public static void main(String[] args) {
            PayWay.AliPay.toString();// AliPay
            // dome1
            StandardType patWay = PayWay.getEnum(PayWay.values, "AliPay");
            patWay.getKey();// AliPay
            patWay.getTitle();// 支付宝支付
            // dome2
            PayWay.getKey(PayWay.values, "支付宝支付");// AliPay
            PayWay.getTitle(PayWay.values, "AliPay");// 支付宝支付
            // dome3
            List<Map<String, String>> listMap1 = PayWay.getOpts(PayWay.values);//所有map
            List<Map<String, String>> listMap2 = PayWay.getSimilarOpts(PayWay.values, "支");//一条支付宝的map
        }
  • 相关阅读:
    vscode webstrom 配置 eslint 使用 airbnb 规范
    IntelliJ idea webstrom Visual Studio Code vscode 设置cmder为默认终端 Terminal
    intellij idea 大内存优化配置 idea64.exe.vmoptions文件配置
    解决因 gtx 显卡而导致的 google chrome 颜色显示不正常。色彩变淡发白,其实很简单
    href=http:// href=// 的区别,src=http:// src=// 的区别。 链接里不带http,链接里直接使用双斜线 // 有什么不同。http://和//有什么区别?
    前后端分离之fiddler前端开发代理 autoresponder 正则表达式 regex:(?insx) 修正符详解
    开发环境部署脚手架搭建 步骤
    开发环境部署git 步骤
    requestAnimationFrame移动端实现回到顶部效果
    占位图片
  • 原文地址:https://www.cnblogs.com/lossingdawn/p/6732003.html
Copyright © 2011-2022 走看看