• 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

87.18
/src/main/java/pro/fessional/mirana/best/TypedReg.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
 *  TypedReg<Integer, String> PasssaltByUid = new TypedReg<Integer, String>() {};
20
 *  TypedReg<Integer, Set<String>> PermitsByUid = new TypedReg<Integer, Set<String>>() {};
21
 * }
22
 * }
23
 * </pre>
24
 *
25
 * @param <K> key type
26
 * @param <V> value type
27
 * @author trydofor
28
 * @since 2022-10-30
29
 */
30
public abstract class TypedReg<K, V> {
31

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

34
    @NotNull
35
    public final Class<? extends TypedReg<K, V>> regType;
36
    @NotNull
37
    public final Type keyType;
38
    @NotNull
39
    public final Type valType;
40

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

51
    public Key<K, V> key(K key) {
52
        return new Key<>(this, key);
1✔
53
    }
54

55
    @Override
56
    public boolean equals(Object o) {
57
        if (this == o) return true;
1✔
58
        if (!(o instanceof TypedReg)) return false;
×
59
        TypedReg<?, ?> reg = (TypedReg<?, ?>) o;
×
60
        return Objects.equals(regType, reg.regType);
×
61
    }
62

63
    @Override
64
    public int hashCode() {
65
        return Objects.hash(regType);
1✔
66
    }
67

68
    @Override
69
    public String toString() {
70
        return "TypedReg{" +
1✔
71
               "regType=" + regType +
72
               ", keyType=" + keyType +
73
               ", valType=" + valType +
74
               '}';
75
    }
76

77
    /**
78
     * serialize to string
79
     */
80
    @NotNull
81
    public String serialize() {
82
        return regType.getName();
1✔
83
    }
84

85
    /**
86
     * deserialize to singleton instance
87
     */
88
    @NotNull
89
    public static <K, V> TypedReg<K, V> deserialize(@NotNull String clz) {
90
        return deserialize(clz, true);
1✔
91
    }
92

93
    /**
94
     * deserialize to singleton instance
95
     */
96
    @Contract("_,true->!null")
97
    @SuppressWarnings("unchecked")
98
    public static <K, V> TypedReg<K, V> deserialize(@NotNull String clz, boolean nonnull) {
99
        TypedReg<?, ?> ins = INSTANCE.get(clz);
1✔
100
        if (ins == null && nonnull) {
1✔
101
            throw new ClassCastException("instance not found, class=" + clz);
1✔
102
        }
103
        else {
104
            return (TypedReg<K, V>) ins;
1✔
105
        }
106
    }
107

108
    public static class Key<K, V> {
109
        @NotNull
110
        public final TypedReg<K, V> reg;
111
        public final K key;
112

113
        public Key(@NotNull TypedReg<K, V> reg, K key) {
1✔
114
            this.reg = reg;
1✔
115
            this.key = key;
1✔
116
        }
1✔
117

118
        @SuppressWarnings("unchecked")
119
        @Nullable
120
        public V get(@NotNull Function<Key<K, V>, ?> map, boolean nonnull) throws ClassCastException {
121
            Object obj = map.apply(this);
1✔
122
            if (obj == null && nonnull) {
1✔
NEW
123
                throw new ClassCastException("null cast to nonnull");
×
124
            }
125
            return (V) obj;
1✔
126
        }
127

128
        @Contract("_,!null ->!null")
129
        public V getOr(@NotNull Function<Key<K, V>, ?> map, V elze) throws ClassCastException {
130
            final V obj = get(map, false);
1✔
131
            return obj != null ? obj : elze;
1✔
132
        }
133

134
        @SuppressWarnings("unchecked")
135
        @Contract("_,!null ->!null")
136
        public V tryOr(@Nullable Object obj, V elze) throws ClassCastException {
137
            return obj != null ? (V) obj : elze;
1✔
138
        }
139

140
        @Override
141
        public boolean equals(Object o) {
142
            if (this == o) return true;
1✔
143
            if (!(o instanceof Key)) return false;
1✔
144
            Key<?, ?> key1 = (Key<?, ?>) o;
1✔
145
            return reg.equals(key1.reg) && Objects.equals(key, key1.key);
1✔
146
        }
147

148
        @Override
149
        public int hashCode() {
150
            return Objects.hash(reg, key);
1✔
151
        }
152

153
        @Override
154
        public String toString() {
155
            return "TypedReg.Key{" +
×
156
                   "reg=" + reg +
157
                   ", key=" + key +
158
                   '}';
159
        }
160
    }
161
}
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