yangys
2024-04-07 a87afa1be0961724a05cdf14321f0d8fd55efb98
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
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<EnumType> implements DataType {
    public static final String ID = "enum";
    private volatile List<Element> elements;
    private boolean multi;
 
    public void setElements(final List<Element> elements) {
        this.elements = elements;
    }
 
    public void setMulti(final boolean multi) {
        this.multi = multi;
    }
 
    public List<Element> 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<String, String> map) {
            return of(map.get("value"), map.get("text"), map.get("description"));
        }
 
        public Map<String, Object> toMap() {
            Map<String, Object> map = new HashMap<>();
            map.put("value", this.value);
            map.put("text", this.text);
            map.put("description", this.description);
            return map;
        }
    }
}