Skip to content

Commit ca7e6db

Browse files
committed
refactor: clean up
1 parent ee1a5a1 commit ca7e6db

File tree

3 files changed

+56
-66
lines changed

3 files changed

+56
-66
lines changed

backend/src/main/java/ch/xxx/trader/usecase/services/CoinbaseService.java

+21-29
Original file line numberDiff line numberDiff line change
@@ -329,39 +329,31 @@ private GetSetMethodFunctions createGetMethodFunction(PropertyDescriptor propert
329329
if (gsmf == null) {
330330
final MethodHandles.Lookup lookupGetter = MethodHandles.lookup();
331331
final MethodHandles.Lookup lookupSetter = MethodHandles.lookup();
332-
Method getterMethod = null;
333-
Method setterMethod = null;
334-
if ("1Inch".equalsIgnoreCase(propertyDescriptor.getName())) {
335-
getterMethod = Stream.of(QuoteCb.class.getMethods())
336-
.filter(myMethod -> myMethod.getName().equalsIgnoreCase("get1Inch")).findFirst()
337-
.orElseThrow();
338-
setterMethod = Stream.of(QuoteCb.class.getMethods())
339-
.filter(myMethod -> myMethod.getName().equalsIgnoreCase("set1Inch")).findFirst()
340-
.orElseThrow();
341-
} else if ("super".equalsIgnoreCase(propertyDescriptor.getName())) {
342-
getterMethod = Stream.of(QuoteCb.class.getMethods())
343-
.filter(myMethod -> myMethod.getName().equalsIgnoreCase("getSuper")).findFirst()
344-
.orElseThrow();
345-
setterMethod = Stream.of(QuoteCb.class.getMethods())
346-
.filter(myMethod -> myMethod.getName().equalsIgnoreCase("setSuper")).findFirst()
347-
.orElseThrow();
348-
} else if ("try".equalsIgnoreCase(propertyDescriptor.getName())) {
349-
getterMethod = Stream.of(QuoteCb.class.getMethods())
350-
.filter(myMethod -> myMethod.getName().equalsIgnoreCase("getTry1")).findFirst()
351-
.orElseThrow();
352-
setterMethod = Stream.of(QuoteCb.class.getMethods())
353-
.filter(myMethod -> myMethod.getName().equalsIgnoreCase("setTry1")).findFirst()
354-
.orElseThrow();
355-
} else {
356-
getterMethod = propertyDescriptor.getReadMethod();
357-
setterMethod = propertyDescriptor.getWriteMethod();
358-
}
332+
record GetSetMethods(Method getterMethod, Method setterMethod) { }
333+
var result = switch(propertyDescriptor.getName().toLowerCase()) {
334+
case "1inch" -> new GetSetMethods(Stream.of(QuoteCb.class.getMethods())
335+
.filter(myMethod -> myMethod.getName().equalsIgnoreCase("get1Inch")).findFirst()
336+
.orElseThrow(), Stream.of(QuoteCb.class.getMethods())
337+
.filter(myMethod -> myMethod.getName().equalsIgnoreCase("set1Inch")).findFirst()
338+
.orElseThrow());
339+
case "super" -> new GetSetMethods(Stream.of(QuoteCb.class.getMethods())
340+
.filter(myMethod -> myMethod.getName().equalsIgnoreCase("getSuper")).findFirst()
341+
.orElseThrow(),Stream.of(QuoteCb.class.getMethods())
342+
.filter(myMethod -> myMethod.getName().equalsIgnoreCase("setSuper")).findFirst()
343+
.orElseThrow());
344+
case "try" -> new GetSetMethods(Stream.of(QuoteCb.class.getMethods())
345+
.filter(myMethod -> myMethod.getName().equalsIgnoreCase("getTry1")).findFirst()
346+
.orElseThrow(),Stream.of(QuoteCb.class.getMethods())
347+
.filter(myMethod -> myMethod.getName().equalsIgnoreCase("setTry1")).findFirst()
348+
.orElseThrow());
349+
default -> new GetSetMethods(propertyDescriptor.getReadMethod(), propertyDescriptor.getWriteMethod());
350+
};
359351
@SuppressWarnings("unchecked")
360352
Function<QuoteCb, BigDecimal> getterFunction = (Function<QuoteCb, BigDecimal>) DtoUtils
361-
.createGetter(lookupGetter, lookupGetter.unreflect(getterMethod));
353+
.createGetter(lookupGetter, lookupGetter.unreflect(result.getterMethod()));
362354
@SuppressWarnings("unchecked")
363355
BiConsumer<QuoteCb, BigDecimal> setterFunction = DtoUtils.createSetter(lookupSetter,
364-
lookupSetter.unreflect(setterMethod));
356+
lookupSetter.unreflect(result.setterMethod()));
365357
cbFunctionCache.put(propertyDescriptor.getName(), new GetSetMethodFunctions(getterFunction,
366358
setterFunction, propertyDescriptor.getName(), propertyDescriptor));
367359
gsmf = cbFunctionCache.get(propertyDescriptor.getName());

backend/src/main/java/ch/xxx/trader/usecase/services/StatisticService.java

+9-9
Original file line numberDiff line numberDiff line change
@@ -57,14 +57,14 @@ private Mono<CommonStatisticsDto> createBitStampStatistics(StatisticsCurrPair cu
5757
Mono<CommonStatisticsDto> result = this.myMongoRepository
5858
.find(MongoUtils.buildTimeFrameQuery(Optional.of(currPair.getBitStampKey()), TimeFrame.Year5, 5000),
5959
QuoteBs.class, BitstampService.BS_DAY_COL)
60-
.collectList().flatMap(myList -> this.calcStatistics(myList)).map(value -> {
60+
.collectList().flatMap(StatisticService::calcStatistics).map(value -> {
6161
value.setCurrPair(currPair);
6262
return value;
6363
});
6464
return result;
6565
}
6666

67-
private <T extends Quote> Mono<CommonStatisticsDto> calcStatistics(List<T> quotes) {
67+
private static <T extends Quote> Mono<CommonStatisticsDto> calcStatistics(List<T> quotes) {
6868
CommonStatisticsDto commonStatisticsDto = new CommonStatisticsDto();
6969
calcStatistics1Month(quotes, commonStatisticsDto);
7070
calcStatistics3Months(quotes, commonStatisticsDto);
@@ -75,7 +75,7 @@ private <T extends Quote> Mono<CommonStatisticsDto> calcStatistics(List<T> quote
7575
return Mono.just(commonStatisticsDto);
7676
}
7777

78-
<T extends Quote> void calcStatistics5Years(List<T> quotes, CommonStatisticsDto commonStatisticsDto) {
78+
static <T extends Quote> void calcStatistics5Years(List<T> quotes, CommonStatisticsDto commonStatisticsDto) {
7979
List<T> quotes5Year = quotes.stream()
8080
.filter(myQuote -> myQuote.getCreatedAt().after(StatisticService.createBeforeDate(0, 5))).toList();
8181
commonStatisticsDto.setRange5Year(
@@ -85,7 +85,7 @@ <T extends Quote> void calcStatistics5Years(List<T> quotes, CommonStatisticsDto
8585
commonStatisticsDto.setVolatility5Year(StatisticService.calcVolatility(quotes5Year));
8686
}
8787

88-
<T extends Quote> void calcStatistics2Years(List<T> quotes, CommonStatisticsDto commonStatisticsDto) {
88+
static <T extends Quote> void calcStatistics2Years(List<T> quotes, CommonStatisticsDto commonStatisticsDto) {
8989
List<T> quotes2Year = quotes.stream()
9090
.filter(myQuote -> myQuote.getCreatedAt().after(StatisticService.createBeforeDate(0, 2))).toList();
9191
commonStatisticsDto.setRange2Year(
@@ -95,7 +95,7 @@ <T extends Quote> void calcStatistics2Years(List<T> quotes, CommonStatisticsDto
9595
commonStatisticsDto.setVolatility2Year(StatisticService.calcVolatility(quotes2Year));
9696
}
9797

98-
<T extends Quote> void calcStatistics1Year(List<T> quotes, CommonStatisticsDto commonStatisticsDto) {
98+
static <T extends Quote> void calcStatistics1Year(List<T> quotes, CommonStatisticsDto commonStatisticsDto) {
9999
List<T> quotes1Year = quotes.stream()
100100
.filter(myQuote -> myQuote.getCreatedAt().after(StatisticService.createBeforeDate(0, 1))).toList();
101101
commonStatisticsDto.setRange1Year(
@@ -105,7 +105,7 @@ <T extends Quote> void calcStatistics1Year(List<T> quotes, CommonStatisticsDto c
105105
commonStatisticsDto.setVolatility1Year(StatisticService.calcVolatility(quotes1Year));
106106
}
107107

108-
<T extends Quote> void calcStatistics6Months(List<T> quotes, CommonStatisticsDto commonStatisticsDto) {
108+
static <T extends Quote> void calcStatistics6Months(List<T> quotes, CommonStatisticsDto commonStatisticsDto) {
109109
List<T> quotes6Month = quotes.stream()
110110
.filter(myQuote -> myQuote.getCreatedAt().after(StatisticService.createBeforeDate(6, 0))).toList();
111111
commonStatisticsDto.setPerformance6Month(StatisticService.calcPerformance(quotes6Month));
@@ -115,7 +115,7 @@ <T extends Quote> void calcStatistics6Months(List<T> quotes, CommonStatisticsDto
115115
new RangeDto(StatisticService.getMinMaxValue(quotes6Month, false), StatisticService.getMinMaxValue(quotes6Month, true)));
116116
}
117117

118-
<T extends Quote> void calcStatistics3Months(List<T> quotes, CommonStatisticsDto commonStatisticsDto) {
118+
static <T extends Quote> void calcStatistics3Months(List<T> quotes, CommonStatisticsDto commonStatisticsDto) {
119119
List<T> quotes3Month = quotes.stream()
120120
.filter(myQuote -> myQuote.getCreatedAt().after(StatisticService.createBeforeDate(3, 0))).toList();
121121
commonStatisticsDto.setRange3Month(
@@ -125,7 +125,7 @@ <T extends Quote> void calcStatistics3Months(List<T> quotes, CommonStatisticsDto
125125
commonStatisticsDto.setVolatility3Month(StatisticService.calcVolatility(quotes3Month));
126126
}
127127

128-
<T extends Quote> void calcStatistics1Month(List<T> quotes, CommonStatisticsDto commonStatisticsDto) {
128+
static <T extends Quote> void calcStatistics1Month(List<T> quotes, CommonStatisticsDto commonStatisticsDto) {
129129
Date beforeDate = StatisticService.createBeforeDate(1, 0);
130130
List<T> quotes1Month = quotes.stream()
131131
.filter(myQuote -> myQuote.getCreatedAt().after(beforeDate)).toList();
@@ -183,7 +183,7 @@ private Mono<CommonStatisticsDto> createBitfinexStatistics(StatisticsCurrPair cu
183183
Mono<CommonStatisticsDto> result = this.myMongoRepository
184184
.find(MongoUtils.buildTimeFrameQuery(Optional.of(currPair.getBitfinexKey()), TimeFrame.Year5, 5000),
185185
QuoteBf.class, BitfinexService.BF_DAY_COL)
186-
.collectList().flatMap(myList -> this.calcStatistics(myList)).map(value -> {
186+
.collectList().flatMap(StatisticService::calcStatistics).map(value -> {
187187
value.setCurrPair(currPair);
188188
return value;
189189
});

backend/src/test/java/ch/xxx/trader/usecase/services/StatisticServiceTest.java

+26-28
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,10 @@
2626
import org.junit.jupiter.api.Assertions;
2727
import org.junit.jupiter.api.Test;
2828
import org.junit.jupiter.api.extension.ExtendWith;
29-
import org.mockito.Mock;
3029
import org.mockito.junit.jupiter.MockitoExtension;
3130

3231
import ch.xxx.trader.domain.model.dto.CommonStatisticsDto;
3332
import ch.xxx.trader.domain.model.dto.RangeDto;
34-
import ch.xxx.trader.domain.model.entity.MyMongoRepository;
3533
import ch.xxx.trader.domain.model.entity.QuoteBf;
3634
import ch.xxx.trader.domain.model.entity.QuoteBs;
3735

@@ -41,16 +39,16 @@ private enum StatisticKeys {
4139
getPerformance, getAvgVolume, getRange, getVolatility
4240
}
4341

44-
@Mock
45-
private MyMongoRepository myMongoRepository;
42+
// @Mock
43+
// private MyMongoRepository myMongoRepository;
4644

4745

4846
@Test
4947
public void statistic5Years() {
50-
StatisticService statisticService = new StatisticService(this.myMongoRepository);
48+
// StatisticService statisticService = new StatisticService(this.myMongoRepository);
5149
List<QuoteBs> quotesBs = createBsQuotes();
5250
CommonStatisticsDto dto = new CommonStatisticsDto();
53-
statisticService.calcStatistics5Years(quotesBs, dto);
51+
StatisticService.calcStatistics5Years(quotesBs, dto);
5452
Assertions.assertEquals(dto.getPerformance5Year().longValue(), 800L);
5553
Assertions.assertEquals(dto.getAvgVolume5Year(), BigDecimal.valueOf(50L));
5654
Assertions.assertEquals(dto.getRange5Year().getMin(), BigDecimal.TEN);
@@ -60,10 +58,10 @@ public void statistic5Years() {
6058

6159
@Test
6260
public void statistic2Years() {
63-
StatisticService statisticService = new StatisticService(this.myMongoRepository);
61+
// StatisticService statisticService = new StatisticService(this.myMongoRepository);
6462
List<QuoteBf> quotesBf = createBfQuotes();
6563
CommonStatisticsDto dto = new CommonStatisticsDto();
66-
statisticService.calcStatistics2Years(quotesBf, dto);
64+
StatisticService.calcStatistics2Years(quotesBf, dto);
6765
Assertions.assertEquals(dto.getPerformance2Year().longValue(), 350L);
6866
Assertions.assertEquals(dto.getAvgVolume2Year(), BigDecimal.valueOf(55L));
6967
Assertions.assertEquals(dto.getRange2Year().getMin(), BigDecimal.valueOf(20L));
@@ -73,10 +71,10 @@ public void statistic2Years() {
7371

7472
@Test
7573
public void statistic1Year() {
76-
StatisticService statisticService = new StatisticService(this.myMongoRepository);
74+
// StatisticService statisticService = new StatisticService(this.myMongoRepository);
7775
List<QuoteBs> quotesBs = createBsQuotes();
7876
CommonStatisticsDto dto = new CommonStatisticsDto();
79-
statisticService.calcStatistics1Year(quotesBs, dto);
77+
StatisticService.calcStatistics1Year(quotesBs, dto);
8078
Assertions.assertEquals(dto.getPerformance1Year().longValue(), 200L);
8179
Assertions.assertEquals(dto.getAvgVolume1Year(), BigDecimal.valueOf(60L));
8280
Assertions.assertEquals(dto.getRange1Year().getMin(), BigDecimal.valueOf(30L));
@@ -86,10 +84,10 @@ public void statistic1Year() {
8684

8785
@Test
8886
public void statistic6Months() {
89-
StatisticService statisticService = new StatisticService(this.myMongoRepository);
87+
// StatisticService statisticService = new StatisticService(this.myMongoRepository);
9088
List<QuoteBf> quotesBf = createBfQuotes();
9189
CommonStatisticsDto dto = new CommonStatisticsDto();
92-
statisticService.calcStatistics6Months(quotesBf, dto);
90+
StatisticService.calcStatistics6Months(quotesBf, dto);
9391
Assertions.assertEquals(dto.getPerformance6Month().longValue(), 125L);
9492
Assertions.assertEquals(dto.getAvgVolume6Month(), BigDecimal.valueOf(65L));
9593
Assertions.assertEquals(dto.getRange6Month().getMin(), BigDecimal.valueOf(40L));
@@ -99,10 +97,10 @@ public void statistic6Months() {
9997

10098
@Test
10199
public void statistic3Months() {
102-
StatisticService statisticService = new StatisticService(this.myMongoRepository);
100+
// StatisticService statisticService = new StatisticService(this.myMongoRepository);
103101
List<QuoteBs> quotesBs = createBsQuotes();
104102
CommonStatisticsDto dto = new CommonStatisticsDto();
105-
statisticService.calcStatistics3Months(quotesBs, dto);
103+
StatisticService.calcStatistics3Months(quotesBs, dto);
106104
Assertions.assertEquals(dto.getPerformance3Month().longValue(), 80L);
107105
Assertions.assertEquals(dto.getAvgVolume3Month(), BigDecimal.valueOf(70L));
108106
Assertions.assertEquals(dto.getRange3Month().getMin(), BigDecimal.valueOf(50L));
@@ -112,10 +110,10 @@ public void statistic3Months() {
112110

113111
@Test
114112
public void statistic1Month() {
115-
StatisticService statisticService = new StatisticService(this.myMongoRepository);
113+
// StatisticService statisticService = new StatisticService(this.myMongoRepository);
116114
List<QuoteBf> quotesBf = createBfQuotes();
117115
CommonStatisticsDto dto = new CommonStatisticsDto();
118-
statisticService.calcStatistics1Month(quotesBf, dto);
116+
StatisticService.calcStatistics1Month(quotesBf, dto);
119117
Assertions.assertEquals(dto.getPerformance1Month().longValue(), 50L);
120118
Assertions.assertEquals(dto.getAvgVolume1Month(), BigDecimal.valueOf(75L));
121119
Assertions.assertEquals(dto.getRange1Month().getMin(), BigDecimal.valueOf(60L));
@@ -125,65 +123,65 @@ public void statistic1Month() {
125123

126124
@Test
127125
public void statistic1MonthEmpty() throws NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
128-
StatisticService statisticService = new StatisticService(this.myMongoRepository);
126+
// StatisticService statisticService = new StatisticService(this.myMongoRepository);
129127
List<QuoteBf> quotesBf = List.of();
130128
CommonStatisticsDto dto = new CommonStatisticsDto();
131-
statisticService.calcStatistics1Month(quotesBf, dto);
129+
StatisticService.calcStatistics1Month(quotesBf, dto);
132130
String durationStr = "1Month";
133131

134132
checkEmptyResult(dto, durationStr);
135133
}
136134

137135
@Test
138136
public void statistic3MonthEmpty() throws NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
139-
StatisticService statisticService = new StatisticService(this.myMongoRepository);
137+
// StatisticService statisticService = new StatisticService(this.myMongoRepository);
140138
List<QuoteBf> quotesBf = List.of();
141139
CommonStatisticsDto dto = new CommonStatisticsDto();
142-
statisticService.calcStatistics3Months(quotesBf, dto);
140+
StatisticService.calcStatistics3Months(quotesBf, dto);
143141
String durationStr = "3Month";
144142

145143
checkEmptyResult(dto, durationStr);
146144
}
147145

148146
@Test
149147
public void statistic6MonthEmpty() throws NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
150-
StatisticService statisticService = new StatisticService(this.myMongoRepository);
148+
// StatisticService statisticService = new StatisticService(this.myMongoRepository);
151149
List<QuoteBf> quotesBf = List.of();
152150
CommonStatisticsDto dto = new CommonStatisticsDto();
153-
statisticService.calcStatistics6Months(quotesBf, dto);
151+
StatisticService.calcStatistics6Months(quotesBf, dto);
154152
String durationStr = "6Month";
155153

156154
checkEmptyResult(dto, durationStr);
157155
}
158156

159157
@Test
160158
public void statistic1YearEmpty() throws NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
161-
StatisticService statisticService = new StatisticService(this.myMongoRepository);
159+
// StatisticService statisticService = new StatisticService(this.myMongoRepository);
162160
List<QuoteBf> quotesBf = List.of();
163161
CommonStatisticsDto dto = new CommonStatisticsDto();
164-
statisticService.calcStatistics1Year(quotesBf, dto);
162+
StatisticService.calcStatistics1Year(quotesBf, dto);
165163
String durationStr = "1Year";
166164

167165
checkEmptyResult(dto, durationStr);
168166
}
169167

170168
@Test
171169
public void statistic2YearEmpty() throws NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
172-
StatisticService statisticService = new StatisticService(this.myMongoRepository);
170+
// StatisticService statisticService = new StatisticService(this.myMongoRepository);
173171
List<QuoteBf> quotesBf = List.of();
174172
CommonStatisticsDto dto = new CommonStatisticsDto();
175-
statisticService.calcStatistics2Years(quotesBf, dto);
173+
StatisticService.calcStatistics2Years(quotesBf, dto);
176174
String durationStr = "2Year";
177175

178176
checkEmptyResult(dto, durationStr);
179177
}
180178

181179
@Test
182180
public void statistic5YearEmpty() throws NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
183-
StatisticService statisticService = new StatisticService(this.myMongoRepository);
181+
// StatisticService statisticService = new StatisticService(this.myMongoRepository);
184182
List<QuoteBf> quotesBf = List.of();
185183
CommonStatisticsDto dto = new CommonStatisticsDto();
186-
statisticService.calcStatistics5Years(quotesBf, dto);
184+
StatisticService.calcStatistics5Years(quotesBf, dto);
187185
String durationStr = "5Year";
188186

189187
checkEmptyResult(dto, durationStr);

0 commit comments

Comments
 (0)