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
package com.qianwen.core.tool.metadata.types;
 
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;
import com.qianwen.core.tool.metadata.Converter;
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/ArrayType.class */
public class ArrayType extends AbstractType<ArrayType> implements DataType, Converter<List<Object>> {
    public static final String ID = "array";
    private DataType elementType;
 
    public void setElementType(final DataType elementType) {
        this.elementType = elementType;
    }
 
    public DataType getElementType() {
        return this.elementType;
    }
 
    @Override // org.springblade.core.tool.metadata.Metadata
    public String getId() {
        return ID;
    }
 
    @Override // org.springblade.core.tool.metadata.Metadata
    public String getName() {
        return "数组";
    }
 
    public ArrayType elementType(DataType elementType) {
        this.elementType = elementType;
        return this;
    }
 
    @Override // org.springblade.core.tool.metadata.DataType
    public ValidateResult validate(Object value) {
        List<Object> listValue = convert(value);
        if (this.elementType != null && (value instanceof Collection)) {
            for (Object data : listValue) {
                ValidateResult result = this.elementType.validate(data);
                if (!result.isSuccess()) {
                    return result;
                }
            }
        }
        return ValidateResult.success(listValue);
    }
 
    @Override // org.springblade.core.tool.metadata.FormatSupport
    public Object format(Object value) {
        if (this.elementType != null && (value instanceof Collection)) {
            Collection<?> collection = (Collection) value;
            return new JSONArray((List) collection.stream().map(data -> {
                return this.elementType.format(data);
            }).collect(Collectors.toList()));
        }
        return JSON.toJSON(value);
    }
 
    /* JADX WARN: Can't rename method to resolve collision */
    @Override // org.springblade.core.tool.metadata.Converter
    public List<Object> convert(Object value) {
        if (value instanceof Collection) {
            return (List) ((Collection) value).stream().map(val -> {
                if (this.elementType instanceof Converter) {
                    return ((Converter) this.elementType).convert(val);
                }
                return val;
            }).collect(Collectors.toList());
        }
        if (value instanceof String) {
            return JSON.parseArray(String.valueOf(value));
        }
        return Collections.singletonList(value);
    }
}