Completed
Branch master (034e5d)
by Tomasz
03:17 queued 20s
created

toDtos(ServiceVersion,Collection)   B

Complexity

Conditions 3

Size

Total Lines 54
Code Lines 42

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 42
dl 0
loc 54
c 0
b 0
f 0
cc 3
rs 8.872

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
package com.hltech.judged.server.interfaces.rest.validation;
2
3
import com.google.common.collect.Ordering;
4
import com.hltech.judged.server.domain.ServiceVersion;
5
import com.hltech.judged.server.domain.validation.EnvironmentValidatorResult;
6
7
import java.util.Collection;
8
import java.util.Comparator;
9
import java.util.List;
10
import java.util.Map;
11
import java.util.stream.Collectors;
12
13
import static com.google.common.collect.Maps.newHashMap;
14
15
public class Converters {
16
17
    private Converters() {
18
    }
19
20
    public static List<ContractValidationReportDto> toDtos(ServiceVersion serviceVersion, Collection<EnvironmentValidatorResult> validationResults) {
21
        Map<ConsumerAndProviderDto, ContractValidationReportDto> result = newHashMap();
22
        validationResults
23
            .stream()
24
            .forEach(environmentValidatorResult -> {
25
                environmentValidatorResult.getCapabilitiesValidationResults()
26
                    .stream()
27
                    .forEach(cvr -> {
28
                        ConsumerAndProviderDto key = new ConsumerAndProviderDto(cvr.getConsumerName(), cvr.getConsumerVersion(), serviceVersion.getName(), serviceVersion.getVersion());
29
                        if (!result.containsKey(key)) {
30
                            result.put(
31
                                key,
32
                                new ContractValidationReportDto(key)
33
                            );
34
                        }
35
                        result.get(key).addInteractions(
36
                            cvr.getInteractionValidationResults()
37
                                .stream()
38
                                .map(ivr -> new InteractionValidationReportDto(
39
                                    environmentValidatorResult.getCommunicationInterface(),
40
                                    ivr.getName(),
41
                                    ivr.getStatus(),
42
                                    ivr.getErrors()
43
                                ))
44
                                .collect(Collectors.toList())
45
                        );
46
                    });
47
                environmentValidatorResult.getExpectationValidationResults()
48
                    .stream()
49
                    .forEach(evr -> {
50
                        ConsumerAndProviderDto key = new ConsumerAndProviderDto(serviceVersion.getName(), serviceVersion.getVersion(), evr.getProviderName(), evr.getProviderVersion());
51
                        if (!result.containsKey(key)) {
52
                            result.put(
53
                                key,
54
                                new ContractValidationReportDto(key)
55
                            );
56
                        }
57
                        result.get(key).addInteractions(
58
                            evr.getInteractionValidationResults()
59
                                .stream()
60
                                .map(ivr -> new InteractionValidationReportDto(
61
                                    environmentValidatorResult.getCommunicationInterface(),
62
                                    ivr.getName(),
63
                                    ivr.getStatus(),
64
                                    ivr.getErrors()
65
                                ))
66
                                .collect(Collectors.toList())
67
                        );
68
                    });
69
            });
70
71
        return Ordering
72
            .from((Comparator<ContractValidationReportDto>) (o1, o2) -> o1.getConsumerAndProvider().toString().compareTo(o2.getConsumerAndProvider().toString()))
73
            .sortedCopy(result.values());
74
    }
75
}
76