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

leeonky / test-charm-java / 368

12 Oct 2025 01:55PM UTC coverage: 75.301% (+0.1%) from 75.185%
368

push

circleci

leeonky
one to many

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

10 existing lines in 4 files now uncovered.

8893 of 11810 relevant lines covered (75.3%)

0.75 hits per line

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

98.57
/jfactory/src/main/java/com/github/leeonky/jfactory/ObjectFactory.java
1
package com.github.leeonky.jfactory;
2

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

5
import java.util.*;
6
import java.util.function.Consumer;
7
import java.util.function.Function;
8
import java.util.function.Supplier;
9
import java.util.regex.Matcher;
10
import java.util.regex.Pattern;
11
import java.util.stream.Collectors;
12

13
class ObjectFactory<T> implements Factory<T> {
14
    protected final FactorySet factorySet;
15
    private final BeanClass<T> type;
16
    private final Map<String, Consumer<Instance<T>>> traits = new LinkedHashMap<>();
1✔
17
    private final Map<String, Pattern> traitPatterns = new LinkedHashMap<>();
1✔
18
    private final Map<String, Transformer> transformers = new LinkedHashMap<>();
1✔
19
    private final Transformer passThrough = input -> input;
1✔
20
    private Function<Instance<T>, T> constructor = this::defaultConstruct;
1✔
21
    private Consumer<Instance<T>> spec = (instance) -> {
1✔
22
    };
1✔
23

24
    public ObjectFactory(BeanClass<T> type, FactorySet factorySet) {
1✔
25
        this.type = type;
1✔
26
        this.factorySet = factorySet;
1✔
27
    }
1✔
28

29
    @SuppressWarnings("unchecked")
30
    private T defaultConstruct(Instance<T> instance) {
31
        return getType().isCollection()
1✔
32
                ? (T) getType().createCollection(Collections.nCopies(instance.collectionSize(), getType().getElementType().createDefault()))
1✔
33
                : getType().newInstance();
1✔
34
    }
35

36
    protected Spec<T> createSpec() {
37
        return new Spec<>();
1✔
38
    }
39

40
    @Override
41
    public final Factory<T> constructor(Function<Instance<T>, T> constructor) {
42
        this.constructor = Objects.requireNonNull(constructor);
1✔
43
        return this;
1✔
44
    }
45

46
    @Override
47
    public Factory<T> spec(Consumer<Instance<T>> spec) {
48
        this.spec = Objects.requireNonNull(spec);
1✔
49
        return this;
1✔
50
    }
51

52
    @Override
53
    public Factory<T> spec(String name, Consumer<Instance<T>> trait) {
54
        traits.put(name, Objects.requireNonNull(trait));
1✔
55
        traitPatterns.put(name, Pattern.compile(name));
1✔
56
        return this;
1✔
57
    }
58

59
    public final T create(Instance<T> instance) {
60
        instance.getSequence();
1✔
61
        return constructor.apply(instance);
1✔
62
    }
63

64
    @Override
65
    public BeanClass<T> getType() {
66
        return type;
1✔
67
    }
68

69
    @Override
70
    public Factory<T> transformer(String property, Transformer transformer) {
71
        transformers.put(property, transformer);
1✔
72
        return this;
1✔
73
    }
74

75
    private static class TraitExecutor<T> {
76
        private final Consumer<Instance<T>> action;
77
        private final List<Object> args = new ArrayList<>();
1✔
78

79
        public TraitExecutor(Matcher matcher, Consumer<Instance<T>> action) {
1✔
80
            for (int i = 0; i < matcher.groupCount(); i++)
1✔
81
                args.add(matcher.group(i + 1));
1✔
82
            this.action = action;
1✔
83
        }
1✔
84

85
        public TraitExecutor(Consumer<Instance<T>> action) {
1✔
86
            this.action = action;
1✔
87
        }
1✔
88

89
        public void execute(Instance<T> instance) {
90
            ((RootInstance<T>) instance).runTraitWithParams(args.toArray(), action);
1✔
91
        }
1✔
92
    }
93

94
    public void collectSpec(Collection<String> traits, RootInstance<T> instance) {
95
        spec.accept(instance);
1✔
96
        collectSubSpec(instance);
1✔
97
        for (String name : traits)
1✔
98
            queryTrait(name).execute(instance);
1✔
99
    }
1✔
100

101
    private TraitExecutor<T> queryTrait(String name) {
102
        Consumer<Instance<T>> action = traits.get(name);
1✔
103
        if (action != null)
1✔
104
            return new TraitExecutor<>(action);
1✔
105
        List<Matcher> matchers = traitPatterns.values().stream().map(p -> p.matcher(name)).filter(Matcher::matches)
1✔
106
                .collect(Collectors.toList());
1✔
107
        if (matchers.size() == 1)
1✔
108
            return new TraitExecutor<>(matchers.get(0), traits.get(matchers.get(0).pattern().pattern()));
1✔
109
        if (matchers.size() > 1)
1✔
110
            throw new IllegalArgumentException(String.format("Ambiguous trait pattern: %s, candidates are:\n%s", name,
1✔
111
                    matchers.stream().map(p -> "  " + p.pattern().pattern()).collect(Collectors.joining("\n"))));
1✔
112
        throw new IllegalArgumentException("Trait `" + name + "` not exist");
1✔
113
    }
114

115
    protected void collectSubSpec(RootInstance<T> instance) {
116
    }
1✔
117

118
    public RootInstance<T> createInstance(Arguments argument, Optional<Association> association,
119
                                          Optional<ReverseAssociation> reverseAssociation,
120
                                          ObjectProducer<?> objectProducer) {
121
        Spec<T> spec = createSpec();
1✔
122
        spec.association = association;
1✔
123
        spec.reverseAssociation = reverseAssociation;
1✔
124
        spec.objectProducer = objectProducer;
1✔
125
        RootInstance<T> rootInstance = new RootInstance<>(spec, argument,
1✔
126
                factorySet.sequence(getType().getType()));
1✔
127
        spec.setInstance(rootInstance);
1✔
128
        return rootInstance;
1✔
129
    }
130

131
    public FactorySet getFactorySet() {
132
        return factorySet;
1✔
133
    }
134

135
    public ObjectFactory<T> getBase() {
UNCOV
136
        return this;
×
137
    }
138

139
    public Object transform(String name, Object value) {
140
        return queryTransformer(name, () -> passThrough).checkAndTransform(value);
1✔
141
    }
142

143
    protected Transformer queryTransformer(String name, Supplier<Transformer> fallback) {
144
        return transformers.getOrDefault(name, fallback(name, fallback).get());
1✔
145
    }
146

147
    protected Supplier<Transformer> fallback(String name, Supplier<Transformer> fallback) {
148
        return () -> getType().getType().getSuperclass() == null ? fallback.get()
1✔
149
                : factorySet.queryObjectFactory(BeanClass.create(getType().getType().getSuperclass()))
1✔
150
                .queryTransformer(name, fallback);
1✔
151
    }
152
}
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