-
Notifications
You must be signed in to change notification settings - Fork 21
/
StrategySelector.java
64 lines (55 loc) · 2.32 KB
/
StrategySelector.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package com.struchev.invest.strategy;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import jakarta.annotation.PostConstruct;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
@Service
@RequiredArgsConstructor
@Slf4j
public class StrategySelector {
@Getter
private final List<AStrategy> allStrategies;
private List<AStrategy> activeStrategies;
public List<AStrategy> suitableByFigi(String figi, AStrategy.Type type) {
return activeStrategies.stream()
.filter(AStrategy::isEnabled)
.filter(s -> type == null || s.getType() == type)
.filter(s -> s.isSuitableByFigi(figi))
.collect(Collectors.toList());
}
public boolean isEnabled(String name, String figi) {
return activeStrategies.stream()
.filter(s -> s.getName().equals(name))
.filter(s -> figi == null || s.isSuitableByFigi(figi))
.map(AStrategy::isEnabled)
.findFirst().orElse(false);
}
public List<AStrategy> getActiveStrategies() {
return activeStrategies;
}
public Set<String> getFigiesForActiveStrategies() {
return getActiveStrategies().stream().flatMap(s -> s.getFigies().keySet().stream()).collect(Collectors.toSet());
}
public AStrategy.Type getStrategyType(String name, AStrategy.Type defaultValue) {
return allStrategies.stream()
.filter(s -> s.getName().equals(name))
.findFirst().map(AStrategy::getType).orElse(defaultValue);
}
@PostConstruct
private void init() {
activeStrategies = allStrategies.stream()
.filter(AStrategy::isEnabled)
.peek(s -> {
if (s.getType() == AStrategy.Type.instrumentByInstrument && s.getFigies().size() < 2
|| s.getType() == AStrategy.Type.instrumentByFiat && s.getFigies().size() < 1) {
throw new UnsupportedOperationException("Incorrect count of figies in " + s.getName());
}
})
.peek(s -> log.info("Enabled strategy: {}: {}", s.getName(), s.getFigies().keySet()))
.collect(Collectors.toList());
}
}