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

leeonky / test-charm-java / 308

22 Sep 2025 03:23PM UTC coverage: 71.191% (-3.5%) from 74.692%
308

push

circleci

leeonky
choose multi properties of consistency item

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

1 existing line in 1 file now uncovered.

7013 of 9851 relevant lines covered (71.19%)

0.86 hits per line

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

97.87
/jfactory/src/main/java/com/github/leeonky/jfactory/DefaultConsistency.java
1
package com.github.leeonky.jfactory;
2

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

5
import java.util.ArrayList;
6
import java.util.Collections;
7
import java.util.List;
8
import java.util.Optional;
9

10
import static com.github.leeonky.jfactory.ConsistencyItem.guessCustomerPositionStackTrace;
11
import static com.github.leeonky.util.function.Extension.not;
12
import static java.lang.String.format;
13
import static java.util.Arrays.asList;
14
import static java.util.Optional.of;
15
import static java.util.stream.Collectors.toList;
16

17
public class DefaultConsistency<T> implements Consistency<T> {
18
    private static final List<Class<?>> TYPE_PRIORITY = asList(
1✔
19
            FixedValueProducer.class,
20
            ReadOnlyProducer.class,
21
            DependencyProducer.class,
22
            UnFixedValueProducer.class
23
    );
24

25
    private final List<ConsistencyItem<T>> items = new ArrayList<>();
1✔
26
    private final BeanClass<T> type;
27
    private final List<StackTraceElement> locations = new ArrayList<>();
1✔
28

29
    public DefaultConsistency(Class<T> type) {
1✔
30
        this.type = BeanClass.create(type);
1✔
31
        locations.add(guessCustomerPositionStackTrace());
1✔
32
    }
1✔
33

34
    @Override
35
    public BeanClass<T> type() {
36
        return type;
1✔
37
    }
38

39
    @Override
40
    public Consistency<T> link(ConsistencyItem<T> item) {
41
        items.add(item);
1✔
42
        return this;
1✔
43
    }
44

45
    @Override
46
    public void apply(Producer<?> producer) {
47
        List<ConsistencyItem<T>.Resolving> resolvingList = items.stream().map(i -> i.resolving(producer)).collect(toList());
1✔
48
        guessDependency(resolvingList).ifPresent(dependency -> resolvingList.stream().filter(not(dependency::equals))
1✔
49
                .forEach(dependent -> dependent.resolve(dependency)));
1✔
50
    }
1✔
51

52
    private Optional<ConsistencyItem<T>.Resolving> guessDependency(List<ConsistencyItem<T>.Resolving> resolvingList) {
53
        resolvingList = resolvingList.stream().filter(ConsistencyItem.Resolving::hasComposer).collect(toList());
1✔
54
        for (Class<?> type : TYPE_PRIORITY)
1✔
55
            for (ConsistencyItem<T>.Resolving resolving : resolvingList)
1✔
56
                if (resolving.isProducerType(type))
1✔
57
                    return of(resolving);
1✔
58
        return resolvingList.stream().findFirst();
1✔
59
    }
60

61
    public boolean merge(DefaultConsistency<?> another) {
62
        if (items.stream().anyMatch(item -> another.items.stream().anyMatch(item::sameProperty))) {
1✔
63
            for (ConsistencyItem item : another.items)
1✔
64
                items.add(item);
1✔
65
            return true;
1✔
66
        }
67
        return false;
1✔
68
    }
69

70
    public List<ConsistencyItem<T>> items() {
UNCOV
71
        return Collections.unmodifiableList(items);
×
72
    }
73

74
    public boolean dependsOn(DefaultConsistency<?> another) {
75
        List<ConsistencyItem<?>> dependencyPair = dependencyPair(another);
1✔
76
        if (dependencyPair.isEmpty())
1✔
77
            return false;
1✔
78
        List<ConsistencyItem<?>> reversePair = another.dependencyPair(this);
1✔
79
        if (!reversePair.isEmpty())
1✔
80
            throw new ConflictConsistencyException(format("Conflict dependency between consistencies:\n%s\n%s",
1✔
81
                    dependencyPair.get(0).toTable(dependencyPair.get(1), "  "),
1✔
82
                    reversePair.get(0).toTable(reversePair.get(1), "  ")));
1✔
83
        return true;
1✔
84
    }
85

86
    private List<ConsistencyItem<?>> dependencyPair(DefaultConsistency<?> another) {
87
        for (ConsistencyItem<?> item : items)
1✔
88
            for (ConsistencyItem<?> anotherItem : another.items)
1✔
89
                if (item.dependsOn(anotherItem))
1✔
90
                    return asList(item, anotherItem);
1✔
91
        return Collections.emptyList();
1✔
92
    }
93

94
    public String info() {
95
        StringBuilder builder = new StringBuilder();
1✔
96
        builder.append("  ").append(type().getName()).append(":");
1✔
97
        for (StackTraceElement location : locations) {
1✔
98
            builder.append("\n    ").append(location.getClassName()).append(".").append(location.getMethodName())
1✔
99
                    .append("(").append(location.getFileName()).append(":").append(location.getLineNumber()).append(")");
1✔
100
        }
1✔
101
        return builder.toString();
1✔
102
    }
103
}
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