zno2

java 省份小工具

 

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

/**
 * GB/T 2260-2007
 * @author witas
 *
 */
public class ProvinceUtils {

    private static final String PROV = 
            "110000_京_华北_北京市\r\n" + 
            "120000_津_华北_天津市\r\n" + 
            "130000_冀_华北_河北省\r\n" + 
            "140000_晋_华北_山西省\r\n" + 
            "150000_蒙_华北_内蒙古自治区\r\n" + 
            "210000_辽_东北_辽宁省\r\n" + 
            "220000_吉_东北_吉林省\r\n" + 
            "230000_黑_东北_黑龙江省\r\n" + 
            "310000_沪_华东_上海市\r\n" + 
            "320000_苏_华东_江苏省\r\n" + 
            "330000_浙_华东_浙江省\r\n" + 
            "340000_皖_华东_安徽省\r\n" + 
            "350000_闽_华南_福建省\r\n" + 
            "360000_赣_华中_江西省\r\n" + 
            "370000_鲁_华东_山东省\r\n" + 
            "410000_豫_华中_河南省\r\n" + 
            "420000_鄂_华中_湖北省\r\n" + 
            "430000_湘_华中_湖南省\r\n" + 
            "440000_粤_华南_广东省\r\n" + 
            "450000_桂_华南_广西壮族自治区\r\n" + 
            "460000_琼_华南_海南省\r\n" + 
            "500000_渝_西南_重庆市\r\n" + 
            "510000_川_西南_四川省\r\n" + 
            "520000_贵_西南_贵州省\r\n" + 
            "530000_云_西南_云南省\r\n" + 
            "540000_藏_西南_西藏自治区\r\n" + 
            "610000_陕_西北_陕西省\r\n" + 
            "620000_甘_西北_甘肃省\r\n" + 
            "630000_青_西北_青海省\r\n" + 
            "640000_宁_西北_宁夏回族自治区\r\n" + 
            "650000_新_西北_新疆维吾尔自治区\r\n" + 
            "710000_台_港澳台_台湾省\r\n" + 
            "810000_港_港澳台_香港特别行政区\r\n" + 
            "820000_澳_港澳台_澳门特别行政区";
    private static final Map<Integer,Prov> PROV_MAP = new HashMap<>();
    private static final List<Prov> PROV_LIST = new ArrayList<>();
    static {
        String[] splits = PROV.split("\r\n");
        for (String s : splits) {
            String[] items = s.split("_");
            
            Prov prov = new Prov();
            prov.setCode(Integer.valueOf(items[0]));
            prov.setShortName(items[1]);
            prov.setArea(items[2]);
            prov.setName(items[3]);
            PROV_LIST.add(prov);
            
            PROV_MAP.put(Integer.valueOf(items[0]), prov);
        }
    }
    
    
    
    
    /**
     * 获取省名
     * @param code
     * @return
     */
    public static String getProv(Integer code) {
        Prov prov = PROV_MAP.get(code);
        if(prov != null) {
            return prov.getName();
        }
        return code+"";
    }
    
    public static Map<Integer,Prov> getProvMap(){
        Map<Integer,Prov> result = new HashMap<>();
        for(Prov p : PROV_LIST) {
            result.put(p.getCode(), JSONObject.parseObject(JSONObject.toJSONString(p), Prov.class));
        }
        return result;
    }
    
    
    /**
     * 获取省份列表
     * @return
     */
    public static List<Prov> getProvList(){
        return JSONArray.parseArray(JSONObject.toJSONString(PROV_LIST), Prov.class);
    }
    
    /**
     * 获取地域省份
     * @param area
     * @return
     */
    public static List<Prov> getArea(String area){
        Area a= Area.forName(area);
        return getArea(a);
    }
    /**
     * 获取地域省份
     * @param area
     * @return
     */
    public static List<Prov> getArea(Area area){
        if(area == null) {
            return new ArrayList<>();
        }
        List<Prov> list = new ArrayList<>();
        for(Prov p : PROV_LIST) {
            if(p.getArea().equals(area.getName())) {
                list.add(JSONObject.parseObject(JSONObject.toJSONString(p), Prov.class));
            }
        }
        return list;
    }
    
    /**
     * 获取地域 
     * @param code
     * @return
     */
    public static Area toArea(Integer code) {
        Prov prov = PROV_MAP.get(code);
        return toArea(prov);
    }

    /**
     * 获取地域
     * @param prov
     * @return
     */
    public static Area toArea(Prov prov) {
        if(prov == null || prov.getArea()== null) {
            return null;
        }
        Area a= Area.forName(prov.getArea());
        return a;
    }
    
    
    
    public static enum Area {
        XB("西北"),
        XN("西南"),
        HB("华北"),
        HZ("华中"),
        HN("华南"),
        HD("华东"),
        DB("东北"),
        ;
        
        private String name;
        private Area(String name) {
            this.name = name;
        }
        public String getName() {
            return name;
        }
        public void setName(String name) {
            this.name = name;
        }
        
        public static Area forName(String area) {
            Area a=null;
            try {
                a = Area.valueOf(area);
            } catch (Exception e) {
            }
            if(a == null) {
                if(Area.XB.getName().equals(area)) {
                    a = Area.XB;
                }else if(Area.XN.getName().equals(area)) {
                    a = Area.XN;
                }else if(Area.HB.getName().equals(area)) {
                    a = Area.HB;
                }else if(Area.HZ.getName().equals(area)) {
                    a = Area.HZ;
                }else if(Area.HN.getName().equals(area)) {
                    a = Area.HN;
                }else if(Area.HD.getName().equals(area)) {
                    a = Area.HD;
                }else if(Area.DB.getName().equals(area)) {
                    a = Area.DB;
                }
            }
            return a;
        }
    }
    
    public static class Prov {
        private Integer code;
        private String shortName;
        private String name;
        private String area;
        public Integer getCode() {
            return code;
        }
        public void setCode(Integer code) {
            this.code = code;
        }
        public String getShortName() {
            return shortName;
        }
        public void setShortName(String shortName) {
            this.shortName = shortName;
        }
        public String getName() {
            return name;
        }
        public void setName(String name) {
            this.name = name;
        }
        public String getArea() {
            return area;
        }
        public void setArea(String area) {
            this.area = area;
        }
        @Override
        public String toString() {
            return code + "_" + shortName + "_" + name + "_" + area;
        }
    }
}

 

posted on 2023-06-02 08:18  zno2  阅读(65)  评论(0编辑  收藏  举报

导航