public class CNPinyin <T extends CN> implements Serializable, Comparable<CNPinyin<T>> {
/**
* 对应首字首拼音字母
*/
char firstChar;
/**
* 所有字符中的拼音首字母
*/
String firstChars;
/**
* 对应的所有字母拼音
*/
String[] pinyins;
/**
* 拼音总长度
*/
int pinyinsTotalLength;
public final T data;
CNPinyin(T data) {
this.data = data;
}
public char getFirstChar() {
return firstChar;
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder().append("--firstChar--").append(firstChar).append("--pinyins:");
for (String str : pinyins) {
sb.append(str);
}
return sb.toString();
}
int compareValue() {
if (firstChar == DEF_CHAR) {
return 'Z' + 1;
}
return firstChar;
}
@Override
public int compareTo(CNPinyin<T> tcnPinyin) {
int compare = compareValue() - tcnPinyin.compareValue();
if (compare == 0) {
String chinese1 = data.chinese();
String chinese2 = tcnPinyin.data.chinese();
return chinese1.compareTo(chinese2);
}
return compare;
}
}
static CNPinyinIndex matcherChinese(CNPinyin cnPinyin, String keyword) {
if (keyword.length() < cnPinyin.data.chinese().length()) {
Matcher matcher = Pattern.compile(keyword, Pattern.CASE_INSENSITIVE).matcher(cnPinyin.data.chinese());
if (matcher.find()) {
return new CNPinyinIndex(cnPinyin, matcher.start(), matcher.end());
}
}
return null;
}
static CNPinyinIndex matcherFirst(CNPinyin cnPinyin, String keyword) {
if (keyword.length() <= cnPinyin.pinyins.length) {
Matcher matcher = Pattern.compile(keyword, Pattern.CASE_INSENSITIVE).matcher(cnPinyin.firstChars);
if (matcher.find()) {
return new CNPinyinIndex(cnPinyin, matcher.start(), matcher.end());
}
}
return null;
}
/**
* 所有拼音匹配
* @param cnPinyin
* @param keyword
* @return
*/
static CNPinyinIndex matchersPinyins(CNPinyin cnPinyin, String keyword) {
if (keyword.length() > cnPinyin.pinyinsTotalLength) return null;
int start = -1;
int end = -1;
for (int i = 0; i < cnPinyin.pinyins.length; i++) {
String pat = cnPinyin.pinyins[i];
if (pat.length() >= keyword.length()) {//首个位置索引
Matcher matcher = Pattern.compile(keyword, Pattern.CASE_INSENSITIVE).matcher(pat);
if (matcher.find() && matcher.start() == 0) {
start = i;
end = i + 1;
break;
}
} else {
Matcher matcher = Pattern.compile(pat, Pattern.CASE_INSENSITIVE).matcher(keyword);
if (matcher.find() && matcher.start() == 0) {//全拼匹配第一个必须在0位置
start = i;
String left = matcher.replaceFirst("");
end = end(cnPinyin.pinyins, left, ++i);
break;
}
}
}
if (start >= 0 && end >= start) {
return new CNPinyinIndex(cnPinyin, start, end);
}
return null;
}
/**
* 根据匹配字符递归查找下一结束位置
* @param pinyinGroup
* @param pattern
* @param index
* @return -1 匹配失败
*/
private static int end(String[] pinyinGroup, String pattern, int index) {
if (index < pinyinGroup.length) {
String pinyin = pinyinGroup[index];
if (pinyin.length() >= pattern.length()) {//首个位置索引
Matcher matcher = Pattern.compile(pattern, Pattern.CASE_INSENSITIVE).matcher(pinyin);
if (matcher.find() && matcher.start() == 0) {
return index + 1;
}
} else {
Matcher matcher = Pattern.compile(pinyin, Pattern.CASE_INSENSITIVE).matcher(pattern);
if (matcher.find() && matcher.start() == 0) {//全拼匹配第一个必须在0位置
String left = matcher.replaceFirst("");
return end(pinyinGroup, left, index + 1);
}
}
}
return -1;
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有