update 优化代码注释
This commit is contained in:
+59
-1
@@ -26,12 +26,26 @@ import java.util.Set;
|
||||
@Order(0)
|
||||
public class TranslationJsonFieldProcessor implements JsonFieldProcessor {
|
||||
|
||||
/**
|
||||
* 响应增强上下文中保存待批量翻译数据的属性键。
|
||||
*/
|
||||
private static final String ATTR_BATCHES = TranslationJsonFieldProcessor.class.getName() + ".batches";
|
||||
|
||||
/**
|
||||
* 响应增强上下文中保存批量翻译结果的属性键。
|
||||
*/
|
||||
private static final String ATTR_RESULTS = TranslationJsonFieldProcessor.class.getName() + ".results";
|
||||
|
||||
/**
|
||||
* 翻译类型与翻译实现映射。
|
||||
*/
|
||||
private final Map<String, TranslationInterface<?>> translationMap;
|
||||
|
||||
/**
|
||||
* 构造翻译响应处理器。
|
||||
*
|
||||
* @param translations 翻译实现列表
|
||||
*/
|
||||
public TranslationJsonFieldProcessor(List<TranslationInterface<?>> translations) {
|
||||
Map<String, TranslationInterface<?>> map = new LinkedHashMap<>(translations.size());
|
||||
for (TranslationInterface<?> t : translations) {
|
||||
@@ -43,6 +57,12 @@ public class TranslationJsonFieldProcessor implements JsonFieldProcessor {
|
||||
this.translationMap = Collections.unmodifiableMap(map);
|
||||
}
|
||||
|
||||
/**
|
||||
* 收集字段上的翻译注解和原始值,供后续批量翻译使用。
|
||||
*
|
||||
* @param fieldContext 字段上下文
|
||||
* @param context 响应增强上下文
|
||||
*/
|
||||
@Override
|
||||
public void collect(JsonFieldContext fieldContext, JsonEnhancementContext context) {
|
||||
Translation translation = fieldContext.getAnnotation(Translation.class);
|
||||
@@ -58,6 +78,11 @@ public class TranslationJsonFieldProcessor implements JsonFieldProcessor {
|
||||
.add(sourceValue);
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据收集到的原始值执行批量翻译,并将翻译结果写入响应增强上下文。
|
||||
*
|
||||
* @param context 响应增强上下文
|
||||
*/
|
||||
@Override
|
||||
public void prepare(JsonEnhancementContext context) {
|
||||
Map<TranslationBatchKey, Set<Object>> batches = context.getAttribute(ATTR_BATCHES);
|
||||
@@ -80,6 +105,14 @@ public class TranslationJsonFieldProcessor implements JsonFieldProcessor {
|
||||
context.setAttribute(ATTR_RESULTS, results);
|
||||
}
|
||||
|
||||
/**
|
||||
* 处理单个字段的翻译值,优先使用批量翻译结果,缺失时回退到单值翻译。
|
||||
*
|
||||
* @param fieldContext 字段上下文
|
||||
* @param value 当前字段值
|
||||
* @param context 响应增强上下文
|
||||
* @return 翻译后的字段值
|
||||
*/
|
||||
@Override
|
||||
public Object process(JsonFieldContext fieldContext, Object value, JsonEnhancementContext context) {
|
||||
Translation translation = fieldContext.getAnnotation(Translation.class);
|
||||
@@ -88,7 +121,7 @@ public class TranslationJsonFieldProcessor implements JsonFieldProcessor {
|
||||
}
|
||||
Object sourceValue = resolveSourceValue(fieldContext, translation);
|
||||
if (sourceValue == null) {
|
||||
return null;
|
||||
return value;
|
||||
}
|
||||
TranslationBatchKey batchKey = new TranslationBatchKey(translation.type(), translation.other());
|
||||
Map<TranslationBatchKey, Map<Object, Object>> results = context.getAttribute(ATTR_RESULTS);
|
||||
@@ -110,6 +143,12 @@ public class TranslationJsonFieldProcessor implements JsonFieldProcessor {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取或创建待批量翻译数据映射。
|
||||
*
|
||||
* @param context 响应增强上下文
|
||||
* @return 待批量翻译数据映射
|
||||
*/
|
||||
private Map<TranslationBatchKey, Set<Object>> getOrCreateBatches(JsonEnhancementContext context) {
|
||||
Map<TranslationBatchKey, Set<Object>> batches = context.getAttribute(ATTR_BATCHES);
|
||||
if (batches == null) {
|
||||
@@ -119,6 +158,13 @@ public class TranslationJsonFieldProcessor implements JsonFieldProcessor {
|
||||
return batches;
|
||||
}
|
||||
|
||||
/**
|
||||
* 解析翻译原始值。
|
||||
*
|
||||
* @param fieldContext 字段上下文
|
||||
* @param translation 翻译注解
|
||||
* @return 翻译原始值
|
||||
*/
|
||||
private Object resolveSourceValue(JsonFieldContext fieldContext, Translation translation) {
|
||||
if (StringUtils.isNotBlank(translation.mapper())) {
|
||||
return ReflectUtils.invokeGetter(fieldContext.owner(), translation.mapper());
|
||||
@@ -126,10 +172,22 @@ public class TranslationJsonFieldProcessor implements JsonFieldProcessor {
|
||||
return fieldContext.value();
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据翻译类型获取翻译实现。
|
||||
*
|
||||
* @param type 翻译类型
|
||||
* @return 翻译实现
|
||||
*/
|
||||
private TranslationInterface<?> getTranslation(String type) {
|
||||
return translationMap.get(type);
|
||||
}
|
||||
|
||||
/**
|
||||
* 批量翻译分组键。
|
||||
*
|
||||
* @param type 翻译类型
|
||||
* @param other 额外参数
|
||||
*/
|
||||
private record TranslationBatchKey(String type, String other) {
|
||||
}
|
||||
|
||||
|
||||
+21
@@ -24,11 +24,25 @@ public class DeptNameTranslationImpl implements TranslationInterface<String> {
|
||||
@DubboReference
|
||||
private RemoteDeptService remoteDeptService;
|
||||
|
||||
/**
|
||||
* 将部门 ID 或 ID 集合翻译为部门名称。
|
||||
*
|
||||
* @param key 部门 ID 或逗号分隔的 ID 字符串
|
||||
* @param other 额外参数
|
||||
* @return 部门名称
|
||||
*/
|
||||
@Override
|
||||
public String translation(Object key, String other) {
|
||||
return remoteDeptService.selectDeptNameByIds(key.toString());
|
||||
}
|
||||
|
||||
/**
|
||||
* 批量将部门 ID 翻译为部门名称。
|
||||
*
|
||||
* @param keys 部门 ID 集合
|
||||
* @param other 额外参数
|
||||
* @return 部门 ID 与部门名称映射
|
||||
*/
|
||||
@Override
|
||||
public Map<Object, String> translationBatch(Set<Object> keys, String other) {
|
||||
Set<Long> deptIds = collectLongIds(keys);
|
||||
@@ -43,6 +57,13 @@ public class DeptNameTranslationImpl implements TranslationInterface<String> {
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据原始键构建部门名称翻译值。
|
||||
*
|
||||
* @param source 原始键
|
||||
* @param deptNames 部门 ID 与部门名称映射
|
||||
* @return 部门名称
|
||||
*/
|
||||
private String buildValue(Object source, Map<Long, String> deptNames) {
|
||||
if (source instanceof String ids) {
|
||||
return joinMappedValues(ids, deptNames::get);
|
||||
|
||||
+14
@@ -24,6 +24,13 @@ public class DictTypeTranslationImpl implements TranslationInterface<String> {
|
||||
|
||||
private final DictService dictService;
|
||||
|
||||
/**
|
||||
* 根据字典类型和字典值翻译显示标签。
|
||||
*
|
||||
* @param key 字典值
|
||||
* @param other 字典类型
|
||||
* @return 字典标签
|
||||
*/
|
||||
@Override
|
||||
public String translation(Object key, String other) {
|
||||
if (key instanceof String && StringUtils.isNotBlank(other)) {
|
||||
@@ -32,6 +39,13 @@ public class DictTypeTranslationImpl implements TranslationInterface<String> {
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 批量根据字典类型和字典值翻译显示标签。
|
||||
*
|
||||
* @param keys 字典值集合
|
||||
* @param other 字典类型
|
||||
* @return 字典值与字典标签映射
|
||||
*/
|
||||
@Override
|
||||
public Map<Object, String> translationBatch(Set<Object> keys, String other) {
|
||||
if (keys.isEmpty() || StringUtils.isBlank(other)) {
|
||||
|
||||
+21
@@ -25,6 +25,13 @@ public class NicknameTranslationImpl implements TranslationInterface<String> {
|
||||
@DubboReference
|
||||
private RemoteUserService remoteUserService;
|
||||
|
||||
/**
|
||||
* 将用户 ID 或 ID 集合翻译为用户昵称。
|
||||
*
|
||||
* @param key 用户 ID 或逗号分隔的 ID 字符串
|
||||
* @param other 额外参数
|
||||
* @return 用户昵称
|
||||
*/
|
||||
@Override
|
||||
public String translation(Object key, String other) {
|
||||
if (key instanceof Long id) {
|
||||
@@ -48,6 +55,13 @@ public class NicknameTranslationImpl implements TranslationInterface<String> {
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 批量将用户 ID 翻译为用户昵称。
|
||||
*
|
||||
* @param keys 用户 ID 集合
|
||||
* @param other 额外参数
|
||||
* @return 用户 ID 与用户昵称映射
|
||||
*/
|
||||
@Override
|
||||
public Map<Object, String> translationBatch(Set<Object> keys, String other) {
|
||||
Set<Long> userIds = collectLongIds(keys);
|
||||
@@ -62,6 +76,13 @@ public class NicknameTranslationImpl implements TranslationInterface<String> {
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据原始键构建用户昵称翻译值。
|
||||
*
|
||||
* @param source 原始键
|
||||
* @param userNames 用户 ID 与用户昵称映射
|
||||
* @return 用户昵称
|
||||
*/
|
||||
private String buildValue(Object source, Map<Long, String> userNames) {
|
||||
if (source instanceof String ids) {
|
||||
return joinMappedValues(ids, userNames::get);
|
||||
|
||||
+21
@@ -27,11 +27,25 @@ public class OssUrlTranslationImpl implements TranslationInterface<String> {
|
||||
@DubboReference(mock = "true")
|
||||
private RemoteFileService remoteFileService;
|
||||
|
||||
/**
|
||||
* 将 OSS ID 或 ID 集合翻译为访问地址。
|
||||
*
|
||||
* @param key OSS ID 或逗号分隔的 ID 字符串
|
||||
* @param other 额外参数
|
||||
* @return 访问地址
|
||||
*/
|
||||
@Override
|
||||
public String translation(Object key, String other) {
|
||||
return remoteFileService.selectUrlByIds(key.toString());
|
||||
}
|
||||
|
||||
/**
|
||||
* 批量将 OSS ID 翻译为访问地址。
|
||||
*
|
||||
* @param keys OSS ID 集合
|
||||
* @param other 额外参数
|
||||
* @return OSS ID 与访问地址映射
|
||||
*/
|
||||
@Override
|
||||
public Map<Object, String> translationBatch(Set<Object> keys, String other) {
|
||||
Set<Long> ossIds = collectLongIds(keys);
|
||||
@@ -47,6 +61,13 @@ public class OssUrlTranslationImpl implements TranslationInterface<String> {
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据原始键构建 OSS 地址翻译值。
|
||||
*
|
||||
* @param source 原始键
|
||||
* @param ossUrls OSS ID 与访问地址映射
|
||||
* @return OSS 访问地址
|
||||
*/
|
||||
private String buildValue(Object source, Map<Long, String> ossUrls) {
|
||||
if (source instanceof String ids) {
|
||||
return joinMappedValues(ids, ossUrls::get);
|
||||
|
||||
+21
@@ -29,6 +29,13 @@ public class UserNameTranslationImpl implements TranslationInterface<String> {
|
||||
@DubboReference
|
||||
private RemoteUserService remoteUserService;
|
||||
|
||||
/**
|
||||
* 将用户 ID 翻译为用户名。
|
||||
*
|
||||
* @param key 用户 ID
|
||||
* @param other 额外参数
|
||||
* @return 用户名
|
||||
*/
|
||||
@Override
|
||||
public String translation(Object key, String other) {
|
||||
Long userId = Convert.toLong(key);
|
||||
@@ -39,6 +46,13 @@ public class UserNameTranslationImpl implements TranslationInterface<String> {
|
||||
return remoteUserService.selectUserNameById(userId);
|
||||
}
|
||||
|
||||
/**
|
||||
* 批量将用户 ID 翻译为用户名。
|
||||
*
|
||||
* @param keys 用户 ID 集合
|
||||
* @param other 额外参数
|
||||
* @return 用户 ID 与用户名映射
|
||||
*/
|
||||
@Override
|
||||
public Map<Object, String> translationBatch(Set<Object> keys, String other) {
|
||||
Set<Long> userIds = collectLongIds(keys);
|
||||
@@ -53,6 +67,13 @@ public class UserNameTranslationImpl implements TranslationInterface<String> {
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据原始键构建用户名翻译值。
|
||||
*
|
||||
* @param source 原始键
|
||||
* @param userNames 用户 ID 与用户名映射
|
||||
* @return 用户名
|
||||
*/
|
||||
private String buildValue(Object source, Map<Long, String> userNames) {
|
||||
if (source instanceof String ids) {
|
||||
return joinMappedValues(ids, userNames::get);
|
||||
|
||||
Reference in New Issue
Block a user