zoukankan      html  css  js  c++  java
  • Java 满血domain

    我们一般创建的Java Model只有getter和setter方法,都是贫血型的;下面是一个满血的Java Model,业务放在实体类中;

    import java.util.*;
    
    public class Person {
    
        private String name;
    
        private Integer age;
    
        private String sex;
    
        /**
         * 兴趣爱好
         */
        private List<String> hobbies = new ArrayList<>();
    
        /**
         * 喜欢吃的食物
         */
        private List<String> foods = new ArrayList<>();
    
        /**
         * 个人经历
         */
        private Map<Integer, Object> personalHistory = new HashMap<>();
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            if(!checkNameChese(name))
                throw new RuntimeException(name + "不规范!");
            this.name = name;
        }
    
        public Integer getAge() {
            return age;
        }
    
        public void setAge(Integer age) {
            if(null == age || age <=0 || age > 120)
                throw new RuntimeException(age + "不规范!");
            this.age = age;
        }
    
        public String getSex() {
            return sex;
        }
    
        public void setSex(String sex) {
            this.sex = sex;
        }
    
        public List<String> getHobbies() {
            return hobbies;
        }
    
        public void setHobbies(List<String> hobbies) {
            this.hobbies = hobbies;
        }
    
        public List<String> getFoods() {
            return foods;
        }
    
        public void setFoods(List<String> foods) {
            this.foods = foods;
        }
    
        public void addFood(String food) {
            if(null == food) {
                throw new RuntimeException("食物不为空!");
            }
            for(String item : hobbies) {
                if(item.equals(food)) {
                    throw new RuntimeException(food + "已填加!");
                }
            }
            foods.add(food);
        }
    
        public void delFood(String food) {
            if(null == food) {
                throw new RuntimeException("食物不为空!");
            }
            if(!hobbies.contains(food)) {
                throw new RuntimeException(food + "不存在!");
            }
            foods.remove(food);
        }
    
        public void addHobby(String hobby) {
            if(null == hobby) {
                throw new RuntimeException("兴趣爱好不为空!");
            }
            for(String item : hobbies) {
                if(item.equals(hobby)) {
                    throw new RuntimeException(hobby + "已填加!");
                }
            }
            hobbies.add(hobby);
        }
    
        public void delHobby(String hobby) {
            if(null == hobby) {
                throw new RuntimeException("兴趣爱好不为空!");
            }
            if(!hobbies.contains(hobby)) {
                throw new RuntimeException(hobby + "不存在!");
            }
            hobbies.remove(hobby);
        }
    
        public void addHistory(Integer age, Object history) {
            if(null == age || age <=0) {
                throw new RuntimeException("年龄不符合规定!");
            }
            if(null == history) {
                throw new RuntimeException("个人经历不为空!");
            }
            if(age > this.age) {
                throw new RuntimeException("个人经历不得大于");
            }
            Iterator<Map.Entry<Integer, Object>> iterator = personalHistory.entrySet().iterator();
            while (iterator.hasNext()) {
                Map.Entry<Integer, Object> next = iterator.next();
                if(next.getKey().equals(age) && next.getValue().equals(history)) {
                    throw new RuntimeException(age + "岁,个人经历:" + history + "已存在!");
                }
            }
            personalHistory.put(age, history);
        }
    
        public void delHistory(Integer age) {
            if(null == age || age <=0) {
                throw new RuntimeException("年龄不符合规定!");
            }
            if(age > this.age) {
                throw new RuntimeException("个人经历不得大于");
            }
            if(!personalHistory.containsKey(age)) {
                throw new RuntimeException(age + "岁,个人经历不存在!");
            }
            Iterator<Map.Entry<Integer, Object>> iterator = personalHistory.entrySet().iterator();
            while (iterator.hasNext()) {
                Map.Entry<Integer, Object> next = iterator.next();
                if(next.getKey().equals(age)) {
                    iterator.remove();
                }
            }
        }
    
        /**
         * 判定输入的是否是汉字
         *
         * @param c 被校验的字符
         *
         * @return true代表是汉字
         */
        private static boolean isChinese(char c) {
            Character.UnicodeBlock ub = Character.UnicodeBlock.of(c);
            if (ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS
                    || ub == Character.UnicodeBlock.CJK_COMPATIBILITY_IDEOGRAPHS
                    || ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_A
                    || ub == Character.UnicodeBlock.GENERAL_PUNCTUATION
                    || ub == Character.UnicodeBlock.CJK_SYMBOLS_AND_PUNCTUATION
                    || ub == Character.UnicodeBlock.HALFWIDTH_AND_FULLWIDTH_FORMS) {
                return true;
            }
            return false;
        }
    
        /**
         * 校验String是否全是中文
         *
         * @param name
         *      被校验的字符串
         * @return true代表全是汉字
         */
        private static boolean checkNameChese(String name) {
            boolean res = true;
            char[] cTemp = name.toCharArray();//转换为数组
            for (int i = 0; i < name.length(); i++) {
                if (!isChinese(cTemp[i])) {//逐个判断是否为中文
                    res = false;
                    break;
                }
            }
            return res;
        }
    
    }
  • 相关阅读:
    centos8 防火墙配置增加端口
    linux上搭建maven私服(下)
    linux上搭建maven私服(中)
    项目成本管理中的PV、EV与AC的区别-实例解释
    配置IKE SA的生存周期(华为)
    IKE SA和IPSec SA的区别
    IPsecVPN协商过程-主模式
    Fortigate防火墙常用命令
    飞塔防火墙清除系统密码
    fatal: unable to access ‘https://github xxxxxxxxx的解决方法
  • 原文地址:https://www.cnblogs.com/mxh-java/p/12511513.html
Copyright © 2011-2022 走看看