1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
package com.tanpu.community.util;
import com.alibaba.fastjson.JSON;
import com.tanpu.community.api.beans.qo.ThemeContentQo;
import com.tanpu.community.api.beans.qo.ThemeQo;
import com.tanpu.community.api.enums.RelTypeEnum;
import org.apache.commons.lang3.StringUtils;
import java.util.ArrayList;
import java.util.List;
public class BizUtils {
public static <T> List<T> subList(List<T> list, int start, int size) {
if (list.isEmpty() || start >= list.size() || start < 0) {
return new ArrayList<>();
}
int realEnd = Math.min(start + size, list.size());
return list.subList(start, realEnd);
}
public static String getThemeContent(String keyword, ThemeQo theme) {
for (ThemeContentQo paragraph : theme.content) {
if (paragraph.getType().equals(RelTypeEnum.TEXT.type)) {
int idx = paragraph.getValue().indexOf(keyword);
if (idx == -1) {
continue;
} else if (idx < 30) {
// 如果关键词在段落偏头部的部分,则全部返回给前端,前端自由展示
return paragraph.getValue();
} else {
// 否则,保留关键词 向前20个字符
return paragraph.getValue().substring(idx - 20);
}
}
}
return "";
}
}