好用的实体类:值-文本(TextValue)

/**
 * 对应于系统的下拉选、选人、单选等key value 格式的数据
 **/
public class TextValue implements Serializable {

    private static final long serialVersionUID = 5495497842351460243L;

    /**
     * 多个值分隔符
     */
    public static final String SEPARATOR = "^";
    /**
     * 分隔符正则匹配字符串
     */
    public static final String SEPATATOR_REG = "\\^";

    private static final String TEXT = "text";

    private static final String VALUE = "value";


    private static final String CHIN_VALUE = "_值";
    private static final String CHIN_TEXT = "_文本";

    private String text;
    private String value;

    public TextValue() {
    }

    public TextValue(String value, String text) {
        this.value = value;
        this.text = text;
    }

    /**
     * 获取连起来的text
     */
    public static String getText(List<TextValue> list) {
        StringBuilder re = new StringBuilder();
        if (list != null && list.size() != 0) {
            for (int i = 0; i < list.size(); ++i) {
                if (i > 0) {
                    re.append(SEPARATOR);
                }
                re.append(list.get(i).getText());
            }

        }
        return re.toString();
    }

    /**
     * 获取连起来的value
     */
    public static String getValue(List<TextValue> list) {
        StringBuilder re = new StringBuilder();
        if (list != null && list.size() != 0) {
            for (int i = 0; i < list.size(); ++i) {
                if (i > 0) {
                    re.append(SEPARATOR);
                }
                re.append(list.get(i).getValue());
            }
        }
        return re.toString();
    }

    /**
     * 拆分key value 为 list对象
     */
    public static List<TextValue> getList(String keys, String values) {
        if (keys == null) {
            return new ArrayList();
        } else {
            String[] keyarr = keys.split(SEPATATOR_REG);
            String[] valuearr = values.split(SEPATATOR_REG);
            List<TextValue> result = new ArrayList(keyarr.length);

            for (int i = 0; i < keyarr.length; ++i) {
                result.add(new TextValue(keyarr[i], valuearr[i]));
            }

            return result;
        }
    }

    public static List<TextValue> getList(TextValue textValue) {
        if (textValue == null) {
            return new ArrayList<>();
        }
        return getList(textValue.getValue(), textValue.getText());
    }

    public static TextValue reduce(List<TextValue> list) {
        return new TextValue(getValue(list), getText(list));
    }

    public String getValue() {
        return this.value;
    }

    public String getText() {
        return this.text;
    }

    public void setValue(String value) {
        this.value = value;
    }

    public void setText(String text) {
        this.text = text;
    }

    @Override
    public int hashCode() {
        if (this.value != null) {
            return value.hashCode();
        }
        return super.hashCode();
    }

    @Override
    public boolean equals(Object obj) {
        if (this.value != null && obj instanceof TextValue) {
            return this.value.equals(((TextValue) obj).getValue());
        }
        return super.equals(obj);
    }

    @Override
    public String toString() {
        return "{文本:" + this.text + ",值:" + this.value + "}";
    }


    public static boolean isEqual(List<TextValue> values1, List<TextValue> values2) {
        if (CollectionUtil.isEmpty(values1) || CollectionUtil.isEmpty(values2)) {
            return CollectionUtil.isEmpty(values1) && CollectionUtil.isEmpty(values2);
        }
        Set<TextValue> collect = new HashSet<>(values1);
        Set<TextValue> collect1 = new HashSet<>(values2);
        if (collect.size() != collect1.size()) {
            return false;
        }
        for (TextValue textValue : collect) {
            if (!collect1.contains(textValue)) {
                return false;
            }
        }
        return true;
    }

    public JSONObject toJSONObject() {
        JSONObject object = new JSONObject();
        object.put(TEXT, text);
        object.put(VALUE, value);
        return object;
    }

    public static String removeSuffix(String name) {
        if (StrKit.isBlank(name)) {
            return name;
        }
        if (name.endsWith(CHIN_VALUE)) {
            name = name.substring(0, name.length() - 2);
        } else if (name.endsWith(CHIN_TEXT)) {
            name = name.substring(0, name.length() - 3);
        }
        return name;
    }
}

分享一个好用的实体类,可用于下拉框、单选框等有键值对的场景

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值