BizUtils.java 1.77 KB
Newer Older
1 2
package com.tanpu.community.util;

3 4 5
import com.tanpu.community.api.beans.qo.ThemeContentQo;
import com.tanpu.community.api.beans.qo.ThemeQo;
import com.tanpu.community.api.enums.RelTypeEnum;
6 7 8 9 10 11 12 13 14 15 16 17 18

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);
    }
19 20 21 22 23 24 25 26 27 28 29 30 31

    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个字符
张辰's avatar
张辰 committed
32
                    return "..." + paragraph.getValue().substring(idx - 20);
33 34 35 36 37
                }
            }
        }
        return "";
    }
刘基明's avatar
刘基明 committed
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52

    public static String formatCountNumber(Integer number) {
        if (number < 10000) {
            return number.toString();
        } else {
            double d = number * 1.0 / 10000;
            return String.format("%.1f",d)+"w";
        }
    }

    public static void main(String[] args) {
        System.out.println(formatCountNumber(110400));
        System.out.println(formatCountNumber(111100));
        System.out.println(formatCountNumber(1000));
    }
53
}