Spring Assert(来自Spring源码)

这个Spring的通用类在代码中使用起来非常简洁!

Spring的工具类,在平时项目中完全可以重新打包使用!

 

public abstract class Assert {

	/**
	 * Assert a boolean expression, throwing
	 * <code>IllegalArgumentException</code> if the test result is
	 * <code>false</code>.
	 * <pre>
	 * Assert.isTrue(i > 0, "The value must be greater than zero");</pre>
	 * @param expression a boolean expression
	 * @param message the exception message to use if the assertion fails
	 * @throws IllegalArgumentException if expression is <code>false</code>
	 */
	public static void isTrue(boolean expression, String message) {
		if (!expression) {
			throw new IllegalArgumentException(message);
		}
	}

	/**
	 * Assert a boolean expression, throwing
	 * <code>IllegalArgumentException</code> if the test result is
	 * <code>false</code>.
	 * <pre>
	 * Assert.isTrue(i > 0);</pre>
	 * @param expression a boolean expression
	 * @throws IllegalArgumentException if expression is <code>false</code>
	 */
	public static void isTrue(boolean expression) {
		isTrue(expression, "[Assertion failed] - this expression must be true");
	}

	/**
	 * Assert that an object is not null.
	 * <pre>
	 * Assert.notNull(clazz, "The class must not be null");</pre>
	 * @param object the object to check
	 * @param message the exception message to use if the assertion fails
	 * @throws IllegalArgumentException if the object is <code>null</code>
	 */
	public static void notNull(Object object, String message) {
		if (object == null) {
			throw new IllegalArgumentException(message);
		}
	}

	/**
	 * Assert that an object is not null.
	 * <pre>
	 * Assert.notNull(clazz);</pre>
	 * @param object the object to check
	 * @throws IllegalArgumentException if the object is <code>null</code>
	 */
	public static void notNull(Object object) {
		notNull(object, "[Assertion failed] - this argument is required; it cannot be null");
	}

	/**
	 * Assert that a string is not empty; that is, it must not be null and not empty.
	 * <pre>
	 * Assert.hasLength(name, "Name must not be empty");</pre>
	 * @param text the string to check
	 * @see StringUtils#hasLength
	 */
	public static void hasLength(String text, String message) {
		if (!StringUtils.hasLength(text)) {
			throw new IllegalArgumentException(message);
		}
	}

	/**
	 * Assert that a string is not empty; that is, it must not be null and not empty.
	 * <pre>
	 * Assert.hasLength(name);</pre>
	 * @param text the string to check
	 * @see StringUtils#hasLength
	 */
	public static void hasLength(String text) {
		hasLength(text, "[Assertion failed] - this string argument must have length; it cannot be null or empty");
	}

	/**
	 * Assert that a string has valid text content; that is, it must not be null
	 * and must contain at least one non-whitespace character.
	 * <pre>
	 * Assert.hasText(name, "Name must not be empty");</pre>
	 * @param text the string to check
	 * @see StringUtils#hasText
	 */
	public static void hasText(String text, String message) {
		if (!StringUtils.hasText(text)) {
			throw new IllegalArgumentException(message);
		}
	}

	/**
	 * Assert that a string has valid text content; that is, it must not be null
	 * and must contain at least one non-whitespace character.
	 * <pre>
	 * Assert.hasText(name, "Name must not be empty");</pre>
	 * @param text the string to check
	 * @see StringUtils#hasText
	 */
	public static void hasText(String text) {
		hasText(text,
				"[Assertion failed] - this string argument must have text; it cannot be null, empty, or blank");
	}

	/**
	 * Assert that an array has elements; that is, it must not be
	 * <code>null</code> and must have at least one element.
	 * <pre>
	 * Assert.notEmpty(array, "The array must have elements");</pre>
	 * @param array the array to check
	 * @param message the exception message to use if the assertion fails
	 * @throws IllegalArgumentException if the object array is <code>null</code> or has no elements
	 */
	public static void notEmpty(Object[] array, String message) {
		if (array == null || array.length == 0) {
			throw new IllegalArgumentException(message);
		}
	}

	/**
	 * Assert that an array has elements; that is, it must not be
	 * <code>null</code> and must have at least one element.
	 * <pre>
	 * Assert.notEmpty(array);</pre>
	 * @param array the array to check
	 * @throws IllegalArgumentException if the object array is <code>null</code> or has no elements
	 */
	public static void notEmpty(Object[] array) {
		notEmpty(array, "[Assertion failed] - this array must not be empty: it must contain at least 1 element");
	}

	/**
	 * Assert that a collection has elements; that is, it must not be
	 * <code>null</code> and must have at least one element.
	 * <pre>
	 * Assert.notEmpty(collection, "Collection must have elements");</pre>
	 * @param collection the collection to check
	 * @param message the exception message to use if the assertion fails
	 * @throws IllegalArgumentException if the collection is <code>null</code> or has no elements
	 */
	public static void notEmpty(Collection<?> collection, String message) {
		if (collection == null || collection.isEmpty()) {
			throw new IllegalArgumentException(message);
		}
	}

	/**
	 * Assert that a collection has elements; that is, it must not be
	 * <code>null</code> and must have at least one element.
	 * <pre>
	 * Assert.notEmpty(collection, "Collection must have elements");</pre>
	 * @param collection the collection to check
	 * @throws IllegalArgumentException if the collection is <code>null</code> or has no elements
	 */
	public static void notEmpty(Collection<?> collection) {
		notEmpty(collection,
				"[Assertion failed] - this collection must not be empty: it must contain at least 1 element");
	}

	/**
	 * Assert that a Map has entries; that is, it must not be <code>null</code>
	 * and must have at least one entry.
	 * <pre>
	 * Assert.notEmpty(map, "Map must have entries");</pre>
	 * @param map the map to check
	 * @param message the exception message to use if the assertion fails
	 * @throws IllegalArgumentException if the map is <code>null</code> or has no entries
	 */
	public static void notEmpty(Map<?,?> map, String message) {
		if (map == null || map.isEmpty()) {
			throw new IllegalArgumentException(message);
		}
	}

	/**
	 * Assert that a Map has entries; that is, it must not be <code>null</code>
	 * and must have at least one entry.
	 * <pre>
	 * Assert.notEmpty(map);</pre>
	 * @param map the map to check
	 * @throws IllegalArgumentException if the map is <code>null</code> or has no entries
	 */
	public static void notEmpty(Map<?,?> map) {
		notEmpty(map, "[Assertion failed] - this map must not be empty; it must contain at least one entry");
	}

	/**
	 * Assert that the provided object is an instance of the provided class.
	 * <pre>
	 * Assert.instanceOf(Foo.class, foo);</pre>
	 * @param clazz the required class
	 * @param obj the object to check
	 * @throws IllegalArgumentException if the object is not an instance of clazz
	 * @see Class#isInstance
	 */
	public static void isInstanceOf(Class<?> clazz, Object obj) {
		isInstanceOf(clazz, obj, "");
	}

	/**
	 * Assert that the provided object is an instance of the provided class.
	 * <pre>
	 * Assert.instanceOf(Foo.class, foo);</pre>
	 * @param clazz the required class
	 * @param obj the object to check
	 * @param message a message which will be prepended to the message produced by
	 * the function itself, and which may be used to provide context. It should
	 * normally end in a ":" or ". " so that the function generate message looks
	 * ok when prepended to it.
	 * @throws IllegalArgumentException if the object is not an instance of clazz
	 * @see Class#isInstance
	 */
	public static void isInstanceOf(Class<?> clazz, Object obj, String message) {
		Assert.notNull(clazz, "The clazz to perform the instanceof assertion cannot be null");
		Assert.isTrue(clazz.isInstance(obj), message +
				"Object of class '" + (obj != null ? obj.getClass().getName() : "[null]") +
				"' must be an instance of '" + clazz.getName() + "'");
	}


	/**
	 * Assert a boolean expression, throwing <code>IllegalStateException</code>
	 * if the test result is <code>false</code>. Call isTrue if you wish to
	 * throw IllegalArgumentException on an assertion failure.
	 * <pre>
	 * Assert.state(id == null, "The id property must not already be initialized");</pre>
	 * @param expression a boolean expression
	 * @param message the exception message to use if the assertion fails
	 * @throws IllegalStateException if expression is <code>false</code>
	 */
	public static void state(boolean expression, String message) {
		if (!expression) {
			throw new IllegalStateException(message);
		}
	}

	/**
	 * Assert a boolean expression, throwing <code>IllegalStateException</code>
	 * if the test result is <code>false</code>. Call isTrue if you wish to
	 * throw IllegalArgumentException on an assertion failure.
	 * <pre>
	 * Assert.state(id == null);</pre>
	 * @param expression a boolean expression
	 * @throws IllegalStateException if expression is <code>false</code>
	 */
	public static void state(boolean expression) {
		state(expression, "[Assertion failed] - this state invariant must be true");
	}

}
【水下小目标检测】SWIPENet与样本重加权:应对模糊与噪声的深度学习策略 本文深入探讨了水下小目标检测的挑战与解决方案,重点介绍了SWIPENet网络架构与IMA样本重加权算法。通过空洞卷积模块、特征金字塔重构和多尺度预测机制,SWIPENet有效提升了模糊目标的检测精度。结合Invert Multi-Class Adaboost算法,系统性地解决了水下环境中的样本不均衡问题,在URPC数据集上实现了48.8%的mAP,特别适合海洋生物检测等应用场景。 阅读详情

相关推荐

机械臂TCP标定实战:从四点法到六点法的精度跃迁

本文详细解析机械臂TCP标定技术,从基础的四点法到高精度的六点法实现精度跃迁。通过工具坐标系与基坐标系的转换原理,结合实战案例展示六点法如何将装配合格率提升至99.5%,并分享环境控制、操作技巧等提升标定精度的关键要素。

weixin_29038345的博客 896

Spring-core源码 -- Assert断言类

Assert断言类,这是一个很常见的类,我们调试的时候验证方法参数使用。一个个方法开始看。 public static void state(boolean expression, String message) { if (!expression) { throw new IllegalStateException(message); } } 判断expression的bo...

游语 558

跨境TRS投资操作指南与系统解决方案

3 点击【申请 TRS 账户】,查看风险披露,录入账户参数、确定录入的参数信息,进行身份验证,输入验证码,TRS 账户开户申请已提交,后台对已提交的 TRS 开户申请进行审核,审核成功后,短信邮箱提醒,进行登录;2.点击【申请TRS账户】,查看风险披露,录入账户参数、确定录入的参数信息,进行身份验证,输入验证码,TRS账户开户申请已提交,后台对已提交的TRS开户申请进行审核,审核成功后,短信邮箱提醒,进行登录;4)更广泛的投资选择:相比A股通,TRS可以交易更多的A股标的,提供了更广泛的投资选择。

2401_85487119的博客 2852

Spring-技术专题-断言Assert

首先,Web 应用在接受表单提交的数据后都需要对其进行合法性检查,如果表单数据不合法,请求将被驳回。类似的,当我们在编写类的方法时,也常常需要对方法入参进行合 法性检查,如果入参不符合要求,方法将通过抛出异常的方式拒绝后续处理。举一个例子:有一个根据文件名获取输入流的方法:InputStream getData(String file),为了使方法能够成功执行,必须保证 file 入参不能为 null 或空白字符,否则根本无须进行后继的处理。这时方法的编写者通常会在方法体的最前面编写一段对入参...

世界上并没有完美的程序,但是我们并不因此而沮丧,因为写程序就是一个不断追求完美的过程。 2063

Voronoi算法构建多晶

孪晶多晶 采用孪晶的原子链作为种子通过voronoi算法可以得到孪晶多晶结构。 孪晶种子: txt文件不需要特殊设置 box 150 150 150 random 8 得到孪晶多晶: 通过改变孪晶种子的长度可以得到不同孪晶片层厚度的多晶。 准二维柱晶 所有晶粒在某一维度的晶向一致,晶粒仅在平面内具有旋转关系,所有晶界为旋转对称晶界,这种结构的多晶其位错滑移在某些方向受限,但是能极大的提高晶粒...

yamatachi的博客 5280

别再被网站识别成机器人了!用Python的undetected_chromedriver+Selenium实现无痕自动化

本文详细介绍了如何利用Python的undetected_chromedriver和Selenium实现无痕自动化,突破现代网站的反爬系统。通过修正浏览器指纹、模拟人类操作行为,并结合实战案例,帮助开发者构建难以被识别的自动化脚本,适用于电商监控等场景。

weixin_30367169的博客 252

spring Assert详解

一.什么是Assert  Assert 翻译为中文为“断言”,使用过 JUnit 的读者都熟知这个概念,它断定某一个实际的运行值和预期想一样,否则就抛出异常。Spring 对方法入参的检测借用了这个概念,其提供的 Assert 类拥有众多按规则对方法入参进行断言的方法,可以满足大部分方法入参检测的要求。 每个Assert的方法可以与java assert表达式进行比较。java assert表...

xxdw1992的博客 4763

Pytorch入门教程

Pytorch学习总结:1.张量Tensor 张量是一种特殊的数据结构,与数组和矩阵非常相似。在PyTorch中,我们使用张量对模型的输入和输出以及模型的参数进行编码。 张量类似于NumPy的ndarray,除了张量可以在 GPU 或其他硬件加速器上运行。事实上,张量和NumPy数组通常可以共享相同的底层内存,从而无需复制数据。...

专注大数据与人工智能技术分享,欢迎私信加群互相学习! 4万+

java断言--02--spring Assert类

Spring Assert Spring Assert与其他框架的名称类似,如JUnit或其他框架,但其实没有任何共同之处。 Spring Assert不是为了测试,而是为了调试。 Spring Assert的方法有一些特点: 都是static方法 抛出IllegalArgumentException 或 IllegalStateException异常 第一个参数通常是需验证的对象或逻辑条件 最后参数通常是异常消息,用于验证失败时显示 消息可以作为String参数或Supplier 参数传输 S

时光里的博客 796

OpenClaw 技术架构解析-网关层(上)

OpenClaw 网关层是架构核心中枢,承接Channel层消息,串联消息路由器、会话状态管理器等五大组件,负责消息分发、会话管理、状态统筹、上下文维护等关键职责,是连接Channel层与AI执行引擎的核心桥梁,保障多渠道交互稳定、有序运行。

wend的博客 713

2026从0到1!C#工控机开发实战指南:新手一周就能跑通的完整项目

这篇文章摘要如下: 工业自动化领域工控机开发入门指南,针对零基础开发者提供从环境搭建到项目实战的全流程指导。文章首先解答工控机开发的3个核心问题,比较工控机与普通PC的区别,推荐使用C#作为开发语言。随后详细讲解开发环境配置、四大核心技术栈(通信协议、数据库、UI框架、多线程),并重点演示Modbus TCP通信的实现。最后通过一个完整的温度监控系统项目实战,展示如何用WPF构建实时数据采集界面,包括Modbus通信类编写、数据采集服务实现等关键代码片段。全文采用图解+代码的方式,帮助开发者快速掌握工控系统

这里只有干货:从 Modbus 通信到 MES 对接,从 YOLO 训练到工控机部署,带你搞定工业软件开发全流程。 702

spring-util-对参数校验的支持 org.springframework.util.Assert

org.springframework.util.Assert Assert.notNull(object, message);实现如下:内网穿透工具分享 https://bob.kim/articles/2018/03/26/1522029456103.htmlngrok服务可以分配给你一个域名或自己的域名让你本地的web项目提供给外网访问,特别适合向别人展示你本机的web demo 以及调试一...

lyb3290的博客 3018

C++ PCL点云处理实战专栏

工业级点云处理技术学习资源,涵盖电力、建筑、隧道等领域的实战案例。内容分为三阶段:代码原理(算法认知)、动态库(项目验证)和完整代码(定制开发)。

weixin_45196569的博客 1117

Spring Assert 详解及详细源码展示

Spring的Assert工具类位于org.springframework.util包,提供静态方法用于验证程序状态、参数合法性和业务逻辑条件。其核心功能包括对象/集合非空检查、字符串校验、数值范围验证、布尔条件判断以及异常断言。源码分析显示其方法简洁高效,如assertNotNull()直接检查null值,assertNotEmpty()验证集合内容,assertNotBlank()处理空白字符串。典型应用场景包含服务层参数校验(如验证非空用户对象)、DAO层数据完整性检查(如确保查询结果存在)及单元测试

csdn_tom_168的博客 1277

计算机控制技术及应用

《计算机控制技术及应用》通过总结作者的科研成果和吸收国内外的先进理论、方法和技术,以计算机控制系统的网络化、开放化、智能化和集成化发展趋势为主线索,主要介绍计算机控制系统的基本概念和计算机控制系统的典型形式,计算机控制系统的数学描述与基本分析方法,以及数字PID控制器、最少拍控制系统、达林算法、状态变量设计法等设计方法及其各种改进算法。重点突出计算机控制系统输入输出通道接口的设计方法和典型控制系统的设计案例与实现方法等。

C# WinForm 基于DirectShow.dll编写的程序,可边预览边保存视频,音频和图片

基于微软的DirectShow.dll的二次开发1.自动识别连接的摄像头跟麦克风且可以对其进行选择(默认选中第一个)2.可录制wmv和avi两种格式的视频并且都带有音频,avi可以录制的分辨率和帧数进行选择,wmv默认进行了淸晰度优化如不喜欢可去除代码中有相关提示3.可在预览点开之后进行拍照,也可边录制视频边拍照4.添加了视频和音频的压缩功能(酌情使用)这次二次开发发现C#对DirectShow的相关信息太少,这次做完也不敢藏私共享出来,还望大家指正

java assertnotnull_spring源码之—Assert.notNull

org.springframework.util.Assert Assert翻译为中文为”断言”.用过JUNIT的应该都知道这个概念了. 就是断定某一个实际的值就为自己预期想得到的,如果不一样就抛出异常. Assert经常用于: 1.判断METHOD的参数是否属于正常值. 2.JUNIT中使用.我发现SPRING1.2.6里面有BUG 请看: org.springframework.core.io...

weixin_32127359的博客 475

大中型超市收银系统(可直接运行)

光步前台收银系统系统功能:商品销售、商品退货、挂单、收银员权限、会员销售、单品取消、整单取消、商品查询、脱机销售、脱机数据上传,基本资料下载,开钱箱、小票打印、收款结算,支持多币种付款方式(现金、储值卡、礼券、微信等),支持多种促销。运行环境:.net 4.0登陆信息用户名:1001 密码:123456内置商品信息,商品编码可输入00001000 - 00010000,测试效果相关功能键F5 结算 F9挂单 F11商品查询 F8退货 Insert单品取消 Del整单取消 F12开钱箱F2 会员 Ctrl+Z退出系统 Home 锁屏 相关源码待公开

[Spring源码阅读]通过Assert校验参数

在Spring源码中发现了另一种相对更优雅的写法,通过Assert校验参数。该工具类位于包: org.springframework.util。

小楼夜听雨的博客 737

ET2012新版免狗免安装绿色软件

ET2012新版免狗免安祖昂绿色软件。,新界面,新视觉,

spring源码之—Assert.notNull

org.springframework.util.Assert Assert翻译为中文为"断言".用过JUNIT的应该都知道这个概念了. 就是断定某一个实际的值就为自己预期想得到的,如果不一样就抛出异常. Assert经常用于: 1.判断METHOD的参数是否属于正常值. 2.JUNIT中使用. 我发现SPRING1.2.6里面有BUG 请看: org.springframewor...

WGH100817的博客 314

读spring源码之—Assert.notNull

org.springframework.util.Assert Assert翻译为中文为"断言".用过JUNIT的应该都知道这个概念了. 就是断定某一个实际的值就为自己预期想得到的,如果不一样就抛出异常. Assert经常用于: 1.判断METHOD的参数是否属于正常值. 2.JUNIT中使用. 我发现SPRING1.2.6里面有BUG 请看: org.springfram

赵鹏飞的博客 581

spring源码之—Assert.notNull()

org.springframework.util.Assert Assert翻译为中文为"断言".用过JUNIT的应该都知道这个概念了. 就是断定某一个实际的值就为自己预期想得到的,如果不一样就抛出异常. Assert经常用于: 1.判断METHOD的参数是否属于正常值. 2.JUNIT中使用. 我发现SPRING1.2.6里面有BUG 请看:

u013456370的专栏 1万+

spring源码之—Assert.notNull-断言

org.springframework.util.Assert Assert翻译为中文为"断言".用过JUNIT的应该都知道这个概念了. 就是断定某一个实际的值就为自己预期想得到的,如果不一样就抛出异常. Assert经常用于: 1.判断METHOD的参数是否属于正常值. 2.JUNIT中使用.   我发现SPRING1.2.6里面有BUG 请看: org.springfra

qq_32571823的博客 2521
上一篇: 路径字符替换(来自Spring源码)
下一篇: 堆和堆栈的区别
gufengjava
博客等级 码龄16年 1粉丝 59原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值