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

trydofor / professional-mirana / #112

19 Mar 2025 04:46AM UTC coverage: 87.128% (+2.7%) from 84.4%
#112

push

web-flow
Merge pull request #53 from trydofor/develop

bump 3.0.0 to semver

1645 of 1747 new or added lines in 25 files covered. (94.16%)

2 existing lines in 1 file now uncovered.

7141 of 8196 relevant lines covered (87.13%)

0.87 hits per line

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

87.5
/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> fun, V value) {
48
        fun.accept(this, value);
1✔
49
    }
1✔
50

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

55
    @Contract("_,true->!null")
56
    public V get(@NotNull Function<TypedKey<V>, V> fun, boolean nonnull) {
57
        V obj = fun.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>, ? extends V> map, boolean nonnull) {
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> fun, V elze) {
75
        final V obj = fun.apply(this);
1✔
76
        return obj != null ? obj : elze;
1✔
77
    }
78

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

85
    @SuppressWarnings("unchecked")
86
    @Contract("_,!null ->!null")
87
    public V tryOr(@NotNull Function<TypedKey<V>, ?> fun, V elze) throws ClassCastException {
88
        final Object obj = fun.apply(this);
1✔
89
        return obj != null ? (V) obj : elze;
1✔
90
    }
91

92
    @SuppressWarnings("unchecked")
93
    @Contract("_,!null ->!null")
94
    public V tryOr(@NotNull Map<?, ?> map, V elze) throws ClassCastException {
95
        final Object obj = map.get(this);
1✔
96
        return obj != null ? (V) obj : elze;
1✔
97
    }
98

99
    @Override
100
    public boolean equals(Object o) {
101
        if (this == o) return true;
1✔
102
        if (!(o instanceof TypedReg)) return false;
1✔
103
        TypedReg<?, ?> reg = (TypedReg<?, ?>) o;
×
104
        //noinspection EqualsBetweenInconvertibleTypes
105
        return Objects.equals(regType, reg.regType);
×
106
    }
107

108
    @Override
109
    public int hashCode() {
110
        return Objects.hash(regType);
1✔
111
    }
112

113
    @Override
114
    public String toString() {
115
        return "TypedKey{" +
1✔
116
               "regType=" + regType +
117
               ", valType=" + valType +
118
               '}';
119
    }
120

121
    /**
122
     * serialize to string
123
     */
124
    @NotNull
125
    public String serialize() {
126
        return regType.getName();
1✔
127
    }
128

129
    /**
130
     * deserialize to singleton instance
131
     */
132
    @NotNull
133
    public static <K> TypedKey<K> deserialize(@NotNull String clz) {
134
        return deserialize(clz, true);
1✔
135
    }
136

137
    /**
138
     * deserialize to singleton instance
139
     */
140
    @Contract("_,true->!null")
141
    @SuppressWarnings("unchecked")
142
    public static <K> TypedKey<K> deserialize(@NotNull String clz, boolean nonnull) {
143
        TypedKey<?> ins = INSTANCE.get(clz);
1✔
144
        if (ins == null && nonnull) {
1✔
NEW
145
            throw new NullPointerException("instance not found, class=" + clz);
×
146
        }
147
        else {
148
            return (TypedKey<K>) ins;
1✔
149
        }
150
    }
151
}
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