1
|
|
|
package com.hltech.judged.server.domain.contracts; |
2
|
|
|
|
3
|
|
|
import com.hltech.judged.server.domain.ServiceId; |
4
|
|
|
import lombok.EqualsAndHashCode; |
5
|
|
|
import lombok.Getter; |
6
|
|
|
import lombok.ToString; |
7
|
|
|
import lombok.experimental.Delegate; |
8
|
|
|
|
9
|
|
|
import java.time.Instant; |
10
|
|
|
import java.util.List; |
11
|
|
|
import java.util.Map; |
12
|
|
|
import java.util.Optional; |
13
|
|
|
import java.util.function.Function; |
14
|
|
|
|
15
|
|
|
import static java.util.stream.Collectors.toMap; |
16
|
|
|
|
17
|
|
|
@Getter |
18
|
|
|
@ToString |
19
|
|
|
@EqualsAndHashCode(exclude = "publicationTime") |
20
|
|
|
public class ServiceContracts { |
21
|
|
|
|
22
|
|
|
@Delegate |
23
|
|
|
private final ServiceId id; |
24
|
|
|
private final List<Capability> capabilities; |
25
|
|
|
private final List<Expectation> expectations; |
26
|
|
|
private final Instant publicationTime; |
27
|
|
|
|
28
|
|
|
public ServiceContracts(ServiceId id, List<Capability> capabilities, List<Expectation> expectations) { |
29
|
|
|
this.id = id; |
30
|
|
|
this.capabilities = capabilities; |
31
|
|
|
this.expectations = expectations; |
32
|
|
|
this.publicationTime = Instant.now(); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
public ServiceContracts(ServiceId id, List<Capability> capabilities, List<Expectation> expectations, Instant publicationTime) { |
36
|
|
|
this.id = id; |
37
|
|
|
this.capabilities = capabilities; |
38
|
|
|
this.expectations = expectations; |
39
|
|
|
this.publicationTime = publicationTime; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
public <C> Optional<C> getMappedCapabilities(String communicationInterface, Function<String, C> deserializer) { |
43
|
|
|
return this.capabilities.stream() |
44
|
|
|
.filter(capability -> capability.getProtocol().equals(communicationInterface)) |
45
|
|
|
.findAny() |
46
|
|
|
.map(capability -> capability.getContract().getValue()) |
47
|
|
|
.map(deserializer); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
public <E> Optional<E> getMappedExpectations(String providerName, String communicationInterface, Function<String, E> deserializer) { |
51
|
|
|
return this.expectations.stream() |
52
|
|
|
.filter(expectation -> expectation.getProvider().equals(providerName)) |
53
|
|
|
.filter(expectation -> expectation.getProtocol().equals(communicationInterface)) |
54
|
|
|
.findAny() |
55
|
|
|
.map(expectation -> expectation.getContract().getValue()) |
56
|
|
|
.map(deserializer); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
public <E> Map<String, E> getMappedExpectations(String communicationInterface, Function<String, E> deserializer) { |
60
|
|
|
return this.expectations.stream() |
61
|
|
|
.filter(expectation -> expectation.getProtocol().equals(communicationInterface)) |
62
|
|
|
.collect(toMap( |
63
|
|
|
Expectation::getProvider, |
64
|
|
|
expectation -> deserializer.apply(expectation.getContract().getValue()) |
65
|
|
|
)); |
66
|
|
|
} |
67
|
|
|
} |
68
|
|
|
|