Skip to content

Commit 34c72a0

Browse files
committed
feat: improve streams
1 parent a5392da commit 34c72a0

File tree

4 files changed

+19
-14
lines changed

4 files changed

+19
-14
lines changed

backend/src/main/java/ch/xxx/aidoclibchat/adapter/client/OpenLibraryRestClient.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import java.nio.charset.StandardCharsets;
1717
import java.util.List;
1818
import java.util.Optional;
19+
import java.util.function.Predicate;
1920
import java.util.stream.Collectors;
2021

2122
import org.slf4j.Logger;
@@ -41,7 +42,7 @@ public Response apply(Request request) {
4142
var titleOpt = this.createParamOpt(request.title(), "title");
4243
var subjectOpt = this.createParamOpt(request.subject(), "subject");
4344
var paramsStr = List.of(authorOpt, titleOpt, subjectOpt).stream()
44-
.filter(Optional::isPresent).map(myOpt -> myOpt.get()).collect(Collectors.joining("&"));
45+
.filter(Optional::isPresent).map(Optional::get).collect(Collectors.joining("&"));
4546
var urlStr =
4647
String.format("%s?%s", this.baseUrl, paramsStr);
4748
LOGGER.info(urlStr);
@@ -50,7 +51,7 @@ public Response apply(Request request) {
5051
}
5152

5253
private Optional<String> createParamOpt(String valueStr, String keyStr) {
53-
return Optional.ofNullable(valueStr).stream().filter(myAuthor -> !myAuthor.isBlank())
54+
return Optional.ofNullable(valueStr).stream().filter(Predicate.not(String::isBlank))
5455
.map(myAuthor -> String.format("%s=%s", keyStr, URLEncoder.encode(myAuthor, StandardCharsets.UTF_8))).findFirst();
5556
}
5657
}

backend/src/main/java/ch/xxx/aidoclibchat/adapter/config/ForwardServletFilter.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ public void doFilter(ServletRequest request, ServletResponse response, FilterCha
6161
} else {
6262
Iterable<Locale> iterable = () -> myRequest.getLocales().asIterator();
6363
Locale userLocale = StreamSupport.stream(iterable.spliterator(), false)
64-
.filter(myLocale -> SUPPORTED_LOCALES.contains(myLocale)).findFirst().orElse(Locale.ENGLISH);
64+
.filter(SUPPORTED_LOCALES::contains).findFirst().orElse(Locale.ENGLISH);
6565
String forwardPath = String.format("/%s/index.html", userLocale.getLanguage());
6666
// LOG.info(String.format("Forward to: %s", forwardPath));
6767
RequestDispatcher dispatcher = myRequest.getServletContext().getRequestDispatcher(forwardPath);

backend/src/main/java/ch/xxx/aidoclibchat/usecase/service/DocumentService.java

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,12 @@
1818
import java.util.ArrayList;
1919
import java.util.List;
2020
import java.util.Map;
21+
import java.util.Objects;
2122
import java.util.Optional;
2223
import java.util.StringTokenizer;
2324
import java.util.UUID;
2425
import java.util.concurrent.atomic.AtomicReference;
26+
import java.util.function.Predicate;
2527
import java.util.stream.Collectors;
2628
import java.util.stream.Stream;
2729

@@ -144,16 +146,16 @@ public Optional<Book> findBookByUuid(String uuidStr) {
144146

145147
public List<Book> findBooksByTitleAuthor(String titleAuthor) {
146148
return Optional.ofNullable(titleAuthor).stream().filter(myStr -> myStr.trim().length() > 2)
147-
.map(myStr -> myStr.toLowerCase())
149+
.map(String::toLowerCase)
148150
.flatMap(myStr -> Stream.of(this.bookRepository.findByTitleAuthorWithChapters(myStr)))
149-
.flatMap(myList -> myList.stream()).toList();
151+
.flatMap(List::stream).toList();
150152
}
151153

152154
@Async
153155
public void addBookSummaries(Book book) {
154156
var myChapters = book.getChapters().stream().map(myChapter -> this.addChapterSummary(myChapter)).toList();
155157
// LOGGER.info(myChapters.getLast().getSummary());
156-
var summaries = myChapters.stream().map(myChapter -> myChapter.getChapterText())
158+
var summaries = myChapters.stream().map(Chapter::getChapterText)
157159
.reduce((acc, myChapter) -> acc + "\n" + myChapter);
158160
book.setSummary(this.chatClient
159161
.call(new SystemPromptTemplate(this.bookPrompt).createMessage(Map.of("text", summaries)).getContent()));
@@ -173,7 +175,7 @@ private Chapter addChapterSummary(Chapter myChapter) {
173175

174176
private Chapter createChapter(Book book, String heading, AtomicReference<List<String>> atomicRef) {
175177
var result = new Chapter();
176-
result.setTitle(atomicRef.get().stream().filter(myLine -> !myLine.isBlank()).findFirst().orElse(""));
178+
result.setTitle(atomicRef.get().stream().filter(Predicate.not(String::isBlank)).findFirst().orElse(""));
177179
result.setBook(book);
178180
var chapterText = atomicRef.get().stream().takeWhile(myLine -> !myLine.contains(heading))
179181
.collect(Collectors.joining(System.lineSeparator()));
@@ -251,8 +253,9 @@ public AiDocumentResult queryDocuments(SearchDto searchDto) {
251253
private Message getSystemMessage(List<org.springframework.ai.document.Document> similarDocuments, int tokenLimit,
252254
String prompt) {
253255
String documentStr = this.cutStringToTokenLimit(
254-
similarDocuments.stream().map(entry -> entry.getContent())
255-
.filter(myStr -> myStr != null && !myStr.isBlank()).collect(Collectors.joining("\n")),
256+
similarDocuments.stream().map(entry -> entry.getContent())
257+
.filter(Predicate.not(Objects::isNull))
258+
.filter(Predicate.not(String::isBlank)).collect(Collectors.joining("\n")),
256259
tokenLimit);
257260
SystemPromptTemplate systemPromptTemplate = this.activeProfile.contains("ollama")
258261
? new SystemPromptTemplate(this.ollamaPrompt)

backend/src/main/java/ch/xxx/aidoclibchat/usecase/service/TableService.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import java.util.Optional;
2121
import java.util.Set;
2222
import java.util.concurrent.atomic.AtomicReference;
23+
import java.util.function.Predicate;
2324
import java.util.stream.Collectors;
2425
import java.util.stream.Stream;
2526

@@ -225,7 +226,7 @@ private EmbeddingContainer retrieveEmbeddings(SearchDto searchDto) {
225226
.collect(Collectors.joining(" ")));
226227
}
227228
}
228-
var rowDocuments = rowSearchStrs.stream().filter(myStr -> !myStr.isBlank())
229+
var rowDocuments = rowSearchStrs.stream().filter(Predicate.not(String::isBlank))
229230
.flatMap(myStr -> this.documentVsRepository
230231
.retrieve(myStr, MetaData.DataType.ROW, searchDto.getResultAmount()).stream())
231232
.toList();
@@ -285,10 +286,10 @@ private void updateEmbeddings() {
285286
List<Document> rowDocs = Stream.concat(
286287
this.importService.findAllSubjects().stream()
287288
.filter(mySubject -> Optional.ofNullable(mySubject.getSubject()).stream()
288-
.allMatch(mySubjectStr -> !mySubjectStr.isBlank()))
289+
.allMatch(Predicate.not(String::isBlank)))
289290
.map(this::map),
290291
this.importService.findAllWorks().stream().filter(myWork -> Optional.ofNullable(myWork.getStyle())
291-
.stream().allMatch(myStyle -> !myStyle.isBlank())).map(this::map))
292+
.stream().allMatch(Predicate.not(String::isBlank))).map(this::map))
292293
.toList();
293294
this.importService.addDocuments(rowDocs);
294295
LOGGER.info("Row Embeddings updated {}ms", new Date().getTime() - rowStart.getTime());
@@ -319,9 +320,9 @@ private Document map(ColumnMetadata columnMetadata) {
319320
result.getMetadata().put(MetaData.DATANAME, columnMetadata.getColumnName());
320321
result.getMetadata().put(MetaData.TABLE_NAME, columnMetadata.getTableMetadata().getTableName());
321322
result.getMetadata().put(MetaData.PRIMARY_KEY, columnMetadata.isColumnPrimaryKey());
322-
Optional.ofNullable(columnMetadata.getReferenceTableName()).stream().filter(myStr -> !myStr.isBlank())
323+
Optional.ofNullable(columnMetadata.getReferenceTableName()).stream().filter(Predicate.not(String::isBlank))
323324
.findFirst().ifPresent(myStr -> result.getMetadata().put(MetaData.REFERENCE_TABLE, myStr));
324-
Optional.ofNullable(columnMetadata.getReferenceTableColumn()).stream().filter(myStr -> !myStr.isBlank())
325+
Optional.ofNullable(columnMetadata.getReferenceTableColumn()).stream().filter(Predicate.not(String::isBlank))
325326
.findFirst().ifPresent(myStr -> result.getMetadata().put(MetaData.REFERENCE_COLUMN, myStr));
326327
return result;
327328
}

0 commit comments

Comments
 (0)