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

leeonky / test-charm-java / 227

21 Apr 2025 03:32PM UTC coverage: 71.06% (-3.0%) from 74.052%
227

push

circleci

leeonky
Refactor UI test

6 of 15 new or added lines in 5 files covered. (40.0%)

42 existing lines in 11 files now uncovered.

6858 of 9651 relevant lines covered (71.06%)

0.71 hits per line

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

95.24
/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.RuntimeContextBuilder.DALRuntimeContext;
5
import com.github.leeonky.util.NumberType;
6
import com.github.leeonky.util.Pair;
7

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

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

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

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

30
    private static String dump(Object value) {
31
        return value == null ? "[null]" : format("[%s: %s]", getClassName(value), value);
1✔
32
    }
33

34
    public static boolean equals(Data<?> data1, Data<?> data2) {
35
        if (data1.value() == data2.value()
1✔
36
                || opt2(data2::isNull) && opt1(data1::isNull)) return true;
1✔
37
        if (data2.isList() && data1.isList())
1✔
38
            return collect(data2, "2").equals(collect(data1, "1"));
1✔
39
        else
40
            return Objects.equals(data1.value(), data2.value());
1✔
41

42
    }
43

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

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

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

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

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

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

78
    public static Data<?> negate(Data<?> input, DALRuntimeContext context) {
79
        return input.map(value -> {
1✔
80
            if (value instanceof Number)
1✔
81
                return context.getNumberType().negate((Number) value);
1✔
82
            if (input.isList())
1✔
83
                return sortList(input.list(), reverseOrder(), context);
1✔
84
            throw illegalOp2(format("Operand should be number or list but '%s'", getClassName(value)));
1✔
85
        });
86
    }
87

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

93
    public static Data<?> positive(Data<?> input, DALRuntimeContext context) {
94
        return input.map(value -> {
1✔
95
            if (input.isList())
1✔
96
                return sortList(input.list(), naturalOrder(), context);
1✔
97
            throw illegalOp2(format("Operand should be list but '%s'", getClassName(input.value())));
1✔
98
        });
99
    }
100

101
    public static Object less(Data<?> left, DALOperator opt, Data<?> right, DALRuntimeContext context) {
102
        return compare(pair(left, right), context) < 0;
1✔
103
    }
104

105
    public static Object greaterOrEqual(Data<?> left, DALOperator opt, Data<?> right, DALRuntimeContext context) {
106
        return compare(pair(left, right), context) >= 0;
1✔
107
    }
108

109
    public static Object lessOrEqual(Data<?> left, DALOperator opt, Data<?> right, DALRuntimeContext context) {
110
        return compare(pair(left, right), context) <= 0;
1✔
111
    }
112

113
    public static Object greater(Data<?> left, DALOperator opt, Data<?> right, DALRuntimeContext context) {
114
        return compare(pair(left, right), context) > 0;
1✔
115
    }
116

117
    public static boolean notEqual(Data<?> left, Data<?> right) {
118
        return !equals(left, right);
1✔
119
    }
120
}
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