From efdef715276b08eb3409febb3cb3f3d568415eeb Mon Sep 17 00:00:00 2001 From: Priamo Germosen <6233424+pgermosen@users.noreply.github.com> Date: Mon, 14 Sep 2026 21:55:25 -0400 Subject: [PATCH] refactor(cloudfront): extract repeated marker/maxItems pagination into the existing paginate helper (#3586) CloudFrontService.java already has a private generic paginate(List, marker, maxItems, keyFn) helper, used by seven of its list* methods (functions, continuous deployment policies, public keys, key groups, realtime log configs, field-level encryption configs/profiles). Six older list* methods (listDistributions, listInvalidations, listCachePolicies, listOriginRequestPolicies, listOriginAccessControls, listCloudFrontOriginAccessIdentities) were never migrated to it and kept their own inline copy of the same 18-line marker-lookup and maxItems-truncation block, the duplication CPD flagged. Wired all six onto the existing helper. listResponseHeadersPolicies keeps its own inline version untouched: unlike the others, it treats an unmatched marker as InvalidArgument instead of silently starting from the beginning, a genuine behavioral difference from paginate() that this issue does not ask to change. No behavior change for the six migrated methods. Full CloudFront and CloudFront CloudFormation test suites pass unchanged. --- .../cloudfront/CloudFrontService.java | 90 ++----------------- 1 file changed, 6 insertions(+), 84 deletions(-) diff --git a/src/main/java/io/github/hectorvent/floci/services/cloudfront/CloudFrontService.java b/src/main/java/io/github/hectorvent/floci/services/cloudfront/CloudFrontService.java index 93db2d7d4c..7ad1aa7d42 100644 --- a/src/main/java/io/github/hectorvent/floci/services/cloudfront/CloudFrontService.java +++ b/src/main/java/io/github/hectorvent/floci/services/cloudfront/CloudFrontService.java @@ -266,20 +266,7 @@ public class CloudFrontService { public List listDistributions(String marker, int maxItems) { List all = new ArrayList<>(distStore.scan(k -> true)); all.sort((a, b) -> a.getId().compareTo(b.getId())); - if (marker != null && !marker.isEmpty()) { - int idx = 0; - for (int i = 0; i < all.size(); i++) { - if (all.get(i).getId().equals(marker)) { - idx = i + 1; - break; - } - } - all = all.subList(idx, all.size()); - } - if (maxItems > 0 && all.size() > maxItems) { - return all.subList(0, maxItems); - } - return all; + return paginate(all, marker, maxItems, Distribution::getId); } public synchronized void associateAlias(String targetDistributionId, String alias) { @@ -443,20 +430,7 @@ public class CloudFrontService { getDistribution(distributionId); List all = new ArrayList<>( invalidationStore.get(distributionId).orElse(List.of())); - if (marker != null && !marker.isEmpty()) { - int idx = 0; - for (int i = 0; i < all.size(); i++) { - if (all.get(i).getId().equals(marker)) { - idx = i + 1; - break; - } - } - all = all.subList(idx, all.size()); - } - if (maxItems > 0 && all.size() > maxItems) { - return all.subList(0, maxItems); - } - return all; + return paginate(all, marker, maxItems, Invalidation::getId); } // ── Cache Policies ──────────────────────────────────────────────────────── @@ -505,20 +479,7 @@ public class CloudFrontService { List all = new ArrayList<>(cachePolicyStore.scan(k -> true)); all.sort((a, b) -> a.getName() != null && b.getName() != null ? a.getName().compareTo(b.getName()) : a.getId().compareTo(b.getId())); - if (marker != null && !marker.isEmpty()) { - int idx = 0; - for (int i = 0; i < all.size(); i++) { - if (all.get(i).getId().equals(marker)) { - idx = i + 1; - break; - } - } - all = all.subList(idx, all.size()); - } - if (maxItems > 0 && all.size() > maxItems) { - return all.subList(0, maxItems); - } - return all; + return paginate(all, marker, maxItems, CachePolicy::getId); } // ── Origin Request Policies ─────────────────────────────────────────────── @@ -569,20 +530,7 @@ public class CloudFrontService { List all = new ArrayList<>(orpStore.scan(k -> true)); all.sort((a, b) -> a.getName() != null && b.getName() != null ? a.getName().compareTo(b.getName()) : a.getId().compareTo(b.getId())); - if (marker != null && !marker.isEmpty()) { - int idx = 0; - for (int i = 0; i < all.size(); i++) { - if (all.get(i).getId().equals(marker)) { - idx = i + 1; - break; - } - } - all = all.subList(idx, all.size()); - } - if (maxItems > 0 && all.size() > maxItems) { - return all.subList(0, maxItems); - } - return all; + return paginate(all, marker, maxItems, OriginRequestPolicy::getId); } // ── Response Headers Policies ───────────────────────────────────────────── @@ -933,20 +881,7 @@ public class CloudFrontService { List all = new ArrayList<>(oacStore.scan(k -> true)); all.sort((a, b) -> a.getName() != null && b.getName() != null ? a.getName().compareTo(b.getName()) : a.getId().compareTo(b.getId())); - if (marker != null && !marker.isEmpty()) { - int idx = 0; - for (int i = 0; i < all.size(); i++) { - if (all.get(i).getId().equals(marker)) { - idx = i + 1; - break; - } - } - all = all.subList(idx, all.size()); - } - if (maxItems > 0 && all.size() > maxItems) { - return all.subList(0, maxItems); - } - return all; + return paginate(all, marker, maxItems, OriginAccessControl::getId); } // ── Origin Access Identity (OAI) ────────────────────────────────────────── @@ -1003,20 +938,7 @@ public class CloudFrontService { public List listCloudFrontOriginAccessIdentities( String marker, int maxItems) { List all = new ArrayList<>(oaiStore.scan(k -> true)); - if (marker != null && !marker.isEmpty()) { - int idx = 0; - for (int i = 0; i < all.size(); i++) { - if (all.get(i).getId().equals(marker)) { - idx = i + 1; - break; - } - } - all = all.subList(idx, all.size()); - } - if (maxItems > 0 && all.size() > maxItems) { - return all.subList(0, maxItems); - } - return all; + return paginate(all, marker, maxItems, CloudFrontOriginAccessIdentity::getId); } private static void validateOriginAccessControl(OriginAccessControl oac) {