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

import com.google.common.collect.BiMap;
import com.google.common.collect.HashBiMap;
import com.google.common.collect.Maps;

import java.lang.annotation.Annotation;
import java.util.Objects;

/**
 * More appropriate name: ParameterTypeKey
 */
public class ParameterKey {

    private final Class<?> returnType;
    private final Class<? extends Annotation> annotationClass;

    // just a marker, not used in equals or hashCode().
    // returnType is never primitive
    private boolean isPrimitive;

    public ParameterKey(Class<?> returnType) {
        this(returnType, null);
    }

    public ParameterKey(Class<?> returnType, Class<? extends Annotation> annotationClass) {
        boolean isPrimitive = returnType.isPrimitive();

        if (isPrimitive) {
            returnType = primitivesToWrappers.get(returnType);
        }

        this.returnType = Objects.requireNonNull(returnType);
        this.annotationClass = annotationClass;
        this.isPrimitive = isPrimitive;
    }

    public Class<?> getReturnType() {
        return returnType;
    }

    public Class<? extends Annotation> getAnnotationClass() {
        return annotationClass;
    }

    @Override
    public boolean equals(Object o) {
        return this == o || (o instanceof ParameterKey && equals((ParameterKey) o));
    }

    public boolean equals(ParameterKey that) {
        return returnType == that.returnType && annotationClass == that.annotationClass;
    }

    @Override
    public int hashCode() {
        int result = returnType.hashCode();
        result = 31 * result + (annotationClass != null ? annotationClass.hashCode() : 0);
        return result;
    }

    private static Class<?> getPrimitiveWrapperClass(Class<?> primitiveClass) {
        if (!primitiveClass.isPrimitive()) return null;
        switch (primitiveClass.getName()) {
            case "boolean":
                return Boolean.class;
            case "char":
                return Character.class;
            case "byte":
                return Byte.class;
            case "short":
                return Short.class;
            case "int":
                return Integer.class;
            case "float":
                return Float.class;
            case "long":
                return Long.class;
            case "double":
                return Double.class;
            case "void":
                return Void.class;
            default:
                throw new InternalError();
        }
    }

    private static final BiMap<Class<?>, Class<?>> primitivesToWrappers;
    static {
        HashBiMap<Class<?>, Class<?>> tmp = HashBiMap.create();
        tmp.put(Boolean.TYPE, Boolean.class);
        tmp.put(Character.TYPE, Character.class);
        tmp.put(Byte.TYPE, Byte.class);
        tmp.put(Short.TYPE, Short.class);
        tmp.put(Integer.TYPE, Integer.class);
        tmp.put(Float.TYPE, Float.class);
        tmp.put(Long.TYPE, Long.class);
        tmp.put(Double.TYPE, Double.class);
        tmp.put(Void.TYPE, Void.class);
        primitivesToWrappers = Maps.unmodifiableBiMap(tmp);
    }

}