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

trydofor / professional-mirana / #85

22 Jan 2025 02:22PM UTC coverage: 87.354% (+0.05%) from 87.305%
#85

push

trydofor
✨ TypedRef to ref k-v type #50

30 of 33 new or added lines in 3 files covered. (90.91%)

6825 of 7813 relevant lines covered (87.35%)

0.87 hits per line

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

86.21
/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.BiConsumer;
13
import java.util.function.Function;
14

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

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

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

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

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

52
    @SuppressWarnings("unchecked")
53
    @Contract("_,true->!null")
54
    public V get(@NotNull Function<TypedKey<V>,?> map, boolean nonnull) throws ClassCastException {
55
        Object obj = map.apply(this);
1✔
56
        if (obj == null && nonnull) {
1✔
NEW
57
            throw new ClassCastException("null cast to nonnull");
×
58
        }
59
        return (V) obj;
1✔
60
    }
61

62
    @Contract("_,!null ->!null")
63
    public V getOr(@NotNull Function<TypedKey<V>,?> map, V elze) throws ClassCastException {
64
        final V obj = get(map, false);
1✔
65
        return obj != null ? obj : elze;
1✔
66
    }
67

68
    @SuppressWarnings("unchecked")
69
    @Contract("_,!null ->!null")
70
    public V tryOr(@Nullable Object obj, V elze) throws ClassCastException {
71
        return obj != null ? (V) obj : elze;
1✔
72
    }
73

74
    @Override
75
    public boolean equals(Object o) {
76
        if (this == o) return true;
1✔
77
        if (!(o instanceof TypedReg)) return false;
1✔
78
        TypedReg<?, ?> reg = (TypedReg<?, ?>) o;
×
79
        //noinspection EqualsBetweenInconvertibleTypes
80
        return Objects.equals(regType, reg.regType);
×
81
    }
82

83
    @Override
84
    public int hashCode() {
85
        return Objects.hash(regType);
1✔
86
    }
87

88
    @Override
89
    public String toString() {
90
        return "TypedKey{" +
1✔
91
               "regType=" + regType +
92
               ", valType=" + valType +
93
               '}';
94
    }
95

96
    /**
97
     * serialize to string
98
     */
99
    @NotNull
100
    public String serialize() {
101
        return regType.getName();
1✔
102
    }
103

104
    /**
105
     * deserialize to singleton instance
106
     */
107
    @NotNull
108
    public static <K> TypedKey<K> deserialize(@NotNull String clz) {
109
        return deserialize(clz, true);
1✔
110
    }
111

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