• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

trydofor / professional-mirana / #86

23 Jan 2025 01:41AM UTC coverage: 87.376% (+0.07%) from 87.305%
#86

push

trydofor
✨ TypedRef to ref k-v type #50

54 of 56 new or added lines in 3 files covered. (96.43%)

6845 of 7834 relevant lines covered (87.38%)

0.87 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

86.11
/src/main/java/pro/fessional/mirana/best/TypedKey.java
1
package pro.fessional.mirana.best;
2

3
import org.jetbrains.annotations.Contract;
4
import org.jetbrains.annotations.NotNull;
5

6
import java.lang.reflect.ParameterizedType;
7
import java.lang.reflect.Type;
8
import java.util.HashMap;
9
import java.util.Map;
10
import java.util.Objects;
11
import java.util.function.BiConsumer;
12
import java.util.function.Function;
13

14
/**
15
 * <pre>
16
 * Usage: construct final anonymous subclasses in interfaces.
17
 * {@code
18
 * public interface Solos {
19
 *  TypedKey<String> PasssaltByUid = new TypedKey<String>() {};
20
 *  TypedKey<Set<String>> PermitsByUid = new TypedKey<Set<String>>() {};
21
 * }
22
 * }
23
 * </pre>
24
 *
25
 * @param <V> value type
26
 * @author trydofor
27
 * @since 2022-10-30
28
 */
29
public abstract class TypedKey<V> {
30

31
    private static final Map<String, TypedKey<?>> INSTANCE = new HashMap<>();
1✔
32

33
    @NotNull
34
    public final Class<? extends TypedKey<V>> regType;
35
    @NotNull
36
    public final Type valType;
37

38
    @SuppressWarnings("unchecked")
39
    protected TypedKey() {
1✔
40
        final Class<? extends TypedKey<V>> clz = (Class<? extends TypedKey<V>>) getClass();
1✔
41
        final Type[] tps = ((ParameterizedType) clz.getGenericSuperclass()).getActualTypeArguments();
1✔
42
        regType = clz;
1✔
43
        valType = tps[0];
1✔
44
        INSTANCE.put(clz.getName(), this);
1✔
45
    }
1✔
46

47
    public void set(@NotNull BiConsumer<TypedKey<V>, V> map, V value) throws ClassCastException {
48
        map.accept(this, value);
1✔
49
    }
1✔
50

51
    public void set(@NotNull Map<TypedKey<V>, V> map, V value) throws ClassCastException {
NEW
52
        map.put(this, value);
×
NEW
53
    }
×
54

55
    @Contract("_,true->!null")
56
    public V get(@NotNull Function<TypedKey<V>,V> map, boolean nonnull) throws ClassCastException {
57
        V obj = map.apply(this);
1✔
58
        if (obj == null && nonnull) {
1✔
59
            throw new NullPointerException("cannot be null, regType=" + regType);
1✔
60
        }
61
        return obj;
1✔
62
    }
63

64
    @Contract("_,true->!null")
65
    public V get(@NotNull Map<TypedKey<V>, V> map, boolean nonnull) throws ClassCastException {
66
        V obj = map.get(this);
1✔
67
        if (obj == null && nonnull) {
1✔
68
            throw new NullPointerException("cannot be null, regType=" + regType);
1✔
69
        }
70
        return obj;
1✔
71
    }
72

73
    @Contract("_,!null ->!null")
74
    public V getOr(@NotNull Function<TypedKey<V>,V> map, V elze) throws ClassCastException {
75
        final V obj = map.apply(this);
1✔
76
        return obj != null ? obj : elze;
1✔
77
    }
78

79
    @Contract("_,!null ->!null")
80
    public V getOr(@NotNull Map<TypedKey<V>, V> map, V elze) throws ClassCastException {
81
        final V obj = map.get(this);
1✔
82
        return obj != null ? obj : elze;
1✔
83
    }
84

85
    @Override
86
    public boolean equals(Object o) {
87
        if (this == o) return true;
1✔
88
        if (!(o instanceof TypedReg)) return false;
1✔
89
        TypedReg<?, ?> reg = (TypedReg<?, ?>) o;
×
90
        //noinspection EqualsBetweenInconvertibleTypes
91
        return Objects.equals(regType, reg.regType);
×
92
    }
93

94
    @Override
95
    public int hashCode() {
96
        return Objects.hash(regType);
1✔
97
    }
98

99
    @Override
100
    public String toString() {
101
        return "TypedKey{" +
1✔
102
               "regType=" + regType +
103
               ", valType=" + valType +
104
               '}';
105
    }
106

107
    /**
108
     * serialize to string
109
     */
110
    @NotNull
111
    public String serialize() {
112
        return regType.getName();
1✔
113
    }
114

115
    /**
116
     * deserialize to singleton instance
117
     */
118
    @NotNull
119
    public static <K> TypedKey<K> deserialize(@NotNull String clz) {
120
        return deserialize(clz, true);
1✔
121
    }
122

123
    /**
124
     * deserialize to singleton instance
125
     */
126
    @Contract("_,true->!null")
127
    @SuppressWarnings("unchecked")
128
    public static <K> TypedKey<K> deserialize(@NotNull String clz, boolean nonnull) {
129
        TypedKey<?> ins = INSTANCE.get(clz);
1✔
130
        if (ins == null && nonnull) {
1✔
131
            throw new ClassCastException("instance not found, class=" + clz);
×
132
        }
133
        else {
134
            return (TypedKey<K>) ins;
1✔
135
        }
136
    }
137
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc