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

leeonky / test-charm-java / 156

20 Mar 2025 01:53PM UTC coverage: 74.243% (-0.2%) from 74.475%
156

push

circleci

leeonky
Refactor

14 of 15 new or added lines in 12 files covered. (93.33%)

126 existing lines in 29 files now uncovered.

7947 of 10704 relevant lines covered (74.24%)

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.util.TextUtil;
11
import com.github.leeonky.interpreter.SourceCode;
12
import com.github.leeonky.interpreter.SyntaxException;
13
import com.github.leeonky.util.Classes;
14

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

18
import static com.github.leeonky.util.Classes.subTypesOf;
19
import static com.github.leeonky.util.function.Extension.not;
20
import static java.util.Arrays.asList;
21
import static java.util.Collections.emptyList;
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<>();
1✔
29
    private final String name;
30

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

36
    @Deprecated
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
    @Deprecated
44
    public static DAL create(Class<?>... exceptExtensions) {
45
        Iterator<DALFactory> iterator = ServiceLoader.load(DALFactory.class).iterator();
1✔
46
        if (iterator.hasNext())
1✔
47
            return iterator.next().newInstance();
1✔
48
        return new DAL().extend(exceptExtensions);
1✔
49
    }
50

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

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

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

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

75
    public RuntimeContextBuilder getRuntimeContextBuilder() {
76
        return runtimeContextBuilder;
1✔
77
    }
78

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

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

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

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

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

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

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

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

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

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