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

leeonky / test-charm-java / 165

22 Mar 2025 11:09AM UTC coverage: 71.321% (-2.9%) from 74.248%
165

push

circleci

leeonky
update version

6809 of 9547 relevant lines covered (71.32%)

0.71 hits per line

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

94.87
/DAL-java/src/main/java/com/github/leeonky/dal/runtime/Calculator.java
1
package com.github.leeonky.dal.runtime;
2

3
import com.github.leeonky.dal.ast.opt.DALOperator;
4
import com.github.leeonky.dal.runtime.Data.Resolved;
5
import com.github.leeonky.dal.runtime.RuntimeContextBuilder.DALRuntimeContext;
6
import com.github.leeonky.util.NumberType;
7
import com.github.leeonky.util.Pair;
8

9
import java.util.Comparator;
10
import java.util.List;
11
import java.util.Objects;
12
import java.util.function.Supplier;
13

14
import static com.github.leeonky.dal.runtime.Data.ResolvedMethods.cast;
15
import static com.github.leeonky.dal.runtime.ExpressionException.*;
16
import static com.github.leeonky.util.Classes.getClassName;
17
import static com.github.leeonky.util.Pair.pair;
18
import static com.github.leeonky.util.function.Extension.getFirstPresent;
19
import static java.lang.String.format;
20
import static java.util.Comparator.*;
21

22
public class Calculator {
×
23
    private static final NumberType numberType = new NumberType();
1✔
24

25
    private static int compare(Pair<Resolved> pair, DALRuntimeContext context) {
26
        return getFirstPresent(
1✔
27
                () -> pair.both(cast(Number.class), (num1, num2) -> context.getNumberType().compare(num1, num2)),
1✔
28
                () -> pair.both(cast(String.class), String::compareTo)).orElseThrow(() ->
1✔
29
                illegalOperation(pair.map(Calculator::dump, (s1, s2) -> format("Can not compare %s and %s", s1, s2))));
1✔
30
    }
31

32
    private static String dump(Resolved resolved) {
33
        Object value = resolved.value();
1✔
34
        return value == null ? "[null]" : format("[%s: %s]", getClassName(value), value);
1✔
35
    }
36

37
    public static boolean equals(Resolved resolved1, Resolved resolved2) {
38
        return resolved1.value() == resolved2.value()
1✔
39
                || opt2(resolved2::isNull) && opt1(resolved1::isNull)
1✔
40
                || resolved2.castList().flatMap(l2 -> resolved1.castList().map(l1 ->
1✔
41
                        collect(resolved2, "2").equals(collect(resolved1, "1"))))
1✔
42
                .orElseGet(() -> Objects.equals(resolved1.value(), resolved2.value()));
1✔
43
    }
44

45
    private static List<Object> collect(Resolved resolved, String index) {
46
        try {
47
            return resolved.list().collect();
1✔
48
        } catch (InfiniteCollectionException ignore) {
1✔
49
            throw illegalOperation("Invalid operation, operand " + index + " is infinite collection");
1✔
50
        }
51
    }
52

53
    public static Data arithmetic(Data v1, DALOperator opt, Data v2, DALRuntimeContext context) {
54
        return context.calculate(v1, opt, v2);
1✔
55
    }
56

57
    public static Data and(Supplier<Data> s1, Supplier<Data> s2) {
58
        Data v1 = s1.get();
1✔
59
        return isTrue(v1) ? s2.get() : v1;
1✔
60
    }
61

62
    private static boolean isTrue(Data value) {
63
        Resolved resolved = value.resolved();
1✔
64
        return getFirstPresent(() -> resolved.cast(Boolean.class),
1✔
65
                () -> resolved.cast(Number.class).map(number -> numberType.compare(0, number) != 0)
1✔
66
        ).orElseGet(() -> !resolved.isNull());
1✔
67
    }
68

69
    public static Data or(Supplier<Data> s1, Supplier<Data> s2) {
70
        Data v1 = s1.get();
1✔
71
        return isTrue(v1) ? v1 : s2.get();
1✔
72
    }
73

74
    public static Object not(Object v) {
75
        if (v instanceof Boolean)
1✔
76
            return !(boolean) v;
1✔
77
        throw illegalOperation("Operand" + " should be boolean but '" + getClassName(v) + "'");
×
78
    }
79

80
    public static Data negate(Data input, DALRuntimeContext context) {
81
        return input.map(data -> data.castList().map(l -> sortList(l, reverseOrder(), context))
1✔
82
                .orElseGet(() -> data.cast(Number.class).map(context.getNumberType()::negate)
1✔
83
                        .orElseThrow(() -> illegalOp2(format("Operand should be number or list but '%s'", getClassName(data.value()))))));
1✔
84
    }
85

86
    @SuppressWarnings("unchecked")
87
    private static Object sortList(Data.DataList list, Comparator<?> comparator, DALRuntimeContext context) {
88
        return list.sort(comparing(data -> context.transformComparable(data.instance()), (Comparator<Object>) comparator));
1✔
89
    }
90

91
    public static Data positive(Data input, DALRuntimeContext context) {
92
        return input.map(data -> data.castList().map(l -> sortList(l, naturalOrder(), context))
1✔
93
                .orElseThrow(() -> illegalOp2(format("Operand should be list but '%s'", getClassName(data.value())))));
1✔
94
    }
95

96
    public static Object less(Data left, DALOperator opt, Data right, DALRuntimeContext context) {
97
        return compare(pair(left.resolved(), right.resolved()), context) < 0;
1✔
98
    }
99

100
    public static Object greaterOrEqual(Data left, DALOperator opt, Data right, DALRuntimeContext context) {
101
        return compare(pair(left.resolved(), right.resolved()), context) >= 0;
1✔
102
    }
103

104
    public static Object lessOrEqual(Data left, DALOperator opt, Data right, DALRuntimeContext context) {
105
        return compare(pair(left.resolved(), right.resolved()), context) <= 0;
1✔
106
    }
107

108
    public static Object greater(Data left, DALOperator opt, Data right, DALRuntimeContext context) {
109
        return compare(pair(left.resolved(), right.resolved()), context) > 0;
1✔
110
    }
111

112
    public static boolean notEqual(Data left, Data right) {
113
        return !equals(left.resolved(), right.resolved());
1✔
114
    }
115
}
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