package com.qianwen.core.tool.metadata.types; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import com.qianwen.core.tool.metadata.DataType; import com.qianwen.core.tool.metadata.ValidateResult; /* loaded from: blade-core-tool-9.3.0.0-SNAPSHOT.jar:org/springblade/core/tool/metadata/types/EnumType.class */ public class EnumType extends AbstractType implements DataType { public static final String ID = "enum"; private volatile List elements; private boolean multi; public void setElements(final List elements) { this.elements = elements; } public void setMulti(final boolean multi) { this.multi = multi; } public List getElements() { return this.elements; } public boolean isMulti() { return this.multi; } @Override // org.springblade.core.tool.metadata.Metadata public String getId() { return ID; } @Override // org.springblade.core.tool.metadata.Metadata public String getName() { return "枚举"; } public EnumType multi(boolean multi) { this.multi = multi; return this; } @Override // org.springblade.core.tool.metadata.DataType public ValidateResult validate(Object value) { if (this.elements == null) { return ValidateResult.fail("值[" + value + "]不在枚举中"); } return (ValidateResult) this.elements.stream().filter(ele -> { return ele.value.equals(String.valueOf(value)) || ele.text.equals(String.valueOf(value)); }).findFirst().map(e -> { return ValidateResult.success(e.value); }).orElseGet(() -> { return ValidateResult.fail("值[" + value + "]不在枚举中"); }); } @Override // org.springblade.core.tool.metadata.FormatSupport public String format(Object value) { String stringVal = String.valueOf(value); if (this.elements == null) { return stringVal; } return (String) this.elements.stream().filter(ele -> { return ele.value.equals(String.valueOf(value)); }).findFirst().map((v0) -> { return v0.getText(); }).orElse(stringVal); } public EnumType addElement(Element element) { if (this.elements == null) { synchronized (this) { if (this.elements == null) { this.elements = new ArrayList(); } } } this.elements.add(element); return this; } /* loaded from: blade-core-tool-9.3.0.0-SNAPSHOT.jar:org/springblade/core/tool/metadata/types/EnumType$Element.class */ public static class Element { private String value; private String text; private String description; public void setValue(final String value) { this.value = value; } public void setText(final String text) { this.text = text; } public void setDescription(final String description) { this.description = description; } private Element(final String value, final String text, final String description) { this.value = value; this.text = text; this.description = description; } public static Element of(final String value, final String text, final String description) { return new Element(value, text, description); } public Element() { } public String getValue() { return this.value; } public String getText() { return this.text; } public String getDescription() { return this.description; } public static Element of(String value, String text) { return of(value, text, null); } public static Element of(Map map) { return of(map.get("value"), map.get("text"), map.get("description")); } public Map toMap() { Map map = new HashMap<>(); map.put("value", this.value); map.put("text", this.text); map.put("description", this.description); return map; } } }