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

leeonky / test-charm-java / 239

29 Apr 2025 03:10PM UTC coverage: 74.224% (+0.09%) from 74.134%
239

push

circleci

leeonky
Fill in

1 of 1 new or added line in 1 file covered. (100.0%)

5 existing lines in 2 files now uncovered.

8063 of 10863 relevant lines covered (74.22%)

0.74 hits per line

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

89.71
/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 = value();
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(value()));
1✔
56
        }
57
        return list;
1✔
58
    }
59

60
    public boolean isNull() {
61
        return context.isNull(value());
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 |
1✔
79
                     PropertyAccessException ex) {
80
                throw ex;
1✔
81
            } catch (Throwable e) {
1✔
82
                throw new PropertyAccessException(propertyChain, e);
1✔
83
            }
84
        }
85
        return property(chain);
1✔
86
    }
87

88
    public SchemaType propertySchema(Object property, boolean isListMapping) {
89
        return isListMapping ? schemaType.mappingAccess(property) : schemaType.access(property);
1✔
90
    }
91

92
    public Object firstFieldFromAlias(Object alias) {
93
        return schemaType.firstFieldFromAlias(alias);
1✔
94
    }
95

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

110
    public <N> Data<N> convert(Class<N> target) {
111
        return map(object -> context.getConverter().convert(target, object));
1✔
112
    }
113

114
    public <N> Data<N> map(Function<T, N> mapper) {
115
        return new Data<>(mapper.apply(value()), context, schemaType);
1✔
116
    }
117

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

128
    public String dump() {
129
        return DumpingBuffer.rootContext(context).dump(this).content();
1✔
130
    }
131

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

136
    public <N> N execute(Supplier<N> supplier) {
137
        return context.pushAndExecute(this, supplier);
1✔
138
    }
139

140
    public <N> Optional<N> cast(Class<N> type) {
141
        return BeanClass.cast(value(), type);
1✔
142
    }
143

144
    public boolean instanceOf(Class<?> type) {
145
        try {
146
            return type.isInstance(value());
1✔
147
        } catch (Throwable ignore) {
1✔
148
            return false;
1✔
149
        }
150
    }
151

152
    public Optional<CurryingMethod> currying(Object property) {
153
        return context.currying(value(), property);
1✔
154
    }
155

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

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

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

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

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