• 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

94.44
/src/main/java/pro/fessional/mirana/best/TypedRef.java
1
package pro.fessional.mirana.best;
2

3

4
import org.jetbrains.annotations.Contract;
5
import org.jetbrains.annotations.NotNull;
6
import org.jetbrains.annotations.Nullable;
7

8
import java.util.Objects;
9
import java.util.function.BiConsumer;
10
import java.util.function.Function;
11

12
/**
13
 * <pre>
14
 * Usage: mark value and its ref type.
15
 * {@code
16
 * Map<String, Integer> map = new HashMap<>();
17
 * map.put("key", 42);
18
 * TypedRef<String, Integer> ref = new TypedRef<>("key");
19
 * Integer result = ref.get(map);
20
 * }
21
 * </pre>
22
 *
23
 * @author trydofor
24
 * @since 2025-01-21
25
 */
26
public class TypedRef<V, R> {
27
    @NotNull
28
    public final V value;
29

30
    public TypedRef(@NotNull V value) {
1✔
31
        this.value = value;
1✔
32
    }
1✔
33

34

35
    public void set(@NotNull BiConsumer<V, R> map, R refer) throws ClassCastException {
36
        map.accept(value, refer);
1✔
37
    }
1✔
38

39
    @SuppressWarnings("unchecked")
40
    @Contract("_,true->!null")
41
    public R get(@NotNull Function<V, ?> map, boolean nonnull) throws ClassCastException {
42
        Object obj = map.apply(value);
1✔
43
        if (obj == null && nonnull) {
1✔
NEW
44
            throw new ClassCastException("null cast to nonnull");
×
45
        }
46
        return (R) obj;
1✔
47
    }
48

49
    @Contract("_,!null ->!null")
50
    public R getOr(@NotNull Function<V, ?> map, R elze) throws ClassCastException {
51
        final R obj = get(map, false);
1✔
52
        return obj != null ? obj : elze;
1✔
53
    }
54

55
    @SuppressWarnings("unchecked")
56
    @Contract("_,!null ->!null")
57
    public R tryOr(@Nullable Object obj, R elze) throws ClassCastException {
58
        return obj != null ? (R) obj : elze;
1✔
59
    }
60

61
    @Override
62
    public boolean equals(Object o) {
63
        if (this == o) return true;
1✔
64
        if (o == null || getClass() != o.getClass()) return false;
1✔
65
        TypedRef<?, ?> typedRef = (TypedRef<?, ?>) o;
1✔
66
        return Objects.equals(value, typedRef.value);
1✔
67
    }
68

69
    @Override
70
    public int hashCode() {
71
        return Objects.hashCode(value);
1✔
72
    }
73

74
    @Override
75
    public String toString() {
76
        return value.toString();
1✔
77
    }
78
}
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