From f826f1d543be43045ba3b04aa99698090386b664 Mon Sep 17 00:00:00 2001 From: Athou Date: Mon, 27 Apr 2026 17:57:30 +0200 Subject: [PATCH] create comparator only once --- .../com/commafeed/backend/feed/parser/FeedParser.java | 4 +++- .../java/com/commafeed/backend/opml/OPMLExporter.java | 9 +++++++-- .../com/commafeed/frontend/resource/CategoryREST.java | 9 +++++++-- 3 files changed, 17 insertions(+), 5 deletions(-) diff --git a/commafeed-server/src/main/java/com/commafeed/backend/feed/parser/FeedParser.java b/commafeed-server/src/main/java/com/commafeed/backend/feed/parser/FeedParser.java index f6d48f0e..88de6ec8 100644 --- a/commafeed-server/src/main/java/com/commafeed/backend/feed/parser/FeedParser.java +++ b/commafeed-server/src/main/java/com/commafeed/backend/feed/parser/FeedParser.java @@ -50,6 +50,8 @@ public class FeedParser { private static final Instant START = Instant.ofEpochMilli(86400000); private static final Instant END = Instant.ofEpochMilli(1000L * Integer.MAX_VALUE - 86400000); + private static final Comparator ENTRY_COMPARATOR = Comparator.comparing(Entry::published).reversed(); + private final EncodingDetector encodingDetector; private final FeedCleaner feedCleaner; @@ -138,7 +140,7 @@ public class FeedParser { entries.add(new Entry(guid, url, publishedDate, content)); } - entries.sort(Comparator.comparing(Entry::published).reversed()); + entries.sort(ENTRY_COMPARATOR); return entries; } diff --git a/commafeed-server/src/main/java/com/commafeed/backend/opml/OPMLExporter.java b/commafeed-server/src/main/java/com/commafeed/backend/opml/OPMLExporter.java index a47bd240..fad5a625 100644 --- a/commafeed-server/src/main/java/com/commafeed/backend/opml/OPMLExporter.java +++ b/commafeed-server/src/main/java/com/commafeed/backend/opml/OPMLExporter.java @@ -23,6 +23,11 @@ import lombok.RequiredArgsConstructor; @Singleton public class OPMLExporter { + private static final Comparator CATEGORY_COMPARATOR = Comparator + .comparingInt(e -> ObjectUtils.firstNonNull(e.getPosition(), 0)); + private static final Comparator SUBSCRIPTION_COMPARATOR = Comparator + .comparingInt(e -> ObjectUtils.firstNonNull(e.getPosition(), 0)); + private final FeedCategoryDAO feedCategoryDAO; private final FeedSubscriptionDAO feedSubscriptionDAO; @@ -33,10 +38,10 @@ public class OPMLExporter { opml.setCreated(new Date()); List categories = feedCategoryDAO.findAll(user); - categories.sort(Comparator.comparingInt(e -> ObjectUtils.firstNonNull(e.getPosition(), 0))); + categories.sort(CATEGORY_COMPARATOR); List subscriptions = feedSubscriptionDAO.findAll(user); - subscriptions.sort(Comparator.comparingInt(e -> ObjectUtils.firstNonNull(e.getPosition(), 0))); + subscriptions.sort(SUBSCRIPTION_COMPARATOR); // export root categories for (FeedCategory cat : categories.stream().filter(c -> c.getParent() == null).toList()) { diff --git a/commafeed-server/src/main/java/com/commafeed/frontend/resource/CategoryREST.java b/commafeed-server/src/main/java/com/commafeed/frontend/resource/CategoryREST.java index c8592628..ed855b09 100644 --- a/commafeed-server/src/main/java/com/commafeed/frontend/resource/CategoryREST.java +++ b/commafeed-server/src/main/java/com/commafeed/frontend/resource/CategoryREST.java @@ -84,6 +84,11 @@ public class CategoryREST { public static final String ALL = "all"; public static final String STARRED = "starred"; + private static final Comparator CATEGORY_COMPARATOR = Comparator.comparing(Category::getPosition) + .thenComparing(Category::getName); + private static final Comparator SUBSCRIPTION_COMPARATOR = Comparator.comparing(Subscription::getPosition) + .thenComparing(Subscription::getName); + private final AuthenticationContext authenticationContext; private final FeedCategoryDAO feedCategoryDAO; private final FeedEntryStatusDAO feedEntryStatusDAO; @@ -432,7 +437,7 @@ public class CategoryREST { category.getChildren().add(child); } } - category.getChildren().sort(Comparator.comparing(Category::getPosition).thenComparing(Category::getName)); + category.getChildren().sort(CATEGORY_COMPARATOR); for (FeedSubscription subscription : subscriptions) { if (id == null && subscription.getCategory() == null @@ -442,7 +447,7 @@ public class CategoryREST { category.getFeeds().add(sub); } } - category.getFeeds().sort(Comparator.comparing(Subscription::getPosition).thenComparing(Subscription::getName)); + category.getFeeds().sort(SUBSCRIPTION_COMPARATOR); return category; }