/**
* 对应于系统的下拉选、选人、单选等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;
}
}
分享一个好用的实体类,可用于下拉框、单选框等有键值对的场景