Spring 工具类

序:

这些工具类不仅仅只局限于spring框架的内部使用,完全可以作为独立的工具类在我们编写的项目中进行使用。好处便是代码质量提高,编写效率加快。今天在阅读

spring源码时,发现Assert类的使用,于是便参考网络资源,在这里做一个记录。

工具类:

断言Assert类

包名-org.springframework.util

public abstract class Assert {
 	public static void isTrue(boolean expression, String message) {
 		if (!expression) {
 			throw new IllegalArgumentException(message);
 		}
 	}
 	/**
 	 * 1.根据某个表达式的boolean类型,进行断言。
 	 */
 	public static void isTrue(boolean expression) {
 		isTrue(expression, "[Assertion failed] - this expression must be true");
 	}
 	public static void isNull(Object object, String message) {
 		if (object != null) {
 			throw new IllegalArgumentException(message);
 		}
 	}
 	/**
 	 * 2.根据对象是否为空进行断言。不为空产生异常。
 	 * @param object
 	 */
 	public static void isNull(Object object) {
 		isNull(object, "[Assertion failed] - the object argument must be null");
 	}
 	public static void notNull(Object object, String message) {
 		if (object == null) {
 			throw new IllegalArgumentException(message);
 		}
 	}
 	/**
 	 * 3.根据对象是否为空进行断言。为空产生异常。
 	 * @param object
 	 */
 	public static void notNull(Object object) {
 		notNull(object, "[Assertion failed] - this argument is required; it must not be null");
 	}
 	public static void hasLength(String text, String message) {
 		if (!StringUtils.hasLength(text)) {
 			throw new IllegalArgumentException(message);
 		}
 	}
 	/**
 	 * 4.根据字符串内容是否为空/空值进行断言,为空或空值则产生异常
 	 * @param text
 	 */
 	public static void hasLength(String text) {
 		hasLength(text,
 				"[Assertion failed] - this String argument must have length; it must not be null or empty");
 	}
 	public static void hasText(String text, String message) {
 		if (!StringUtils.hasText(text)) {
 			throw new IllegalArgumentException(message);
 		}
 	}
 	/**
 	 * 5.比4的断言更严格,要求字符串内容不能为空格字符串所组成的。否则产生异常
 	 * @param text
 	 */
 	public static void hasText(String text) {
 		hasText(text,
 				"[Assertion failed] - this String argument must have text; it must not be null, empty, or blank");
 	}
 	public static void doesNotContain(String textToSearch, String substring, String message) {
 		if (StringUtils.hasLength(textToSearch) && StringUtils.hasLength(substring) &&
 				textToSearch.indexOf(substring) != -1) {
 			throw new IllegalArgumentException(message);
 		}
 	}
 	/**
 	 * 6.根据字符串是否包含子字符串进行断言,不包含则产生异常。
 	 * @param textToSearch
 	 * @param substring
 	 */
 	public static void doesNotContain(String textToSearch, String substring) {
 		doesNotContain(textToSearch, substring,
 				"[Assertion failed] - this String argument must not contain the substring [" + substring + "]");
 	}
 	public static void notEmpty(Object[] array, String message) {
 		if (ObjectUtils.isEmpty(array)) {
 			throw new IllegalArgumentException(message);
 		}
 	}
 	/**
 	 * 
 	 * 
 	 * 7.根据数组是否为空或者数组长度为0进行断言,满足前者之一则发生异常。
 	 */
 	public static void notEmpty(Object[] array) {
 		notEmpty(array, "[Assertion failed] - this array must not be empty: it must contain at least 1 element");
 	}
 	public static void noNullElements(Object[] array, String message) {
 		if (array != null) {
 			for (int i = 0; i < array.length; i++) {
 				if (array[i] == null) {
 					throw new IllegalArgumentException(message);
 				}
 			}
 		}
 	}
 	/**
 	 * 8.根据数组是否包含null对象进行断言,满足则发生异常
 	 * @param array
 	 */
 	public static void noNullElements(Object[] array) {
 		noNullElements(array, "[Assertion failed] - this array must not contain any null elements");
 	}
 	public static void notEmpty(Collection collection, String message) {
 		if (CollectionUtils.isEmpty(collection)) {
 			throw new IllegalArgumentException(message);
 		}
 	}
 	/**
 	 * 9.对集合类型是否为空或者元素为0进行断言,满足则发生异常
 	 * @param collection
 	 */
 	public static void notEmpty(Collection collection) {
 		notEmpty(collection,
 				"[Assertion failed] - this collection must not be empty: it must contain at least 1 element");
 	}
 	public static void notEmpty(Map map, String message) {
 		if (CollectionUtils.isEmpty(map)) {
 			throw new IllegalArgumentException(message);
 		}
 	}
 	/**
 	 * 10.对Map类型是否为空或者元素为0进行断言,满足则发生异常
 	 * @param map
 	 */
 	public static void notEmpty(Map map) {
 		notEmpty(map, "[Assertion failed] - this map must not be empty; it must contain at least one entry");
 	}
 	public static void isInstanceOf(Class clazz, Object obj) {
 		isInstanceOf(clazz, obj, "");
 	}
 	/**
 	 * 
 	 *11.根据类型是否所属于指定类型进行断言,不是则发生异常
 	 */
 	public static void isInstanceOf(Class type, Object obj, String message) {
 		notNull(type, "Type to check against must not be null");
 		if (!type.isInstance(obj)) {
 			throw new IllegalArgumentException(message +
 					"Object of class [" + (obj != null ? obj.getClass().getName() : "null") +
 					"] must be an instance of " + type);
 		}
 	}
 	/**
 	 * 12.subType必须可以按类型匹配于 superType,否则将抛出异常;
 	 */
 	public static void isAssignable(Class superType, Class subType) {
 		isAssignable(superType, subType, "");
 	}
 	public static void isAssignable(Class superType, Class subType, String message) {
 		notNull(superType, "Type to check against must not be null");
 		if (subType == null || !superType.isAssignableFrom(subType)) {
 			throw new IllegalArgumentException(message + subType + " is not assignable to " + superType);
 		}
 	}
 	public static void state(boolean expression, String message) {
 		if (!expression) {
 			throw new IllegalStateException(message);
 		}
 	}
 	/**
 	 * 13.根据某个表达式的boolean类型,进行断言。类似1
 	 * @param expression
 	 */
 	public static void state(boolean expression) {
 		state(expression, "[Assertion failed] - this state invariant must be true");
 	}
 
 }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
RedisTemplate 是 Spring Data Redis 提供的一个 Redis 客户端工具类,它封装了 Redis 的常见操作,方便开发人员对 Redis 数据存储进行操作。RedisTemplate 提供了对 Redis 的五种数据结构(字符串、哈希表、列表、集合、有序集合)的支持。通过 RedisTemplate,可以实现对 Redis 的基本操作,如 get、set、incr、decr、hget、hset、lpush、rpush、sadd、zadd 等操作,以及对 Redis 支持的事务、Lua 脚本等高级功能的调用。 使用 RedisTemplate,需要先配置 Redis 连接池和 RedisTemplate 的实例,然后通过注入 RedisTemplate 实例来操作 Redis 数据。下面是一个简单的 RedisTemplate 配置示例: ```java @Configuration public class RedisConfig { @Bean public JedisPool jedisPool() { JedisPoolConfig jedisPoolConfig = new JedisPoolConfig(); jedisPoolConfig.setMaxTotal(100); jedisPoolConfig.setMaxIdle(20); jedisPoolConfig.setMaxWaitMillis(10000); JedisPool jedisPool = new JedisPool(jedisPoolConfig, "127.0.0.1", 6379); return jedisPool; } @Bean public RedisTemplate<String, Object> redisTemplate(JedisConnectionFactory jedisConnectionFactory) { RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>(); redisTemplate.setConnectionFactory(jedisConnectionFactory); redisTemplate.setKeySerializer(new StringRedisSerializer()); redisTemplate.setValueSerializer(new Jackson2JsonRedisSerializer<>(Object.class)); redisTemplate.setHashKeySerializer(new StringRedisSerializer()); redisTemplate.setHashValueSerializer(new Jackson2JsonRedisSerializer<>(Object.class)); return redisTemplate; } } ``` 在上面的配置中,我们通过 JedisPool 创建了 JedisConnectionFactory 实例,然后将其注入到 RedisTemplate 中,并设置了序列化方式。这里我们使用了 Jackson2JsonRedisSerializer 作为序列化方式,可以将对象序列化为 JSON 格式存储到 Redis 中。最后,我们将自定义的 RedisTemplate 注入到需要使用的类中即可。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值