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

leeonky / test-charm-java / 218

15 Apr 2025 02:45PM UTC coverage: 74.045% (-0.04%) from 74.088%
218

push

circleci

leeonky
Introduce adaptiveList

20 of 21 new or added lines in 3 files covered. (95.24%)

34 existing lines in 11 files now uncovered.

7968 of 10761 relevant lines covered (74.05%)

0.74 hits per line

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

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

3
import com.github.leeonky.dal.runtime.RuntimeContextBuilder.DALRuntimeContext;
4
import com.github.leeonky.dal.runtime.inspector.DumpingBuffer;
5
import com.github.leeonky.interpreter.InterpreterException;
6
import com.github.leeonky.util.BeanClass;
7
import com.github.leeonky.util.ConvertException;
8

9
import java.util.Comparator;
10
import java.util.List;
11
import java.util.Optional;
12
import java.util.Set;
13
import java.util.function.Function;
14
import java.util.function.Supplier;
15

16
import static com.github.leeonky.dal.ast.node.SortGroupNode.NOP_COMPARATOR;
17
import static com.github.leeonky.dal.runtime.ExpressionException.illegalOperation;
18
import static java.lang.String.format;
19
import static java.util.stream.Collectors.toList;
20

21
public class Data<T> {
22
    private final SchemaType schemaType;
23
    private final DALRuntimeContext context;
24
    private final T value;
25
    private DataList list;
26

27
    public Data(T value, DALRuntimeContext context, SchemaType schemaType) {
1✔
28
        this.context = context;
1✔
29
        this.schemaType = schemaType;
1✔
30
        this.value = value;
1✔
31
    }
1✔
32

33
    @Deprecated
34
    public T instance() {
35
        return value;
1✔
36
    }
37

38
    public T value() {
39
        return value;
1✔
40
    }
41

42
    public Set<?> fieldNames() {
43
        return context.findPropertyReaderNames(this);
1✔
44
    }
45

46
    public boolean isList() {
47
        Object instance = instance();
1✔
48
        return context.isRegisteredList(instance) || (instance != null && instance.getClass().isArray());
1✔
49
    }
50

51
    public DataList list() {
52
        if (list == null) {
1✔
53
            if (!isList())
1✔
54
                throw new DalRuntimeException(format("Invalid input value, expect a List but: %s", dump().trim()));
1✔
55
            list = new DataList(context.createCollection(instance()));
1✔
56
        }
57
        return list;
1✔
58
    }
59

60
    public boolean isNull() {
61
        return context.isNull(instance());
1✔
62
    }
63

64
    public Data<?> property(List<Object> propertyChain) {
65
        return propertyChain.isEmpty() ? this :
1✔
66
                property(propertyChain.get(0)).property(propertyChain.subList(1, propertyChain.size()));
1✔
67
    }
68

69
    public Data<?> property(Object propertyChain) {
70
        List<Object> chain = schemaType.access(propertyChain).getPropertyChainBefore(schemaType);
1✔
71
        if (chain.size() == 1 && chain.get(0).equals(propertyChain)) {
1✔
72
            try {
73
                return isList() && !(propertyChain instanceof String)
1✔
74
                        ? new Data<>(list().getByIndex((int) propertyChain), context, schemaType.access(propertyChain))
1✔
75
                        : context.accessProperty(this, propertyChain);
1✔
76
            } catch (IndexOutOfBoundsException ex) {
1✔
77
                throw new DalRuntimeException(ex.getMessage());
1✔
78
            } catch (ListMappingElementAccessException | ExpressionException | InterpreterException ex) {
1✔
79
                throw ex;
1✔
80
            } catch (Throwable e) {
1✔
81
                throw new DalRuntimeException(format("Get property `%s` failed, property can be:\n" +
1✔
82
                        "  1. public field\n" +
83
                        "  2. public getter\n" +
84
                        "  3. public method\n" +
85
                        "  4. Map key value\n" +
86
                        "  5. customized type getter\n" +
87
                        "  6. static method extension", propertyChain), e);
88
            }
89
        }
90
        return property(chain);
1✔
91
    }
92

93
    public SchemaType propertySchema(Object property, boolean isListMapping) {
94
        return isListMapping ? schemaType.mappingAccess(property) : schemaType.access(property);
1✔
95
    }
96

97
    public Object firstFieldFromAlias(Object alias) {
98
        return schemaType.firstFieldFromAlias(alias);
1✔
99
    }
100

101
    public Data<?> tryConvert(Class<?>... targets) {
UNCOV
102
        return map(object -> {
×
UNCOV
103
            ConvertException e = null;
×
UNCOV
104
            for (Class<?> target : targets) {
×
105
                try {
UNCOV
106
                    return context.getConverter().convert(target, object);
×
UNCOV
107
                } catch (ConvertException convertException) {
×
UNCOV
108
                    e = convertException;
×
109
                }
110
            }
UNCOV
111
            throw e;
×
112
        });
113
    }
114

115
    public <N> Data<N> convert(Class<N> target) {
116
        return map(object -> context.getConverter().convert(target, object));
1✔
117
    }
118

119
    public <N> Data<N> map(Function<T, N> mapper) {
120
        return new Data<>(mapper.apply(instance()), context, schemaType);
1✔
121
    }
122

123
    public Data<?> filter(String prefix) {
124
        FilteredObject filteredObject = new FilteredObject();
1✔
125
        fieldNames().stream().filter(String.class::isInstance).map(String.class::cast)
1✔
126
                .filter(field -> field.startsWith(prefix)).forEach(fieldName ->
1✔
127
                        filteredObject.put(fieldName.substring(prefix.length()), property(fieldName).instance()));
1✔
128
        return new Data<>(filteredObject, context, schemaType);
1✔
129
    }
130

131
    public String dump() {
132
        return DumpingBuffer.rootContext(context).dump(this).content();
1✔
133
    }
134

135
    public String dumpValue() {
136
        return DumpingBuffer.rootContext(context).dumpValue(this).content();
1✔
137
    }
138

139
    public <N> N execute(Supplier<N> supplier) {
140
        return context.pushAndExecute(this, supplier);
1✔
141
    }
142

143
    public <N> Optional<N> cast(Class<N> type) {
144
        return BeanClass.cast(instance(), type);
1✔
145
    }
146

147
    public boolean instanceOf(Class<?> type) {
148
        try {
149
            return type.isInstance(instance());
1✔
150
        } catch (Throwable ignore) {
1✔
151
            return false;
1✔
152
        }
153
    }
154

155
    public class DataList extends DALCollection.Decorated<Object> {
156
        public DataList(DALCollection<Object> origin) {
1✔
157
            super(origin);
1✔
158
        }
1✔
159

160
        public DALCollection<Data<?>> wraps() {
161
            return map((index, e) -> new Data<>(e, context, schemaType.access(index)));
1✔
162
        }
163

164
        public AutoMappingList autoMapping(Function<Data<?>, Data<?>> mapper) {
165
            return new AutoMappingList(mapper, wraps());
1✔
166
        }
167

168
        public DataList sort(Comparator<Data<?>> comparator) {
169
            if (comparator != NOP_COMPARATOR)
1✔
170
                try {
171
                    return new DataList(new CollectionDALCollection<Object>(wraps().collect().stream()
1✔
172
                            .sorted(comparator).map(Data::instance).collect(toList())) {
1✔
173
                        @Override
174
                        public int firstIndex() {
175
                            return DataList.this.firstIndex();
1✔
176
                        }
177

178
                        @Override
179
                        public boolean infinite() {
180
                            return DataList.this.infinite();
1✔
181
                        }
182
                    });
183
                } catch (InfiniteCollectionException e) {
1✔
184
                    throw illegalOperation("Can not sort infinite collection");
1✔
185
                }
186
            return this;
1✔
187
        }
188
    }
189
}
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