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

leeonky / test-charm-java / 320

28 Sep 2025 03:56PM UTC coverage: 74.512% (+0.07%) from 74.442%
320

push

circleci

leeonky
one list one property

21 of 21 new or added lines in 2 files covered. (100.0%)

13 existing lines in 7 files now uncovered.

8466 of 11362 relevant lines covered (74.51%)

0.75 hits per line

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

90.7
/jfactory/src/main/java/com/github/leeonky/jfactory/CollectionProducer.java
1
package com.github.leeonky.jfactory;
2

3
import com.github.leeonky.util.BeanClass;
4
import com.github.leeonky.util.PropertyWriter;
5

6
import java.util.ArrayList;
7
import java.util.List;
8
import java.util.Optional;
9
import java.util.function.Function;
10

11
import static java.lang.Integer.max;
12
import static java.lang.Integer.parseInt;
13
import static java.util.stream.Collectors.toList;
14
import static java.util.stream.IntStream.range;
15

16
class CollectionProducer<T, C> extends Producer<C> {
17
    private final List<Producer<?>> children = new ArrayList<>();
1✔
18
    private final BeanClass<T> parentType;
19
    private final CollectionInstance<T> collection;
20
    private final FactorySet factorySet;
21
    private Function<Integer, Producer<?>> elementDefaultValueProducerFactory;
22

23
    public CollectionProducer(BeanClass<T> parentType, BeanClass<C> collectionType,
24
                              SubInstance<T> instance, FactorySet factorySet) {
25
        super(collectionType);
1✔
26
        this.parentType = parentType;
1✔
27
        collection = instance.inCollection();
1✔
28
        this.factorySet = factorySet;
1✔
29
        elementDefaultValueProducerFactory = index -> new DefaultValueFactoryProducer<>(parentType,
1✔
30
                factorySet.getDefaultValueFactory(collectionType.getElementType()), collection.element(index));
1✔
31
    }
1✔
32

33
    public void changeElementDefaultValueProducerFactory(Function<Integer, Producer<?>> factory) {
34
        elementDefaultValueProducerFactory = factory;
1✔
35
    }
1✔
36

37
    @Override
38
    @SuppressWarnings("unchecked")
39
    protected C produce() {
40
        return (C) getType().createCollection(children.stream().map(Producer::getValue).collect(toList()));
1✔
41
    }
42

43
    @Override
44
    public Optional<Producer<?>> child(String property) {
UNCOV
45
        int index = parseInt(property);
×
UNCOV
46
        index = transformNegativeIndex(index);
×
UNCOV
47
        return Optional.ofNullable(index < children.size() ? children.get(index) : null);
×
48
    }
49

50
    @Override
51
    public void setChild(String property, Producer<?> producer) {
52
        int index = parseInt(property);
1✔
53
        fillCollectionWithDefaultValue(index);
1✔
54
        children.set(transformNegativeIndex(index), producer);
1✔
55
    }
1✔
56

57
    private int transformNegativeIndex(int index) {
58
        if (index < 0)
1✔
59
            index = children.size() + index;
1✔
60
        return index;
1✔
61
    }
62

63
    public int fillCollectionWithDefaultValue(int index) {
64
        int changed = 0;
1✔
65
        if (index >= 0) {
1✔
66
            for (int i = children.size(); i <= index; i++, changed++)
1✔
67
                children.add(defaultElementProducer(i));
1✔
68
        } else {
69
            int count = max(children.size(), -index) - children.size();
1✔
70
            for (int i = 0; i < count; i++, changed++)
1✔
71
                children.add(i, defaultElementProducer(i));
1✔
72
        }
73
        return changed;
1✔
74
    }
75

76
    public Producer<?> defaultElementProducer(int i) {
77
        return elementDefaultValueProducerFactory.apply(i);
1✔
78
    }
79

80
    @Override
81
    public Producer<?> childOrDefault(String property) {
82
        int index = parseInt(property);
1✔
83
        fillCollectionWithDefaultValue(index);
1✔
84
        return children.get(transformNegativeIndex(index));
1✔
85
    }
86

87
    @Override
88
    protected void collectConsistent(ObjectProducer<?> root, PropertyChain base) {
89
        range(0, children.size()).forEach(i ->
1✔
90
                children.get(i).collectConsistent(root, base.concat(String.valueOf(i))));
1✔
91
    }
1✔
92

93
    @Override
94
    public Optional<Producer<?>> createPropertyDefaultValueProducer(PropertyWriter<?> property) {
95
        return factorySet.queryDefaultValueFactory(getType().getElementType()).map(builder ->
1✔
96
                new DefaultValueFactoryProducer<>(parentType, builder, collection.element(parseInt(property.getName()))));
1✔
97
    }
98

99
    @Override
100
    protected <T> void setupAssociation(String association, RootInstance<T> instance, ListPersistable cachedChildren) {
101
        children.stream().filter(ObjectProducer.class::isInstance).map(ObjectProducer.class::cast).forEach(objectProducer ->
1✔
102
                objectProducer.setupAssociation(association, instance, cachedChildren));
1✔
103

104
    }
1✔
105

106
    public int childrenCount() {
107
        return children.size();
1✔
108
    }
109

110
    @Override
111
    protected boolean isFixed() {
112
        return children.stream().anyMatch(Producer::isFixed);
×
113
    }
114
}
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