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

leeonky / test-charm-java / 117

26 Feb 2025 03:01AM UTC coverage: 74.16% (+0.9%) from 73.251%
117

push

circleci

leeonky
Update version

7838 of 10569 relevant lines covered (74.16%)

0.74 hits per line

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

80.36
/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.stream.Stream.concat;
23

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

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

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

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

50
    public DAL(String name) {
×
51
        this.name = name;
×
52
    }
×
53

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

58
    public static synchronized DAL getInstance(String name) {
59
        return instances.get().computeIfAbsent(name, DAL::create);
×
60
    }
61

62
    public static DAL create(String name, Class<?>... exceptExtensions) {
63
        Iterator<DALFactory> iterator = ServiceLoader.load(DALFactory.class).iterator();
×
64
        if (iterator.hasNext())
×
65
            return iterator.next().newInstance();
×
66
        return new DAL(name).extend(exceptExtensions);
×
67
    }
68

69
    public RuntimeContextBuilder getRuntimeContextBuilder() {
70
        return runtimeContextBuilder;
1✔
71
    }
72

73
    public <T> List<T> evaluateAll(Object input, String expressions) {
74
        return evaluateAll((InputValue<Object>) () -> input, expressions);
1✔
75
    }
76

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

90
    public <T> T evaluate(Object input, String expression) {
91
        return evaluate((InputValue<Object>) () -> input, expression);
1✔
92
    }
93

94
    public <T> T evaluate(InputCode<Object> input, String expression) {
95
        return evaluate(input, expression, null);
1✔
96
    }
97

98
    @SuppressWarnings("unchecked")
99
    public <T> T evaluate(InputCode<Object> input, String expression, Class<?> rootSchema) {
100
        DALRuntimeContext runtimeContext = runtimeContextBuilder.build(input, rootSchema);
1✔
101
        try {
102
            return (T) compileSingle(expression, runtimeContext).evaluate(runtimeContext);
1✔
103
        } catch (Throwable e) {
1✔
104
            runtimeContext.hookError(expression, e);
1✔
105
            throw e;
1✔
106
        }
107
    }
108

109
    public DALNode compileSingle(String expression, DALRuntimeContext runtimeContext) {
110
        List<DALNode> nodes = compile(expression, runtimeContext);
1✔
111
        if (nodes.size() > 1)
1✔
112
            throw new SyntaxException("more than one expression", getOperandPosition(nodes.get(1)));
1✔
113
        return nodes.get(0);
1✔
114
    }
115

116
    public List<DALNode> compile(String expression, DALRuntimeContext runtimeContext) {
117
        return compiler.compile(new SourceCode(format(expression), Notations.LINE_COMMENTS),
1✔
118
                runtimeContext);
119
    }
120

121
    private int getOperandPosition(DALNode node) {
122
        return node.getPositionBegin() == 0 ? node.getOperandPosition() : node.getPositionBegin();
1✔
123
    }
124

125
    private String format(String expression) {
126
        return String.join("\n", TextUtil.lines(expression));
1✔
127
    }
128

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