• 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

93.75
/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.Function;
10

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

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

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

43
    @Contract("_,!null ->!null")
44
    public R getOr(@NotNull Function<V, ?> map, R elze) throws ClassCastException {
45
        final R obj = get(map, false);
1✔
46
        return obj != null ? obj : elze;
1✔
47
    }
48

49
    @SuppressWarnings("unchecked")
50
    @Contract("_,!null ->!null")
51
    public R tryOr(@Nullable Object obj, R elze) throws ClassCastException {
52
        return obj != null ? (R) obj : elze;
1✔
53
    }
54

55
    @Override
56
    public boolean equals(Object o) {
57
        if (this == o) return true;
1✔
58
        if (o == null || getClass() != o.getClass()) return false;
1✔
59
        TypedRef<?, ?> typedRef = (TypedRef<?, ?>) o;
1✔
60
        return Objects.equals(value, typedRef.value);
1✔
61
    }
62

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

68
    @Override
69
    public String toString() {
70
        return value.toString();
1✔
71
    }
72
}
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