Skip to content

Commit

Permalink
feat: improve Streams
Browse files Browse the repository at this point in the history
  • Loading branch information
Angular2Guy committed Nov 28, 2024
1 parent e143b0b commit b79c032
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public ArrayList<String> importSymbols() {
throw new SocketException(String.format("Failed to connect to %s", HOST));
}
FTPFile[] files = ftp.listFiles(DIR);
if (2 != Arrays.stream(files).map(FTPFile::getName).filter(file -> IMPORT_FILES.contains(file)).count()) {
if (2 != Arrays.stream(files).map(FTPFile::getName).filter(IMPORT_FILES::contains).count()) {
throw new FileNotFoundException(
String.format("Files: %s, %s", IMPORT_FILES.get(0), IMPORT_FILES.get(1)));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public void doFilter(ServletRequest request, ServletResponse response, FilterCha
} else {
Iterable<Locale> iterable = () -> myRequest.getLocales().asIterator();
Locale userLocale = StreamSupport.stream(iterable.spliterator(), false)
.filter(myLocale -> SUPPORTED_LOCALES.contains(myLocale)).findFirst().orElse(Locale.ENGLISH);
.filter(SUPPORTED_LOCALES::contains).findFirst().orElse(Locale.ENGLISH);
String forwardPath = String.format("/%s/index.html", userLocale.getLanguage());
// LOG.info(String.format("Forward to: %s", forwardPath));
RequestDispatcher dispatcher = myRequest.getServletContext().getRequestDispatcher(forwardPath);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -169,14 +169,15 @@ private List<Predicate> createSymbolFinancialsPredicates(SymbolFinancialsQueryPa
Optional.ofNullable(symbolFinancialsQueryParams.getSymbol()).stream()
.filter(myValue -> !myValue.trim().isBlank()).forEach(myValue -> predicates
.add(createColumnCriteria(symbolFinancialsQueryParams.getSymbol(), root, false, SYMBOL)));
Optional.ofNullable(symbolFinancialsQueryParams.getName()).stream().filter(myValue -> !myValue.trim().isBlank())
Optional.ofNullable(symbolFinancialsQueryParams.getName()).stream().map(String::trim).filter(java.util.function.Predicate.not(String::isBlank))
.forEach(
myValue -> predicates.add(createColumnCriteria(symbolFinancialsQueryParams.getName(), root, false, NAME)));
Optional.ofNullable(symbolFinancialsQueryParams.getCity()).stream().filter(myValue -> !myValue.trim().isBlank())
Optional.ofNullable(symbolFinancialsQueryParams.getCity()).stream().map(String::trim).filter(java.util.function.Predicate.not(String::isBlank))
.forEach(
myValue -> predicates.add(createColumnCriteria(symbolFinancialsQueryParams.getCity(), root, false, CITY)));
Optional.ofNullable(symbolFinancialsQueryParams.getCountry()).stream()
.filter(myValue -> !myValue.trim().isBlank()).forEach(myValue -> predicates
Optional.ofNullable(symbolFinancialsQueryParams.getCountry()).stream()
.map(String::trim).filter(java.util.function.Predicate.not(String::isBlank))
.forEach(myValue -> predicates
.add(createColumnCriteria(symbolFinancialsQueryParams.getCountry(), root, false, COUNTRY)));
if (symbolFinancialsQueryParams.getQuarters() != null && !symbolFinancialsQueryParams.getQuarters().isEmpty()) {
predicates.add(this.entityManager.getCriteriaBuilder().in(root.get(QUARTER))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

import java.util.Optional;
import java.util.Set;
import java.util.function.Predicate;

import org.springframework.stereotype.Component;

Expand Down Expand Up @@ -44,9 +45,9 @@ public AppUser convert(AppUserDto dto, Optional<AppUser> entityOpt) {
myEntity.setUserRole(dto.getUserRole());
myEntity.setUuid(dto.getUuid());
myEntity.setAlphavantageKey(Optional.ofNullable(dto.getAlphavantageKey()).stream()
.filter(myString -> !myString.isBlank()).findFirst().orElse(myEntity.getAlphavantageKey()));
.filter(Predicate.not(String::isBlank)).findFirst().orElse(myEntity.getAlphavantageKey()));
myEntity.setRapidApiKey(Optional.ofNullable(dto.getRapidApiKey()).stream()
.filter(myString -> !myString.isBlank()).findFirst().orElse(myEntity.getRapidApiKey()));
.filter(Predicate.not(String::isBlank)).findFirst().orElse(myEntity.getRapidApiKey()));
return myEntity;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
package ch.xxx.manager.usecase.mapping;

import java.util.Optional;
import java.util.function.Predicate;

import ch.xxx.manager.domain.model.entity.Symbol;

Expand All @@ -24,8 +25,8 @@ public static String findSectorName(Symbol entity) {
String result = myEntity.getYahooName() != null && !myEntity.getYahooName().isBlank()
? myEntity.getYahooName()
: switch (entity.getQuoteSource()) {
case ALPHAVANTAGE -> Optional.ofNullable(myEntity.getAlphavantageName()).filter(myName -> !myName.isBlank()).orElse(SECTOR_PORTFOLIO);
case YAHOO -> Optional.ofNullable(myEntity.getYahooName()).filter(myName -> !myName.isBlank()).orElse(SECTOR_PORTFOLIO);
case ALPHAVANTAGE -> Optional.ofNullable(myEntity.getAlphavantageName()).filter(Predicate.not(String::isBlank)).orElse(SECTOR_PORTFOLIO);
case YAHOO -> Optional.ofNullable(myEntity.getYahooName()).filter(Predicate.not(String::isBlank)).orElse(SECTOR_PORTFOLIO);
default -> SECTOR_PORTFOLIO;
};
return result;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import java.util.concurrent.Future;
import java.util.concurrent.atomic.AtomicLong;
import java.util.concurrent.atomic.AtomicReference;
import java.util.function.Predicate;
import java.util.stream.Collectors;
import java.util.stream.Stream;

Expand Down Expand Up @@ -106,9 +107,9 @@ public Future<Long> updateSymbolQuotes(List<Symbol> symbolsToUpdate) {
this.decrypt(myAppUser.getAlphavantageKey(), UUID.fromString(myAppUser.getUuid())),
this.decrypt(myAppUser.getRapidApiKey(), UUID.fromString(myAppUser.getUuid()))))
.filter(myUserKeys -> Optional.ofNullable(myUserKeys.alphavantageKey()).stream()
.filter(myStr -> !myStr.isBlank()).findFirst().isPresent())
.filter(Predicate.not(String::isBlank)).findFirst().isPresent())
.filter(myUserKeys -> Optional.ofNullable(myUserKeys.RapidApiKey()).stream()
.filter(myStr -> !myStr.isBlank()).findFirst().isPresent())
.filter(Predicate.not(String::isBlank)).findFirst().isPresent())
.toList();
LOGGER.info("UserKeys size: {}", allUserKeys.size());
final AtomicLong indexDaily = new AtomicLong(-1L);
Expand Down Expand Up @@ -174,7 +175,7 @@ public Long importDeSymbols(List<String> xetra) {
this.refreshSymbolEntities();
LOGGER.info("importDeSymbols() called.");
List<Symbol> result = this.repository.saveAll(xetra.stream().filter(this::filter).filter(this::filterXetra)
.flatMap(line -> this.convertXetra(line)).collect(Collectors.groupingBy(Symbol::getSymbol)).entrySet()
.flatMap(SymbolImportService::convertXetra).collect(Collectors.groupingBy(Symbol::getSymbol)).entrySet()
.stream()
.flatMap(group -> group.getValue().isEmpty() ? Stream.empty() : Stream.of(group.getValue().get(0)))
.flatMap(entity -> this.replaceEntity(entity)).collect(Collectors.toList()));
Expand Down Expand Up @@ -270,7 +271,7 @@ private boolean filterXetra(String line) {
return line.contains("DEUTSCHLAND") || line.contains("DAX");
}

private Stream<Symbol> convertXetra(String symbolLine) {
private static Stream<Symbol> convertXetra(String symbolLine) {
String[] strParts = symbolLine.split(";");
String symbol = String.format("%s.DEX",
strParts[7].substring(0, strParts[7].length() < 15 ? strParts[7].length() : 15));
Expand Down

0 comments on commit b79c032

Please sign in to comment.