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

trydofor / professional-mirana / #84

22 Jan 2025 12:47PM UTC coverage: 87.319% (+0.01%) from 87.305%
#84

push

trydofor
✨ TypedRef to ref k-v type #50

24 of 27 new or added lines in 3 files covered. (88.89%)

6817 of 7807 relevant lines covered (87.32%)

0.87 hits per line

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

85.19
/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
import org.jetbrains.annotations.Nullable;
6

7
import java.lang.reflect.ParameterizedType;
8
import java.lang.reflect.Type;
9
import java.util.HashMap;
10
import java.util.Map;
11
import java.util.Objects;
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
    @SuppressWarnings("unchecked")
48
    @Contract("_,true->!null")
49
    public V get(@NotNull Function<TypedKey<V>,?> map, boolean nonnull) throws ClassCastException {
50
        Object obj = map.apply(this);
1✔
51
        if (obj == null && nonnull) {
1✔
NEW
52
            throw new ClassCastException("null cast to nonnull");
×
53
        }
54
        return (V) obj;
1✔
55
    }
56

57
    @Contract("_,!null ->!null")
58
    public V getOr(@NotNull Function<TypedKey<V>,?> map, V elze) throws ClassCastException {
59
        final V obj = get(map, false);
1✔
60
        return obj != null ? obj : elze;
1✔
61
    }
62

63
    @SuppressWarnings("unchecked")
64
    @Contract("_,!null ->!null")
65
    public V tryOr(@Nullable Object obj, V elze) throws ClassCastException {
66
        return obj != null ? (V) obj : elze;
1✔
67
    }
68

69
    @Override
70
    public boolean equals(Object o) {
71
        if (this == o) return true;
1✔
72
        if (!(o instanceof TypedReg)) return false;
1✔
73
        TypedReg<?, ?> reg = (TypedReg<?, ?>) o;
×
74
        //noinspection EqualsBetweenInconvertibleTypes
75
        return Objects.equals(regType, reg.regType);
×
76
    }
77

78
    @Override
79
    public int hashCode() {
80
        return Objects.hash(regType);
1✔
81
    }
82

83
    @Override
84
    public String toString() {
85
        return "TypedKey{" +
1✔
86
               "regType=" + regType +
87
               ", valType=" + valType +
88
               '}';
89
    }
90

91
    /**
92
     * serialize to string
93
     */
94
    @NotNull
95
    public String serialize() {
96
        return regType.getName();
1✔
97
    }
98

99
    /**
100
     * deserialize to singleton instance
101
     */
102
    @NotNull
103
    public static <K> TypedKey<K> deserialize(@NotNull String clz) {
104
        return deserialize(clz, true);
1✔
105
    }
106

107
    /**
108
     * deserialize to singleton instance
109
     */
110
    @Contract("_,true->!null")
111
    @SuppressWarnings("unchecked")
112
    public static <K> TypedKey<K> deserialize(@NotNull String clz, boolean nonnull) {
113
        TypedKey<?> ins = INSTANCE.get(clz);
1✔
114
        if (ins == null && nonnull) {
1✔
115
            throw new ClassCastException("instance not found, class=" + clz);
×
116
        }
117
        else {
118
            return (TypedKey<K>) ins;
1✔
119
        }
120
    }
121
}
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