mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-05-09 23:51:34 +08:00
!994 StrFormatter增加缓存
Merge pull request !994 from emptypoint/update-StrFormatter
This commit is contained in:
commit
5ee2e974ec
@ -13,8 +13,11 @@
|
|||||||
package org.dromara.hutool.core.text.placeholder;
|
package org.dromara.hutool.core.text.placeholder;
|
||||||
|
|
||||||
import org.dromara.hutool.core.array.ArrayUtil;
|
import org.dromara.hutool.core.array.ArrayUtil;
|
||||||
|
import org.dromara.hutool.core.lang.mutable.MutableEntry;
|
||||||
|
import org.dromara.hutool.core.map.WeakConcurrentMap;
|
||||||
import org.dromara.hutool.core.text.StrUtil;
|
import org.dromara.hutool.core.text.StrUtil;
|
||||||
import org.dromara.hutool.core.text.placeholder.template.NamedPlaceholderStrTemplate;
|
import org.dromara.hutool.core.text.placeholder.template.NamedPlaceholderStrTemplate;
|
||||||
|
import org.dromara.hutool.core.text.placeholder.template.SinglePlaceholderStrTemplate;
|
||||||
|
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
@ -24,6 +27,7 @@ import java.util.Map;
|
|||||||
* @author Looly
|
* @author Looly
|
||||||
*/
|
*/
|
||||||
public class StrFormatter {
|
public class StrFormatter {
|
||||||
|
private static final WeakConcurrentMap<Map.Entry<CharSequence, Object>, StrTemplate> CACHE = new WeakConcurrentMap<>();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 格式化字符串<br>
|
* 格式化字符串<br>
|
||||||
@ -61,9 +65,8 @@ public class StrFormatter {
|
|||||||
if (StrUtil.isBlank(strPattern) || StrUtil.isBlank(placeHolder) || ArrayUtil.isEmpty(argArray)) {
|
if (StrUtil.isBlank(strPattern) || StrUtil.isBlank(placeHolder) || ArrayUtil.isEmpty(argArray)) {
|
||||||
return strPattern;
|
return strPattern;
|
||||||
}
|
}
|
||||||
return StrTemplate.of(strPattern)
|
return ((SinglePlaceholderStrTemplate) CACHE.computeIfAbsent(MutableEntry.of(strPattern, placeHolder), k ->
|
||||||
.placeholder(placeHolder)
|
StrTemplate.of(strPattern).placeholder(placeHolder).build()))
|
||||||
.build()
|
|
||||||
.format(argArray);
|
.format(argArray);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -85,12 +88,21 @@ public class StrFormatter {
|
|||||||
return template.toString();
|
return template.toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
final NamedPlaceholderStrTemplate.Builder builder = StrTemplate.ofNamed(template.toString());
|
return ((NamedPlaceholderStrTemplate) CACHE.computeIfAbsent(MutableEntry.of(template, ignoreNull), k -> {
|
||||||
if (ignoreNull) {
|
final NamedPlaceholderStrTemplate.Builder builder = StrTemplate.ofNamed(template.toString());
|
||||||
builder.addFeatures(StrTemplate.Feature.FORMAT_NULL_VALUE_TO_WHOLE_PLACEHOLDER);
|
if (ignoreNull) {
|
||||||
} else {
|
builder.addFeatures(StrTemplate.Feature.FORMAT_NULL_VALUE_TO_WHOLE_PLACEHOLDER);
|
||||||
builder.addFeatures(StrTemplate.Feature.FORMAT_NULL_VALUE_TO_EMPTY);
|
} else {
|
||||||
}
|
builder.addFeatures(StrTemplate.Feature.FORMAT_NULL_VALUE_TO_EMPTY);
|
||||||
return builder.build().format(map);
|
}
|
||||||
|
return builder.build();
|
||||||
|
})).format(map);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 清空缓存
|
||||||
|
*/
|
||||||
|
public static void clear() {
|
||||||
|
CACHE.clear();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user