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

leeonky / test-charm-java / 132

28 Feb 2025 01:29AM UTC coverage: 74.304% (+3.0%) from 71.301%
132

push

circleci

leeonky
Allow pass test

15 of 16 new or added lines in 2 files covered. (93.75%)

16 existing lines in 3 files now uncovered.

7874 of 10597 relevant lines covered (74.3%)

0.74 hits per line

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

95.0
/DAL-java/src/main/java/com/github/leeonky/dal/DAL.java
1
package com.github.leeonky.dal;
2

3
import com.github.leeonky.dal.ast.node.DALNode;
4
import com.github.leeonky.dal.compiler.Compiler;
5
import com.github.leeonky.dal.compiler.Notations;
6
import com.github.leeonky.dal.runtime.Extension;
7
import com.github.leeonky.dal.runtime.RuntimeContextBuilder;
8
import com.github.leeonky.dal.runtime.RuntimeContextBuilder.DALRuntimeContext;
9
import com.github.leeonky.dal.type.InputCode;
10
import com.github.leeonky.dal.type.InputValue;
11
import com.github.leeonky.dal.util.TextUtil;
12
import com.github.leeonky.interpreter.SourceCode;
13
import com.github.leeonky.interpreter.SyntaxException;
14
import com.github.leeonky.util.Classes;
15

16
import java.util.*;
17
import java.util.stream.Collectors;
18

19
import static com.github.leeonky.util.Classes.subTypesOf;
20
import static com.github.leeonky.util.function.Extension.not;
21
import static java.util.Arrays.asList;
22
import static java.util.Collections.emptyList;
23
import static java.util.stream.Stream.concat;
24

25
public class DAL {
26
    private final Compiler compiler = new Compiler();
1✔
27
    private final RuntimeContextBuilder runtimeContextBuilder = new RuntimeContextBuilder();
1✔
28
    private static final ThreadLocal<DAL> instance = new ThreadLocal<>();
1✔
29
    private static final ThreadLocal<Map<String, DAL>> instances = new ThreadLocal<>();
1✔
30
    private final String name;
31

32
    public DAL() {
1✔
33
        name = String.valueOf(hashCode());
1✔
34
    }
1✔
35

36
    public static synchronized DAL getInstance() {
37
        if (instance.get() == null)
1✔
38
            instance.set(create());
1✔
39
        return instance.get();
1✔
40
    }
41

42
    public static DAL create(Class<?>... exceptExtensions) {
43
        Iterator<DALFactory> iterator = ServiceLoader.load(DALFactory.class).iterator();
1✔
44
        if (iterator.hasNext())
1✔
45
            return iterator.next().newInstance();
1✔
46
        return new DAL().extend(exceptExtensions);
1✔
47
    }
48

49
    public DAL(String name) {
1✔
50
        this.name = name;
1✔
51
    }
1✔
52

53
    public String getName() {
UNCOV
54
        return name;
×
55
    }
56

57
    public static synchronized DAL getInstance(String name) {
58
        Map<String, DAL> dalMaps = instances.get();
1✔
59
        if (dalMaps == null) {
1✔
60
            dalMaps = new HashMap<>();
1✔
61
            instances.set(dalMaps);
1✔
62
        }
63
        return dalMaps.computeIfAbsent(name, DAL::create);
1✔
64
    }
65

66
    public static DAL create(String name, Class<?>... exceptExtensions) {
67
        Iterator<DALFactory> iterator = ServiceLoader.load(DALFactory.class).iterator();
1✔
68
        if (iterator.hasNext())
1✔
UNCOV
69
            return iterator.next().newInstance();
×
70
        return new DAL(name).extend(exceptExtensions);
1✔
71
    }
72

73
    public RuntimeContextBuilder getRuntimeContextBuilder() {
74
        return runtimeContextBuilder;
1✔
75
    }
76

77
    public <T> List<T> evaluateAll(Object input, String expressions) {
78
        return evaluateAll((InputValue<Object>) () -> input, expressions);
1✔
79
    }
80

81
    @SuppressWarnings("unchecked")
82
    public <T> List<T> evaluateAll(InputCode<Object> input, String expressions) {
83
        DALRuntimeContext runtimeContext = runtimeContextBuilder.build(input);
1✔
84
        try {
85
            return compile(expressions, runtimeContext).stream()
1✔
86
                    .map(node -> (T) node.evaluate(runtimeContext))
1✔
87
                    .collect(Collectors.toList());
1✔
88
        } catch (Throwable e) {
1✔
89
            if (!runtimeContext.hookError(expressions, e))
1✔
UNCOV
90
                throw e;
×
91
            return emptyList();
1✔
92
        }
93
    }
94

95
    public <T> T evaluate(Object input, String expression) {
96
        return evaluate((InputValue<Object>) () -> input, expression);
1✔
97
    }
98

99
    public <T> T evaluate(InputCode<Object> input, String expression) {
100
        return evaluate(input, expression, null);
1✔
101
    }
102

103
    @SuppressWarnings("unchecked")
104
    public <T> T evaluate(InputCode<Object> input, String expression, Class<?> rootSchema) {
105
        DALRuntimeContext runtimeContext = runtimeContextBuilder.build(input, rootSchema);
1✔
106
        try {
107
            return (T) compileSingle(expression, runtimeContext).evaluate(runtimeContext);
1✔
108
        } catch (Throwable e) {
1✔
109
            if (!runtimeContext.hookError(expression, e))
1✔
110
                throw e;
1✔
111
            return null;
1✔
112
        }
113
    }
114

115
    public DALNode compileSingle(String expression, DALRuntimeContext runtimeContext) {
116
        List<DALNode> nodes = compile(expression, runtimeContext);
1✔
117
        if (nodes.size() > 1)
1✔
118
            throw new SyntaxException("more than one expression", getOperandPosition(nodes.get(1)));
1✔
119
        return nodes.get(0);
1✔
120
    }
121

122
    public List<DALNode> compile(String expression, DALRuntimeContext runtimeContext) {
123
        return compiler.compile(new SourceCode(format(expression), Notations.LINE_COMMENTS),
1✔
124
                runtimeContext);
125
    }
126

127
    private int getOperandPosition(DALNode node) {
128
        return node.getPositionBegin() == 0 ? node.getOperandPosition() : node.getPositionBegin();
1✔
129
    }
130

131
    private String format(String expression) {
132
        return String.join("\n", TextUtil.lines(expression));
1✔
133
    }
134

135
    public DAL extend(Class<?>... excepts) {
136
        Set<Class<?>> exceptExtensions = new HashSet<>(asList(excepts));
1✔
137
        concat(subTypesOf(Extension.class, "com.github.leeonky.dal.extensions").stream(),
1✔
138
                subTypesOf(Extension.class, "com.github.leeonky.extensions.dal").stream())
1✔
139
                .filter(not(exceptExtensions::contains))
1✔
140
                .map(Classes::newInstance)
1✔
141
                .sorted(Comparator.comparing(Extension::order))
1✔
142
                .forEach(e -> e.extend(this));
1✔
143
        return this;
1✔
144
    }
145
}
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