Skip to content

Commit

Permalink
refactor: 优化字符串模板方法 API 使用
Browse files Browse the repository at this point in the history
  • Loading branch information
Charles7c committed Feb 18, 2024
1 parent 89108ad commit c986784
Show file tree
Hide file tree
Showing 12 changed files with 16 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public void addResourceHandlers(ResourceHandlerRegistry registry) {
@Bean
@ConditionalOnMissingBean
public OpenAPI openApi(ProjectProperties projectProperties, SpringDocExtensionProperties properties) {
Info info = new Info().title(String.format("%s %s", projectProperties.getName(), "API 文档"))
Info info = new Info().title("%s %s".formatted(projectProperties.getName(), "API 文档"))
.version(projectProperties.getVersion())
.description(projectProperties.getDescription());
ProjectProperties.Contact contact = projectProperties.getContact();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ public void updateObjectTimeout(String key, long timeout) {

@Override
public List<String> searchData(String prefix, String keyword, int start, int size, boolean sortType) {
Collection<String> keys = RedisUtils.keys(String.format("%s*%s*", prefix, keyword));
Collection<String> keys = RedisUtils.keys("%s*%s*".formatted(prefix, keyword));
List<String> list = new ArrayList<>(keys);
return SaFoxUtil.searchList(list, start, size, sortType);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ public KeyGenerator keyGenerator() {
for (int i = 0; i < params.length; i++) {
paramMap.put(String.valueOf(i), params[i]);
}
return String.format("%s:%s", key, DigestUtil.sha256Hex(JSONUtil.toJsonStr(paramMap)));
return "%s:%s".formatted(key, DigestUtil.sha256Hex(JSONUtil.toJsonStr(paramMap)));
};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public static List<Table> getTables(DataSource dataSource, String tableName) thr
List<Entity> tableEntityList;
Db db = Db.use(dataSource);
if (CharSequenceUtil.isNotBlank(tableName)) {
tableEntityList = db.query(String.format("%s WHERE NAME = ?", querySql), tableName);
tableEntityList = db.query("%s WHERE NAME = ?".formatted(querySql), tableName);
} else {
tableEntityList = db.query(querySql);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ private CheckUtils() {
* @param fieldValue 字段值
*/
public static void throwIfNotExists(Object obj, String entityName, String fieldName, Object fieldValue) {
String message = String.format("%s 为 [%s] 的 %s 记录已不存在", fieldName, fieldValue, CharSequenceUtil
String message = "%s 为 [%s] 的 %s 记录已不存在".formatted(fieldName, fieldValue, CharSequenceUtil
.replace(entityName, "DO", StringConstants.EMPTY));
throwIfNull(obj, message, EXCEPTION_TYPE);
}
Expand Down Expand Up @@ -81,7 +81,7 @@ public static void throwIfNotNull(Object obj, String template, Object... params)
* @param fieldValue 字段值
*/
public static void throwIfExists(Object obj, String entityName, String fieldName, Object fieldValue) {
String message = String.format("%s 为 [%s] 的 %s 记录已存在", fieldName, fieldValue, entityName);
String message = "%s 为 [%s] 的 %s 记录已存在".formatted(fieldName, fieldValue, entityName);
throwIfNotNull(obj, message, EXCEPTION_TYPE);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ private Expression buildDataScopeFilter(DataPermission dataPermission, Expressio
case DEPT -> expression = this.buildDeptExpression(dataPermission, currentUser, expression);
case SELF -> expression = this.buildSelfExpression(dataPermission, currentUser, expression);
case CUSTOM -> expression = this.buildCustomExpression(dataPermission, role, expression);
default -> throw new IllegalArgumentException(String.format("暂不支持 [%s] 数据权限", dataScope));
default -> throw new IllegalArgumentException("暂不支持 [%s] 数据权限".formatted(dataScope));
}
}
return null != where ? new AndExpression(where, new Parenthesis(expression)) : expression;
Expand Down Expand Up @@ -227,7 +227,7 @@ private Expression buildCustomExpression(DataPermission dataPermission,
*/
private Column buildColumn(String tableAlias, String columnName) {
if (StringUtils.isNotEmpty(tableAlias)) {
return new Column(String.format("%s.%s", tableAlias, columnName));
return new Column("%s.%s".formatted(tableAlias, columnName));
}
return new Column(columnName);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ private static <R> void parse(QueryType queryType,
}
case IS_NULL -> consumers.add(q -> q.isNull(columnName));
case IS_NOT_NULL -> consumers.add(q -> q.isNotNull(columnName));
default -> throw new IllegalArgumentException(String.format("暂不支持 [%s] 查询类型", queryType));
default -> throw new IllegalArgumentException("暂不支持 [%s] 查询类型".formatted(queryType));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,6 @@ private void checkPermission(Api api) {
String path = crudRequestMapping.value();
String permissionPrefix = String.join(StringConstants.COLON, CharSequenceUtil
.splitTrim(path, StringConstants.SLASH));
StpUtil.checkPermission(String.format("%s:%s", permissionPrefix, api.name().toLowerCase()));
StpUtil.checkPermission("%s:%s".formatted(permissionPrefix, api.name().toLowerCase()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public static <T> void export(List<T> list,
Class<T> clazz,
HttpServletResponse response) {
try {
String exportFileName = URLUtil.encode(String.format("%s_%s.xlsx", fileName, DateUtil
String exportFileName = URLUtil.encode("%s_%s.xlsx".formatted(fileName, DateUtil
.format(new Date(), DatePattern.PURE_DATETIME_PATTERN)));
response.setHeader("Content-disposition", "attachment;filename=" + exportFileName);
response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,11 @@ public void addResourceHandlers(ResourceHandlerRegistry registry) {
String pathPattern = mapping.getPathPattern();
String location = mapping.getLocation();
if (CharSequenceUtil.isBlank(location)) {
throw new IllegalArgumentException(String.format("Path pattern [%s] location is null.", pathPattern));
throw new IllegalArgumentException("Path pattern [%s] location is null.".formatted(pathPattern));
}
registry.addResourceHandler(CharSequenceUtil.appendIfMissing(pathPattern, StringConstants.PATH_PATTERN))
.addResourceLocations(!location.startsWith("file:")
? String.format("file:%s", this.format(location))
? "file:%s".formatted(this.format(location))
: this.format(location))
.setCachePeriod(0);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ public R<Void> handleMethodArgumentTypeMismatchException(MethodArgumentTypeMisma
public R<Void> handleMaxUploadSizeExceededException(MaxUploadSizeExceededException e, HttpServletRequest request) {
log.warn("请求地址 [{}],上传文件失败,文件大小超过限制。", request.getRequestURI(), e);
String sizeLimit = CharSequenceUtil.subBetween(e.getMessage(), "The maximum size ", " for");
String errorMsg = String.format("请上传小于 %sMB 的文件", NumberUtil.parseLong(sizeLimit) / 1024 / 1024);
String errorMsg = "请上传小于 %sMB 的文件".formatted(NumberUtil.parseLong(sizeLimit) / 1024 / 1024);
return R.fail(HttpStatus.BAD_REQUEST.value(), errorMsg);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,10 @@ public static File upload(MultipartFile multipartFile, String filePath, boolean

String fileName;
if (isKeepOriginalFilename) {
fileName = String.format("%s-%s.%s", FileNameUtil.getPrefix(originalFilename), DateUtil.format(LocalDateTime
fileName = "%s-%s.%s".formatted(FileNameUtil.getPrefix(originalFilename), DateUtil.format(LocalDateTime
.now(), DatePattern.PURE_DATETIME_MS_PATTERN), extensionName);
} else {
fileName = String.format("%s.%s", IdUtil.fastSimpleUUID(), extensionName);
fileName = "%s.%s".formatted(IdUtil.fastSimpleUUID(), extensionName);
}

try {
Expand Down

0 comments on commit c986784

Please sign in to comment.