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

leeonky / test-charm-java / 332

02 Oct 2025 03:38PM UTC coverage: 74.653% (-0.03%) from 74.68%
332

push

circleci

leeonky
Refactor

10 of 10 new or added lines in 3 files covered. (100.0%)

16 existing lines in 5 files now uncovered.

8559 of 11465 relevant lines covered (74.65%)

0.75 hits per line

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

93.18
/jfactory/src/main/java/com/github/leeonky/jfactory/FactorySet.java
1
package com.github.leeonky.jfactory;
2

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

7
import java.util.*;
8
import java.util.function.Consumer;
9

10
class FactorySet {
1✔
11
    public final TypeSequence typeSequence = new TypeSequence();
1✔
12
    private final DefaultValueFactories defaultValueFactories = new DefaultValueFactories();
1✔
13
    private final Map<BeanClass<?>, ObjectFactory<?>> objectFactories = new HashMap<>();
1✔
14
    private final Map<Class<?>, SpecClassFactory<?>> specClassFactoriesWithType = new HashMap<>();
1✔
15
    private final Map<String, SpecClassFactory<?>> specClassFactoriesWithName = new LinkedHashMap<>();
1✔
16

17
    @SuppressWarnings("unchecked")
18
    public <T> ObjectFactory<T> queryObjectFactory(BeanClass<T> type) {
19
        return (ObjectFactory<T>) objectFactories.computeIfAbsent(type,
1✔
20
                key -> new ObjectFactory<>(key, this));
1✔
21
    }
22

23
    public <T, S extends Spec<T>> void registerSpecClassFactory(Class<S> specClass) {
24
        Spec<T> spec = Classes.newInstance(specClass);
1✔
25
        boolean globalSpec = isGlobalSpec(specClass);
1✔
26
        SpecClassFactory<?> specClassFactory = specClassFactoriesWithType.computeIfAbsent(specClass,
1✔
27
                type -> new SpecClassFactory<>(specClass, this, globalSpec));
1✔
28
        specClassFactoriesWithName.put(spec.getName(), specClassFactory);
1✔
29
        if (globalSpec)
1✔
30
            registerGlobalSpec(specClassFactory);
1✔
31
        if (!specClass.getSuperclass().equals(Spec.class))
1✔
32
            registerSpecClassFactory((Class<S>) specClass.getSuperclass());
1✔
33
    }
1✔
34

35
    private <T, S extends Spec<T>> boolean isGlobalSpec(Class<S> specClass) {
36
        return specClass.getAnnotation(Global.class) != null;
1✔
37
    }
38

39
    private <T> void registerGlobalSpec(SpecClassFactory<?> specClassFactory) {
40
        Class<? extends Spec<?>> specClass = specClassFactory.getSpecClass();
1✔
41
        if (specClassFactory.getBase() instanceof SpecClassFactory)
1✔
42
            throw new IllegalArgumentException(String.format("More than one @Global Spec class `%s` and `%s`",
1✔
43
                    ((SpecClassFactory<?>) specClassFactory.getBase()).getSpecClass().getName(), specClass.getName()));
1✔
44
        if (!specClass.getSuperclass().equals(Spec.class))
1✔
45
            throw new IllegalArgumentException(String.format("Global Spec %s should not have super Spec %s.",
1✔
46
                    specClass.getName(), specClass.getSuperclass().getName()));
1✔
47
        objectFactories.put(specClassFactory.getType(), specClassFactory);
1✔
48
    }
1✔
49

50
    public void removeGlobalSpec(BeanClass<?> type) {
51
        objectFactories.computeIfPresent(type, (key, factory) -> factory.getBase());
1✔
52
    }
1✔
53

54
    @SuppressWarnings("unchecked")
55
    public <T> SpecClassFactory<T> querySpecClassFactory(String specName) {
56
        return (SpecClassFactory<T>) specClassFactoriesWithName.computeIfAbsent(specName, key -> {
1✔
57
            throw new IllegalArgumentException("Spec `" + specName + "` not exist");
1✔
58
        });
59
    }
60

61
    @SuppressWarnings("unchecked")
62
    public <T> SpecClassFactory<T> querySpecClassFactory(Class<? extends Spec<T>> specClass) {
63
        return (SpecClassFactory<T>) specClassFactoriesWithType.computeIfAbsent(specClass, key -> {
1✔
UNCOV
64
            throw new IllegalArgumentException("Spec `" + specClass.getName() + "` not exist");
×
65
        });
66
    }
67

68
    public <T> Optional<DefaultValueFactory<T>> queryDefaultValueFactory(BeanClass<T> type) {
69
        return defaultValueFactories.query(type.getType());
1✔
70
    }
71

72
    public <T> DefaultValueFactory<T> getDefaultValueFactory(BeanClass<T> type) {
UNCOV
73
        return queryDefaultValueFactory(type).orElseGet(() -> new DefaultValueFactories.DefaultTypeFactory<>(type));
×
74
    }
75

76
    public TypeSequence.Sequence sequence(Class<?> type) {
77
        return typeSequence.register(type);
1✔
78
    }
79

80
    public <T, S extends Spec<T>> SpecFactory<T, S> createSpecFactory(Class<S> specClass, Consumer<S> trait) {
81
        return new SpecFactory<>(Classes.newInstance(specClass), this, trait);
1✔
82
    }
83

84
    public <T> void registerDefaultValueFactory(Class<T> type, DefaultValueFactory<T> factory) {
85
        defaultValueFactories.register(type, factory);
1✔
86
    }
1✔
87

88
    public Set<String> specNames() {
UNCOV
89
        return new HashSet<>(specClassFactoriesWithName.keySet());
×
90
    }
91

92
    public <T> Optional<Producer<?>> newDefaultValueFactoryProducer(PropertyWriter<T> property, RootInstance<T> instance) {
93
        return newDefaultValueFactoryProducer(property.getBeanType(), property, instance);
1✔
94
    }
95

96
    public <T> Optional<Producer<?>> newDefaultValueFactoryProducer(BeanClass<T> beanType, PropertyWriter<?> property, RootInstance<T> instance) {
97
        return queryDefaultValueFactory(property.getType()).map(valueFactory ->
1✔
98
                new DefaultValueFactoryProducer<>(beanType, valueFactory, instance.sub(property)));
1✔
99
    }
100
}
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