7 Commits
Author SHA1 Message Date
Timi 02a13377bc v0.0.7
CI/CD / build-deploy (pull_request) Successful in 14s
2026-08-11 23:53:52 +08:00
Timi 8e1a1a1673 fix defaultIfNull 2026-08-07 17:59:30 +08:00
Timi 99fb2f62f2 add allEmpty 2026-08-05 23:07:28 +08:00
Timi e2bba01daf upper offset/limit from timi-spring 2026-08-05 23:07:16 +08:00
Timi c99df7370c v0.0.6
CI/CD / build-deploy (pull_request) Successful in 10s
2026-08-01 01:09:49 +08:00
Timi d643f8f0c8 v0.0.5
CI/CD / build-deploy (pull_request) Successful in 12s
2026-06-30 15:25:03 +08:00
Timi 80fe2f8cb9 v0.0.4
CI/CD / build-deploy (pull_request) Successful in 13s
2026-05-07 14:34:46 +08:00
9 changed files with 219 additions and 306 deletions
+3 -3
View File
@@ -6,7 +6,7 @@
<groupId>com.imyeyu.java</groupId> <groupId>com.imyeyu.java</groupId>
<artifactId>timi-java</artifactId> <artifactId>timi-java</artifactId>
<version>0.0.3</version> <version>0.0.7</version>
<packaging>jar</packaging> <packaging>jar</packaging>
<properties> <properties>
@@ -50,7 +50,7 @@
<dependency> <dependency>
<groupId>org.projectlombok</groupId> <groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId> <artifactId>lombok</artifactId>
<version>1.18.36</version> <version>1.18.46</version>
</dependency> </dependency>
</dependencies> </dependencies>
</plugin> </plugin>
@@ -92,7 +92,7 @@
<dependency> <dependency>
<groupId>org.projectlombok</groupId> <groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId> <artifactId>lombok</artifactId>
<version>1.18.40</version> <version>1.18.46</version>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.junit.jupiter</groupId> <groupId>org.junit.jupiter</groupId>
+34 -2
View File
@@ -5,7 +5,6 @@ import java.io.StringWriter;
import java.util.Collection; import java.util.Collection;
import java.util.Collections; import java.util.Collections;
import java.util.Map; import java.util.Map;
import java.util.Objects;
/** /**
* 通用工具 * 通用工具
@@ -72,6 +71,39 @@ public interface TimiJava {
return !isEmpty(object); return !isEmpty(object);
} }
static boolean hasEmpty(Object... objects) {
for (Object object : objects) {
if (isEmpty(object)) {
return true;
}
}
return false;
}
static boolean allEmpty(Object... objects) {
for (Object object : objects) {
if (isNotEmpty(object)) {
return false;
}
}
return true;
}
static boolean allNotEmpty(Object... objects) {
for (Object object : objects) {
if (isEmpty(object)) {
return false;
}
}
return true;
}
static <T> T nullIfEmpty(T object) {
if (isEmpty(object)) {
return null;
}
return object;
}
/** /**
* 为空时取默认值 * 为空时取默认值
* *
@@ -81,7 +113,7 @@ public interface TimiJava {
* @param <T> 对象类型 * @param <T> 对象类型
*/ */
static <T> T defaultIfNull(T obj, T defaultObj) { static <T> T defaultIfNull(T obj, T defaultObj) {
return Objects.requireNonNullElse(obj, defaultObj); return obj != null ? obj : defaultObj;
} }
/** /**
@@ -30,4 +30,22 @@ public class BasePage {
public void next() { public void next() {
index++; index++;
} }
/**
* 获取偏移量
*
* @return 偏移量
*/
public long getOffset() {
return (long) index * size;
}
/**
* 获取限制数量
*
* @return 限制数量
*/
public long getLimit() {
return size;
}
} }
@@ -1,81 +0,0 @@
package com.imyeyu.java.bean;
import java.util.Map;
/**
* 多语言消息接口
*
* @author 夜雨
* @since 2024-04-01 10:28
*/
public interface LanguageMsgMapping<T> {
/**
* 设置消息
*
* @param msg 消息
* @return 原对象
*/
T msg(String msg);
/**
* 设置消息多语言键
*
* @param msgKey 键
* @return 原对象
*/
T msgKey(String msgKey);
/**
* 设置消息多语言键并附加插值参数
*
* @param msgKey 键
* @param msgArgs 插值参数
* @return 原对象
*/
T msgKey(String msgKey, Map<String, Object> msgArgs);
/**
* 设置消息
*
* @param msg 消息
*/
void setMsg(String msg);
/**
* 获取消息
*
* @return 消息
*/
String getMsg();
/**
* 设置消息多语言键
*
* @param msgKey 键
*/
void setMsgKey(String msgKey);
/**
* 设置消息多语言键并附加插值参数列表
*
* @param msgKey 键
* @param msgArgs 插值参数
*/
void setMsgKey(String msgKey, Map<String, Object> msgArgs);
/**
* 获取消息多语言键
*
* @return 键
*/
String getMsgKey();
/**
* 获取附加插值参数
*
* @return 附加插值参数列表
*/
Map<String, Object> getMsgArgs();
}
@@ -109,7 +109,7 @@ public enum TimiCode {
* @return 返回对象 * @return 返回对象
*/ */
public TimiResponse<?> toResponse() { public TimiResponse<?> toResponse() {
return new TimiResponse<>(this).msg(toString()); return new TimiResponse<>(this).message(toString());
} }
/** /**
@@ -1,6 +1,5 @@
package com.imyeyu.java.bean.timi; package com.imyeyu.java.bean.timi;
import com.imyeyu.java.bean.LanguageMsgMapping;
import lombok.Getter; import lombok.Getter;
import java.util.Map; import java.util.Map;
@@ -11,81 +10,53 @@ import java.util.Map;
* @author 夜雨 * @author 夜雨
* @since 2023-04-27 15:55 * @since 2023-04-27 15:55
*/ */
public class TimiError extends AssertionError implements LanguageMsgMapping<TimiError> { @Getter
public class TimiError extends AssertionError {
/** 代码 */ /** 代码 */
@Getter
protected final TimiCode code; protected final TimiCode code;
protected transient String msgKey; /** 消息代码 */
protected final String messageCode;
protected transient Map<String, Object> msgArgs; /** 消息插值参数 */
protected final transient Map<String, Object> messageArgs;
/** @param code 代码 */ /** @param code 代码 */
public TimiError(TimiCode code) { public TimiError(TimiCode code) {
this.code = code; this(code, code.toString());
} }
/** /**
* 构造器 * 构造器
* *
* @param code 代码 * @param code 代码
* @param msg 消息 * @param messageCode 消息代码
*/ */
public TimiError(TimiCode code, String msg) { public TimiError(TimiCode code, String messageCode) {
super(msg); this(code, messageCode, null);
}
/**
* 构造器
*
* @param code 代码
* @param messageCode 消息代码
* @param messageArgs 消息插值参数
*/
public TimiError(TimiCode code, String messageCode, Map<String, Object> messageArgs) {
super(messageCode);
this.code = code; this.code = code;
this.messageCode = messageCode;
this.messageArgs = messageArgs;
} }
/**
* 转为通用响应
*
* @return 响应
*/
public TimiResponse<?> toResponse() { public TimiResponse<?> toResponse() {
return new TimiResponse<>(code).msg(getMessage()).msgKey(msgKey, msgArgs); return new TimiResponse<>(code).messageCode(messageCode, messageArgs);
}
@Override
public TimiError msg(String msg) {
throw new UnsupportedOperationException("unsupported to change message");
}
@Override
public TimiError msgKey(String msgKey) {
setMsgKey(msgKey);
return this;
}
@Override
public TimiError msgKey(String msgKey, Map<String, Object> msgArgs) {
setMsgKey(msgKey, msgArgs);
return this;
}
@Override
public void setMsg(String msg) {
throw new UnsupportedOperationException("unsupported to change message");
}
@Override
public String getMsg() {
return getMessage();
}
@Override
public void setMsgKey(String msgKey) {
this.msgKey = msgKey;
}
@Override
public String getMsgKey() {
return msgKey;
}
@Override
public void setMsgKey(String msgKey, Map<String, Object> msgArgs) {
this.msgKey = msgKey;
this.msgArgs = msgArgs;
}
@Override
public Map<String, Object> getMsgArgs() {
return msgArgs;
} }
} }
@@ -2,159 +2,133 @@ package com.imyeyu.java.bean.timi;
import com.imyeyu.java.TimiJava; import com.imyeyu.java.TimiJava;
import com.imyeyu.java.bean.CallbackReturn; import com.imyeyu.java.bean.CallbackReturn;
import com.imyeyu.java.bean.LanguageMsgMapping;
import lombok.Getter; import lombok.Getter;
import java.util.Map; import java.util.Map;
/** /**
* 通用运行时异常,附加通用代码 * 通用运行时异常,附加通用代码和消息代码
* *
* @author 夜雨 * @author 夜雨
* @since 2021-05-20 15:18 * @since 2021-05-20 15:18
*/ */
public class TimiException extends RuntimeException implements LanguageMsgMapping<TimiException> { @Getter
public class TimiException extends RuntimeException {
/** 代码 */ /** 代码 */
@Getter
protected final TimiCode code; protected final TimiCode code;
protected transient String msgKey; /** 消息代码 */
protected final String messageCode;
protected transient Map<String, Object> msgArgs; /** 消息插值参数 */
protected final transient Map<String, Object> messageArgs;
/** @param code 代码 */ /** @param code 代码 */
public TimiException(TimiCode code) { public TimiException(TimiCode code) {
this.code = code; this(code, code.toString());
} }
/** /**
* 构造器 * 构造器
* *
* @param code 代码 * @param code 代码
* @param msg 消息 * @param messageCode 消息代码
*/ */
public TimiException(TimiCode code, String msg) { public TimiException(TimiCode code, String messageCode) {
super(msg); this(code, messageCode, (Map<String, Object>) null);
this.code = code;
} }
/** /**
* 构造器 * 构造器
* *
* @param code 代码 * @param code 代码
* @param msg 消息 * @param messageCode 消息代码
* @param messageArgs 消息插值参数
*/ */
public TimiException(TimiCode code, String msg, Throwable e) { public TimiException(TimiCode code, String messageCode, Map<String, Object> messageArgs) {
super(msg, e); super(messageCode);
this.code = code; this.code = code;
this.messageCode = messageCode;
this.messageArgs = messageArgs;
} }
/**
* 构造器
*
* @param code 代码
* @param messageCode 消息代码
* @param e 异常
*/
public TimiException(TimiCode code, String messageCode, Throwable e) {
super(messageCode, e);
this.code = code;
this.messageCode = messageCode;
this.messageArgs = null;
}
/**
* 转为通用响应
*
* @return 响应
*/
public TimiResponse<?> toResponse() { public TimiResponse<?> toResponse() {
return new TimiResponse<>(code).msg(getMessage()).msgKey(msgKey, msgArgs); return new TimiResponse<>(code).messageCode(messageCode, messageArgs);
} }
@Override public static <T> T required(T t, String messageCode) {
public TimiException msg(String msg) {
throw new UnsupportedOperationException("unsupported to change message");
}
@Override
public TimiException msgKey(String msgKey) {
setMsgKey(msgKey);
return this;
}
@Override
public TimiException msgKey(String msgKey, Map<String, Object> msgArgs) {
setMsgKey(msgKey, msgArgs);
return this;
}
@Override
public void setMsg(String msg) {
throw new UnsupportedOperationException("unsupported to change message");
}
@Override
public String getMsg() {
return getMessage();
}
@Override
public void setMsgKey(String msgKey) {
this.msgKey = msgKey;
}
@Override
public void setMsgKey(String msgKey, Map<String, Object> msgArgs) {
this.msgKey = msgKey;
this.msgArgs = msgArgs;
}
@Override
public String getMsgKey() {
return msgKey;
}
@Override
public Map<String, Object> getMsgArgs() {
return msgArgs;
}
public static <T> T required(T t, String message) {
if (TimiJava.isEmpty(t)) { if (TimiJava.isEmpty(t)) {
throw new TimiException(TimiCode.ARG_MISS, message); throw new TimiException(TimiCode.ARG_MISS, messageCode);
} }
return t; return t;
} }
public static <T> T required(T t, String message, String msgKey) { public static <T> T required(T t, String messageCode, Map<String, Object> messageArgs) {
if (TimiJava.isEmpty(t)) { if (TimiJava.isEmpty(t)) {
throw new TimiException(TimiCode.ARG_MISS, message).msgKey(msgKey); throw new TimiException(TimiCode.ARG_MISS, messageCode, messageArgs);
} }
return t; return t;
} }
public static <T> void requiredNull(T t, String message) throws TimiException { public static <T> void requiredNull(T t, String messageCode) throws TimiException {
if (t != null) { if (t != null) {
throw new TimiException(TimiCode.ERROR, message); throw new TimiException(TimiCode.ERROR, messageCode);
} }
} }
public static <T> void requiredNull(T t, String message, String msgKey) throws TimiException { public static <T> void requiredNull(T t, String messageCode, Map<String, Object> messageArgs) throws TimiException {
if (t != null) { if (t != null) {
throw new TimiException(TimiCode.ERROR, message).msgKey(msgKey); throw new TimiException(TimiCode.ERROR, messageCode, messageArgs);
} }
} }
public static void requiredTrue(boolean bool, String message) throws TimiException { public static void requiredTrue(boolean bool, String messageCode) throws TimiException {
if (bool) { if (bool) {
return; return;
} }
throw new TimiException(TimiCode.ERROR, message); throw new TimiException(TimiCode.ERROR, messageCode);
} }
public static void requiredTrue(boolean bool, String message, String msgKey) throws TimiException { public static void requiredTrue(boolean bool, String messageCode, Map<String, Object> messageArgs) throws TimiException {
if (bool) { if (bool) {
return; return;
} }
throw new TimiException(TimiCode.ERROR, message).msgKey(msgKey); throw new TimiException(TimiCode.ERROR, messageCode, messageArgs);
} }
public static void requiredTrue(CallbackReturn<Boolean> callback, String message) throws TimiException { public static void requiredTrue(CallbackReturn<Boolean> callback, String messageCode) throws TimiException {
TimiException.required(callback, "not found callback"); TimiException.required(callback, "not found callback");
if (callback.handler()) { if (callback.handler()) {
return; return;
} }
throw new TimiException(TimiCode.ERROR, message); throw new TimiException(TimiCode.ERROR, messageCode);
} }
public static void requiredTrue(CallbackReturn<Boolean> callback, String message, String msgKey) throws TimiException { public static void requiredTrue(CallbackReturn<Boolean> callback, String messageCode, Map<String, Object> messageArgs) throws TimiException {
TimiException.required(callback, "not found callback"); TimiException.required(callback, "not found callback");
if (callback.handler()) { if (callback.handler()) {
return; return;
} }
throw new TimiException(TimiCode.ERROR, message).msgKey(msgKey); throw new TimiException(TimiCode.ERROR, messageCode, messageArgs);
} }
} }
@@ -1,6 +1,5 @@
package com.imyeyu.java.bean.timi; package com.imyeyu.java.bean.timi;
import com.imyeyu.java.bean.LanguageMsgMapping;
import lombok.Getter; import lombok.Getter;
import lombok.NoArgsConstructor; import lombok.NoArgsConstructor;
import lombok.Setter; import lombok.Setter;
@@ -11,23 +10,33 @@ import java.util.Map;
/** /**
* 通用接口返回对象 * 通用接口返回对象
* *
* @param <T> 数据类型
*
* @author 夜雨 * @author 夜雨
* @since 2021-07-01 20:18 * @since 2021-07-01 20:18
*/ */
@NoArgsConstructor @NoArgsConstructor
public class TimiResponse<T> implements Serializable, LanguageMsgMapping<TimiResponse<T>> { public class TimiResponse<T> implements Serializable {
/** 响应代码 */ /** 响应代码 */
@Setter @Setter
@Getter @Getter
protected Integer code; protected Integer code;
/** 消息 */ /** 响应消息 */
protected String msg; @Getter
@Setter
protected String message;
protected transient String msgKey; /** 响应消息代码 */
@Getter
@Setter
protected String messageCode;
protected transient Map<String, Object> msgArgs; /** 消息插值参数 */
@Getter
@Setter
protected transient Map<String, Object> messageArgs;
/** 数据体 */ /** 数据体 */
@Setter @Setter
@@ -58,12 +67,12 @@ public class TimiResponse<T> implements Serializable, LanguageMsgMapping<TimiRes
* *
* @param code 代码 * @param code 代码
* @param data 数据体 * @param data 数据体
* @param msg 消息 * @param message 响应消息
*/ */
public TimiResponse(TimiCode code, T data, String msg) { public TimiResponse(TimiCode code, T data, String message) {
this.code = code.getValue(); this.code = code.getValue();
this.data = data; this.data = data;
this.msg = msg; this.message = message;
} }
/** @return true 为成功,code 为 20000 段 */ /** @return true 为成功,code 为 20000 段 */
@@ -86,71 +95,41 @@ public class TimiResponse<T> implements Serializable, LanguageMsgMapping<TimiRes
if (code == null) { if (code == null) {
throw new NullPointerException("unknow timi code: " + this.code); throw new NullPointerException("unknow timi code: " + this.code);
} }
return code.toException(msg); return new TimiException(code, messageCode == null ? message : messageCode, messageArgs);
} }
/** /**
* 追加消息(避免和泛型字符串冲突) * 设置响应消息
* *
* @param msg 消息 * @param message 响应消息
* @return 本对象 * @return 本对象
*/ */
@Override public TimiResponse<T> message(String message) {
public TimiResponse<T> msg(String msg) { this.message = message;
this.msg = msg;
return this; return this;
} }
/** /**
* 获取消息 * 设置响应消息代码
* *
* @return 消息 * @param messageCode 响应消息代码
* @return 本对象
*/ */
@Override public TimiResponse<T> messageCode(String messageCode) {
public String getMsg() { this.messageCode = messageCode;
return msg; return this;
} }
/** /**
* 设置消息 * 设置响应消息代码和插值参数
* *
* @param msg 消息 * @param messageCode 响应消息代码
* @param messageArgs 插值参数
* @return 本对象
*/ */
@Override public TimiResponse<T> messageCode(String messageCode, Map<String, Object> messageArgs) {
public void setMsg(String msg) { this.messageCode = messageCode;
this.msg = msg; this.messageArgs = messageArgs;
}
@Override
public TimiResponse<T> msgKey(String msgKey) {
this.msgKey = msgKey;
return this; return this;
} }
@Override
public TimiResponse<T> msgKey(String msgKey, Map<String, Object> msgArgs) {
setMsgKey(msgKey, msgArgs);
return this;
}
@Override
public String getMsgKey() {
return msgKey;
}
@Override
public void setMsgKey(String msgKey) {
this.msgKey = msgKey;
}
@Override
public void setMsgKey(String msgKey, Map<String, Object> msgArgs) {
this.msgKey = msgKey;
this.msgArgs = msgArgs;
}
@Override
public Map<String, Object> getMsgArgs() {
return msgArgs;
}
} }
@@ -3,9 +3,11 @@ package com.imyeyu.java.thread;
import com.imyeyu.java.bean.Callback; import com.imyeyu.java.bean.Callback;
import com.imyeyu.java.bean.CallbackArg; import com.imyeyu.java.bean.CallbackArg;
import com.imyeyu.java.bean.timi.TimiException; import com.imyeyu.java.bean.timi.TimiException;
import lombok.AllArgsConstructor;
import lombok.Builder; import lombok.Builder;
import lombok.Data; import lombok.Data;
import lombok.Getter; import lombok.Getter;
import lombok.NoArgsConstructor;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
@@ -40,6 +42,41 @@ public class AsyncRetryExecutor {
@Getter @Getter
private volatile boolean isRunning = false; private volatile boolean isRunning = false;
/**
* 任务
*
* @author 夜雨
* @since 2025-11-06 23:37
*/
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public static class Task {
/** 必填回调方法 */
private Callback callback;
/** 最大重试次数,-1 为无限重试 */
@Builder.Default
private int maxRetry = DEFAULT_MAX_RETRY;
/** 重试间隔毫秒数 */
@Builder.Default
private long retryInterval = DEFAULT_RETRY_INTERVAL;
/** 线程名前缀 */
@Builder.Default
private String threadNamePrefix = DEFAULT_THREAD_NAME_PREFIX;
/** 是否设置为守护线程 */
@Builder.Default
private boolean daemon = DEFAULT_DAEMON;
/** 重试耗尽时的异常回调 */
private CallbackArg<Exception> onRetryExhausted;
}
/** /**
* 根据构建器参数创建异步重试执行器 * 根据构建器参数创建异步重试执行器
* *
@@ -62,7 +99,9 @@ public class AsyncRetryExecutor {
} }
if (0 < task.maxRetry && task.maxRetry < retryCount) { if (0 < task.maxRetry && task.maxRetry < retryCount) {
// 超过重试次数 // 超过重试次数
task.onRetryExhausted.handler(e); if (task.onRetryExhausted != null) {
task.onRetryExhausted.handler(e);
}
break; break;
} }
// 重试 // 重试
@@ -79,32 +118,13 @@ public class AsyncRetryExecutor {
} }
/** /**
* 构造 * 使用默认参数创建执行
* *
* @author 夜雨 * @param task 任务
* @since 2025-11-06 23:37 * @return 执行器实例
*/ */
@Data public static AsyncRetryExecutor create(Task task) {
@Builder return new AsyncRetryExecutor(task);
public static class Task {
/** 必填回调方法 */
private Callback callback;
/** 最大重试次数,-1 为无限重试 */
private int maxRetry = DEFAULT_MAX_RETRY;
/** 重试间隔毫秒数 */
private long retryInterval = DEFAULT_RETRY_INTERVAL;
/** 线程名前缀 */
private String threadNamePrefix = DEFAULT_THREAD_NAME_PREFIX;
/** 是否设置为守护线程 */
private boolean daemon = DEFAULT_DAEMON;
/** 重试耗尽时的异常回调 */
private CallbackArg<Exception> onRetryExhausted;
} }
/** /**