zoukankan      html  css  js  c++  java
  • EntityNotFoundException EntityExistException

    package me.zhengjie.common.exception;
    
    import org.springframework.util.StringUtils;
    
    import java.util.HashMap;
    import java.util.Map;
    import java.util.stream.IntStream;
    
    /**
     * @author jie
     * @date 2018-11-23
     */
    public class EntityNotFoundException extends RuntimeException {
    
        public EntityNotFoundException(Class clazz, Object... searchParamsMap) {
            super(EntityNotFoundException.generateMessage(clazz.getSimpleName(), toMap(String.class, String.class, searchParamsMap)));
        }
    
        private static String generateMessage(String entity, Map<String, String> searchParams) {
            return StringUtils.capitalize(entity) +
                    " 不存在 " +
                    searchParams;
        }
    
        private static <K, V> Map<K, V> toMap(
                Class<K> keyType, Class<V> valueType, Object... entries) {
            if (entries.length % 2 == 1)
                throw new IllegalArgumentException("Invalid entries");
            return IntStream.range(0, entries.length / 2).map(i -> i * 2)
                    .collect(HashMap::new,
                            (m, i) -> m.put(keyType.cast(entries[i]), valueType.cast(entries[i + 1])),
                            Map::putAll);
        }
    
    }
    package me.zhengjie.common.exception;
    
    import org.springframework.util.StringUtils;
    
    import java.util.HashMap;
    import java.util.Map;
    import java.util.stream.IntStream;
    
    /**
     * @author jie
     * @date 2018-11-23
     */
    public class EntityExistException extends RuntimeException {
    
        public EntityExistException(Class clazz, Object... saveBodyParamsMap) {
            super(EntityExistException.generateMessage(clazz.getSimpleName(), toMap(String.class, String.class, saveBodyParamsMap)));
        }
    
        private static String generateMessage(String entity, Map<String, String> saveBodyParams) {
            return StringUtils.capitalize(entity) +
                    " 已存在 " +
                    saveBodyParams;
        }
    
        private static <K, V> Map<K, V> toMap(
                Class<K> keyType, Class<V> valueType, Object... entries) {
            if (entries.length % 2 == 1)
                throw new IllegalArgumentException("Invalid entries");
            return IntStream.range(0, entries.length / 2).map(i -> i * 2)
                    .collect(HashMap::new,
                            (m, i) -> m.put(keyType.cast(entries[i]), valueType.cast(entries[i + 1])),
                            Map::putAll);
        }
    }
    throw new EntityNotFoundException(User.class, "name", username);
  • 相关阅读:
    bzoj 2142 礼物——扩展lucas模板
    bzoj 4591 [Shoi2015]超能粒子炮·改——组合数前缀和
    bzoj 4403 序列统计——转化成组合数的思路
    bzoj 2982 combination——lucas模板
    bzoj 3505 [Cqoi2014]数三角形——排列组合
    bzoj 3398 [Usaco2009 Feb]Bullcow 牡牛和牝牛——前缀和优化dp / 排列组合
    bzoj 1009 [HNOI2008]GT考试——kmp+矩阵优化dp
    bzoj 2427 [HAOI2010]软件安装
    bzoj 1951 [Sdoi2010]古代猪文 ——数学综合
    bzoj4247挂饰——DP
  • 原文地址:https://www.cnblogs.com/tonggc1668/p/11216969.html
Copyright © 2011-2022 走看看