summaryrefslogtreecommitdiff
path: root/dicore3/command/src/main/java/io/dico/dicore/command/parameter/type/MapBasedParameterTypeSelector.java
blob: d407f8762acef6cc1e999a749ffa80bd0ede6806 (plain)
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
package io.dico.dicore.command.parameter.type;

import java.lang.annotation.Annotation;
import java.util.HashMap;
import java.util.Map;

/**
 * Map based implementation of {@link IParameterTypeSelector}
 */
public class MapBasedParameterTypeSelector implements IParameterTypeSelector {
    static final MapBasedParameterTypeSelector defaultSelector = new MapBasedParameterTypeSelector(false);
    private final Map<ParameterKey, ParameterType<?, ?>> parameterTypeMap;
    private final boolean useDefault;

    public MapBasedParameterTypeSelector(boolean useDefault) {
        this.parameterTypeMap = new HashMap<>();
        this.useDefault = useDefault;
    }

    @Override
    public <TReturn, TParamInfo> ParameterType<TReturn, TParamInfo> selectExact(ParameterKey key) {
        ParameterType<?, ?> out = parameterTypeMap.get(key);
        if (useDefault && out == null) {
            out = defaultSelector.selectExact(key);
        }
        if (out == null && key.getReturnType().isEnum()) {
            //noinspection unchecked
            out = new EnumParameterType(key.getReturnType());
            addType(false, out);
        }
        return cast(out);
    }

    @Override
    public <TReturn, TParamInfo> ParameterType<TReturn, TParamInfo> selectAny(ParameterKey key) {
        ParameterType<TReturn, TParamInfo> exact = selectExact(key);
        if (exact != null) {
            return exact;
        }

        if (key.getAnnotationClass() != null) {
            exact = selectExact(new ParameterKey(key.getReturnType()));
            if (exact != null) {
                return exact;
            }
        }

        Class<?> returnType = key.getReturnType();
        Class<? extends Annotation> annotationClass = key.getAnnotationClass();

        ParameterType<?, ?> out = selectByReturnType(parameterTypeMap, returnType, annotationClass, false);
        if (out == null && useDefault) {
            out = selectByReturnType(defaultSelector.parameterTypeMap, returnType, annotationClass, false);
        }
        if (out == null) {
            out = selectByReturnType(parameterTypeMap, returnType, annotationClass, true);
        }
        if (out == null && useDefault) {
            out = selectByReturnType(defaultSelector.parameterTypeMap, returnType, annotationClass, true);
        }
        return cast(out);
    }

    private static ParameterType<?, ?> selectByReturnType(Map<ParameterKey, ParameterType<?, ?>> map, Class<?> returnType,
                                                          Class<? extends Annotation> annotationClass, boolean allowSubclass) {
        ParameterType<?, ?> out = null;
        if (allowSubclass) {
            for (ParameterType<?, ?> type : map.values()) {
                if (returnType.isAssignableFrom(type.getReturnType())) {
                    if (annotationClass == type.getAnnotationClass()) {
                        out = type;
                        break;
                    }
                    if (out == null) {
                        out = type;
                    }
                }
            }
        } else {
            for (ParameterType<?, ?> type : map.values()) {
                if (returnType == type.getReturnType()) {
                    if (annotationClass == type.getAnnotationClass()) {
                        out = type;
                        break;
                    }
                    if (out == null) {
                        out = type;
                    }
                }
            }
        }
        return out;
    }

    private static <T> T cast(Object o) {
        //noinspection unchecked
        return (T) o;
    }

    @Override
    public void addType(boolean infolessAlias, ParameterType<?, ?> type) {
        parameterTypeMap.put(type.getTypeKey(), type);

        if (infolessAlias) {
            parameterTypeMap.putIfAbsent(type.getInfolessTypeKey(), type);
        }
    }

    static {
        // registers default parameter types
        ParameterTypes.clinit();
    }

}