add methods

This commit is contained in:
Looly 2025-09-27 22:14:43 +08:00
parent bdeb33cf24
commit 941c04d478
3 changed files with 57 additions and 9 deletions

View File

@ -145,9 +145,8 @@ public class CombinationAnnotatedElement implements AnnotatedElement, Serializab
// 直接注解 // 直接注解
for (final Annotation annotation : annotations) { for (final Annotation annotation : annotations) {
annotationType = annotation.annotationType(); annotationType = annotation.annotationType();
if (!AnnotationUtil.isMetaAnnotation(annotationType) // issue#I5FQGW@Gitee跳过元注解和已经处理过的注解防止递归调用
// issue#I5FQGW@Gitee跳过元注解和已经处理过的注解防止递归调用 if (!AnnotationUtil.isMetaAnnotation(annotationType) && !declaredAnnotationMap.containsKey(annotationType)) {
&& !declaredAnnotationMap.containsKey(annotationType)) {
if (test(annotation)) { if (test(annotation)) {
declaredAnnotationMap.put(annotationType, annotation); declaredAnnotationMap.put(annotationType, annotation);
} }

View File

@ -236,7 +236,8 @@ public class Assert {
} }
/** /**
* 断言对象是否不为{@code null} 如果为{@code null} 抛出{@link IllegalArgumentException} 异常 * 断言对象是否不为{@code null} 如果为{@code null} 抛出{@link IllegalArgumentException} 异常
* 此方法与{@link java.util.Objects#requireNonNull(Object) }不同在于抛出异常类型不同
* <pre>{@code * <pre>{@code
* Assert.notNull(clazz); * Assert.notNull(clazz);
* }</pre> * }</pre>
@ -245,6 +246,7 @@ public class Assert {
* @param object 被检查对象 * @param object 被检查对象
* @return 非空对象 * @return 非空对象
* @throws IllegalArgumentException if the object is {@code null} * @throws IllegalArgumentException if the object is {@code null}
* @see java.util.Objects#requireNonNull(Object)
*/ */
public static <T> T notNull(final T object) throws IllegalArgumentException { public static <T> T notNull(final T object) throws IllegalArgumentException {
if (null == object) { if (null == object) {
@ -875,11 +877,31 @@ public class Assert {
* @throws IllegalArgumentException 如果size &lt; 0 抛出此异常 * @throws IllegalArgumentException 如果size &lt; 0 抛出此异常
* @throws IndexOutOfBoundsException 如果index &lt; 0或者 index &ge; size 抛出此异常 * @throws IndexOutOfBoundsException 如果index &lt; 0或者 index &ge; size 抛出此异常
* @since 4.1.9 * @since 4.1.9
* @see java.util.Objects#checkIndex(int, int)
*/ */
public static int checkIndex(final int index, final int size) throws IllegalArgumentException, IndexOutOfBoundsException { public static int checkIndex(final int index, final int size) throws IllegalArgumentException, IndexOutOfBoundsException {
return checkIndex(index, size, "[Assertion failed]"); return checkIndex(index, size, "[Assertion failed]");
} }
/**
* 检查下标数组集合字符串是否符合要求下标必须满足
*
* <pre>
* 0 &le; index &lt; size
* </pre>
*
* @param index 下标
* @param size 长度
* @return 检查后的下标
* @throws IllegalArgumentException 如果size &lt; 0 抛出此异常
* @throws IndexOutOfBoundsException 如果index &lt; 0或者 index &ge; size 抛出此异常
* @since 7.0.0
* @see java.util.Objects#checkIndex(long, long)
*/
public static long checkIndex(final long index, final long size) throws IllegalArgumentException, IndexOutOfBoundsException {
return checkIndex(index, size, "[Assertion failed]");
}
/** /**
* 检查下标数组集合字符串是否符合要求下标必须满足 * 检查下标数组集合字符串是否符合要求下标必须满足
* *
@ -902,6 +924,29 @@ public class Assert {
} }
return index; return index;
} }
/**
* 检查下标数组集合字符串是否符合要求下标必须满足
*
* <pre>
* 0 &le; index &lt; size
* </pre>
*
* @param index 下标
* @param size 长度
* @param errorMsgTemplate 异常时的消息模板
* @param params 参数列表
* @return 检查后的下标
* @throws IllegalArgumentException 如果size &lt; 0 抛出此异常
* @throws IndexOutOfBoundsException 如果index &lt; 0或者 index &ge; size 抛出此异常
* @since 7.0.0
*/
public static long checkIndex(final long index, final long size, final String errorMsgTemplate, final Object... params) throws IllegalArgumentException, IndexOutOfBoundsException {
if (index < 0 || index >= size) {
throw new IndexOutOfBoundsException(badIndexMsg(index, size, errorMsgTemplate, params));
}
return index;
}
// endregion // endregion
// region ----- checkBetween // region ----- checkBetween
@ -1178,7 +1223,7 @@ public class Assert {
* @param params 参数列表 * @param params 参数列表
* @return 消息 * @return 消息
*/ */
private static String badIndexMsg(final int index, final int size, final String desc, final Object... params) { private static String badIndexMsg(final long index, final long size, final String desc, final Object... params) {
if (index < 0) { if (index < 0) {
return StrUtil.format("{} ({}) must not be negative", StrUtil.format(desc, params), index); return StrUtil.format("{} ({}) must not be negative", StrUtil.format(desc, params), index);
} else if (size < 0) { } else if (size < 0) {

View File

@ -121,8 +121,7 @@ public class ObjUtil {
if (obj.getClass().isArray()) { if (obj.getClass().isArray()) {
return Array.getLength(obj); return Array.getLength(obj);
} }
if (obj instanceof Enumeration) { if (obj instanceof final Enumeration<?> enumeration) {
final Enumeration<?> enumeration = (Enumeration<?>) obj;
while (enumeration.hasMoreElements()) { while (enumeration.hasMoreElements()) {
count++; count++;
enumeration.nextElement(); enumeration.nextElement();
@ -177,8 +176,7 @@ public class ObjUtil {
} }
return false; return false;
} }
if (obj instanceof Enumeration) { if (obj instanceof final Enumeration<?> enumeration) {
final Enumeration<?> enumeration = (Enumeration<?>) obj;
while (enumeration.hasMoreElements()) { while (enumeration.hasMoreElements()) {
final Object o = enumeration.nextElement(); final Object o = enumeration.nextElement();
if (equals(o, element)) { if (equals(o, element)) {
@ -204,6 +202,7 @@ public class ObjUtil {
* *
* @param obj 对象 * @param obj 对象
* @return 是否为null * @return 是否为null
* @see Objects#isNull(Object)
*/ */
public static boolean isNull(final Object obj) { public static boolean isNull(final Object obj) {
return null == obj; return null == obj;
@ -214,6 +213,7 @@ public class ObjUtil {
* *
* @param obj 对象 * @param obj 对象
* @return 是否不为null * @return 是否不为null
* @see Objects#nonNull(Object)
*/ */
public static boolean isNotNull(final Object obj) { public static boolean isNotNull(final Object obj) {
return null != obj; return null != obj;
@ -294,6 +294,7 @@ public class ObjUtil {
* @param defaultValue 被检查对象为{@code null}返回的默认值可以为{@code null} * @param defaultValue 被检查对象为{@code null}返回的默认值可以为{@code null}
* @return 被检查对象不为 {@code null} 返回原值否则返回默认值 * @return 被检查对象不为 {@code null} 返回原值否则返回默认值
* @since 3.0.7 * @since 3.0.7
* @see Objects#requireNonNullElse(Object, Object)
*/ */
public static <T> T defaultIfNull(final T object, final T defaultValue) { public static <T> T defaultIfNull(final T object, final T defaultValue) {
return isNull(object) ? defaultValue : object; return isNull(object) ? defaultValue : object;
@ -307,6 +308,7 @@ public class ObjUtil {
* @param defaultSupplier 为空时的默认值提供者 * @param defaultSupplier 为空时的默认值提供者
* @return 被检查对象不为 {@code null} 返回原值否则返回 {@link Supplier#get()} 提供的默认值 * @return 被检查对象不为 {@code null} 返回原值否则返回 {@link Supplier#get()} 提供的默认值
* @since 5.4.6 * @since 5.4.6
* @see Objects#requireNonNullElseGet(Object, Supplier)
*/ */
public static <T> T defaultIfNull(final T object, final Supplier<? extends T> defaultSupplier) { public static <T> T defaultIfNull(final T object, final Supplier<? extends T> defaultSupplier) {
return isNull(object) ? defaultSupplier.get() : object; return isNull(object) ? defaultSupplier.get() : object;
@ -371,6 +373,7 @@ public class ObjUtil {
} }
// endregion // endregion
// region ----- clone
/** /**
* <p>克隆对象 * <p>克隆对象
* <ol> * <ol>
@ -427,6 +430,7 @@ public class ObjUtil {
} }
return clone == null ? obj : clone; return clone == null ? obj : clone;
} }
// endregion
/** /**
* 序列化后拷贝流的方式克隆<br> * 序列化后拷贝流的方式克隆<br>