最近公司需要对名称进行关键词替换,于是简单写了个关键词替换工具类
还能添加许多功能和进行优化,欢迎大家使用
import com.alibaba.fastjson.JSONObject;
import org.springframework.util.CollectionUtils;
import java.util.Set;
/**
* @Author Roushan
* @Date 2022/6/6 16:49
*
* 采用回溯算法实现
* 用了Fastjson和lang3的包
*/
public class KeywordUtil {
private static final JSONObject KEYWORD_JSON = new JSONObject();
private static final String END = "end";
private static final String SUBSTITUTE = "*";
public static void readDatasource(String keyword) {
loadKeywordJson(keyword, 0, KEYWORD_JSON);
}
public static void readDatasource(Set<String> keywordSet) {
for (String keyword: keywordSet) {
loadKeywordJson(keyword, 0, KEYWORD_JSON);
}
}
public static void deleteDatasource(String keyword) {
remove(keyword, 0, KEYWORD_JSON);
}
public static void deleteDatasource(Set<String> keywordSet) {
for (String keyword: keywordSet) {
remove(keyword, 0, KEYWORD_JSON);
}
}
public static String replace(String text) {
for (int i = 0; i < text.length(); i++) {
StringBuilder sb = new StringBuilder();
search(text, i, KEYWORD_JSON, sb);
if (sb.length() == 0) {
continue;
}
text = text.replaceAll(sb.toString(), generateSubstitute(sb.length()));
}
return text;
}
private static boolean remove(String keyword, int index, JSONObject sourceJson) {
if (keyword.length() == index) {
return sourceJson.remove(END) != null;
}
String str = String.valueOf(keyword.charAt(index));
JSONObject tmpJson = sourceJson.getJSONObject(str);
if (tmpJson == null) {
return false;
}
boolean lastResult = remove(keyword, ++index, sourceJson.getJSONObject(str));
if (lastResult) {
if (sourceJson.getJSONObject(str).values().size() > 0) {
return false;
} else if (sourceJson.keySet().size() > 1) {
sourceJson.remove(str);
return false;
} else {
return sourceJson.remove(str) != null;
}
}
return false;
}
private static void loadKeywordJson(String keyword, int index, JSONObject sourceJson) {
if (keyword.length() == index) {
sourceJson.put(END, true);
return;
}
String str = String.valueOf(keyword.charAt(index));
JSONObject tmpJson = sourceJson.getJSONObject(str);
if (tmpJson == null) {
sourceJson.put(str, new JSONObject());
}
loadKeywordJson(keyword, ++index, sourceJson.getJSONObject(str));
}
private static boolean search(String text, int index, JSONObject sourceJson, StringBuilder targetBuilder) {
if (text.length() == index) {
return false;
}
String str = String.valueOf(text.charAt(index));
if (str.equals(SUBSTITUTE)) {
return false;
}
JSONObject tmpJson = sourceJson.getJSONObject(str);
if (CollectionUtils.isEmpty(tmpJson)) {
return false;
}
if (tmpJson.containsKey(END) && tmpJson.getBoolean(END)) {
targetBuilder.append(str);
return true;
}
boolean result = search(text, ++index, sourceJson.getJSONObject(str), targetBuilder);
if (result) {
targetBuilder.insert(0, str);
}
return result;
}
private static String generateSubstitute(int length) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < length; i++) {
sb.append(SUBSTITUTE);
}
return sb.toString();
}
}
依赖
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.12.0</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.49</version>
</dependency>