TotalRecompressionLimiter implements RecommendationModifier { @Override public List<QualifiedRecommendation> getModifiedRecommendations( File oldFile, File newFile, List<QualifiedRecommendation> originalRecommendations) { List<QualifiedRecommendation> sorted = new ArrayList<QualifiedRecommendation>(originalRecommendations); Collections.sort(sorted, COMPARATOR); Collections.reverse(sorted); List<QualifiedRecommendation> result = new ArrayList<>(sorted.size()); long recompressibleBytesRemaining = maxBytesToRecompress; for (QualifiedRecommendation originalRecommendation : sorted) { if (!originalRecommendation.getRecommendation().uncompressNewEntry) { result.add(originalRecommendation); } else { long bytesToRecompress = originalRecommendation.getNewEntry().getUncompressedSize(); if (recompressibleBytesRemaining - bytesToRecompress >= 0) { result.add(originalRecommendation); recompressibleBytesRemaining -= bytesToRecompress; } else { result.add( new QualifiedRecommendation( originalRecommendation.getOldEntry(), originalRecommendation.getNewEntry(), Recommendation.UNCOMPRESS_NEITHER, RecommendationReason.RESOURCE_CONSTRAINED)); } } } return result; } TotalRecompressionLimiter(long maxBytesToRecompress); @Override List<QualifiedRecommendation> getModifiedRecommendations(
      File oldFile, File newFile, List<QualifiedRecommendation> originalRecommendations);  }
@Test public void testZeroLimit() { TotalRecompressionLimiter limiter = new TotalRecompressionLimiter(0); List<QualifiedRecommendation> expected = new ArrayList<QualifiedRecommendation>(); expected.addAll(suppressed(REC_A_100K, REC_B_200K, REC_C_300K, REC_D_400K)); expected.addAll(ALL_IGNORED_RECS); assertEquivalence(expected, limiter.getModifiedRecommendations(OLD_FILE, NEW_FILE, ALL_RECS)); }
@Test public void testMaxLimit() { TotalRecompressionLimiter limiter = new TotalRecompressionLimiter(Long.MAX_VALUE); assertEquivalence(ALL_RECS, limiter.getModifiedRecommendations(OLD_FILE, NEW_FILE, ALL_RECS)); }
@Test public void testLimit_ExactlySmallest() { long limit = REC_A_100K.getNewEntry().getUncompressedSize(); TotalRecompressionLimiter limiter = new TotalRecompressionLimiter(limit); List<QualifiedRecommendation> expected = new ArrayList<QualifiedRecommendation>(); expected.add(REC_A_100K); expected.addAll(suppressed(REC_B_200K, REC_C_300K, REC_D_400K)); expected.addAll(ALL_IGNORED_RECS); assertEquivalence(expected, limiter.getModifiedRecommendations(OLD_FILE, NEW_FILE, ALL_RECS)); }
@Test public void testLimit_EdgeUnderSmallest() { long limit = REC_A_100K.getNewEntry().getUncompressedSize() - 1; TotalRecompressionLimiter limiter = new TotalRecompressionLimiter(limit); List<QualifiedRecommendation> expected = new ArrayList<QualifiedRecommendation>(); expected.addAll(suppressed(REC_A_100K, REC_B_200K, REC_C_300K, REC_D_400K)); expected.addAll(ALL_IGNORED_RECS); assertEquivalence(expected, limiter.getModifiedRecommendations(OLD_FILE, NEW_FILE, ALL_RECS)); }
@Test public void testLimit_EdgeOverSmallest() { long limit = REC_A_100K.getNewEntry().getUncompressedSize() + 1; TotalRecompressionLimiter limiter = new TotalRecompressionLimiter(limit); List<QualifiedRecommendation> expected = new ArrayList<QualifiedRecommendation>(); expected.add(REC_A_100K); expected.addAll(suppressed(REC_B_200K, REC_C_300K, REC_D_400K)); expected.addAll(ALL_IGNORED_RECS); assertEquivalence(expected, limiter.getModifiedRecommendations(OLD_FILE, NEW_FILE, ALL_RECS)); }
@Test public void testLimit_ExactlyLargest() { long limit = REC_D_400K.getNewEntry().getUncompressedSize(); TotalRecompressionLimiter limiter = new TotalRecompressionLimiter(limit); List<QualifiedRecommendation> expected = new ArrayList<QualifiedRecommendation>(); expected.add(REC_D_400K); expected.addAll(suppressed(REC_A_100K, REC_B_200K, REC_C_300K)); expected.addAll(ALL_IGNORED_RECS); assertEquivalence(expected, limiter.getModifiedRecommendations(OLD_FILE, NEW_FILE, ALL_RECS)); }
@Test public void testLimit_EdgeUnderLargest() { long limit = REC_D_400K.getNewEntry().getUncompressedSize() - 1; TotalRecompressionLimiter limiter = new TotalRecompressionLimiter(limit); List<QualifiedRecommendation> expected = new ArrayList<QualifiedRecommendation>(); expected.add(REC_C_300K); expected.addAll(suppressed(REC_A_100K, REC_B_200K, REC_D_400K)); expected.addAll(ALL_IGNORED_RECS); assertEquivalence(expected, limiter.getModifiedRecommendations(OLD_FILE, NEW_FILE, ALL_RECS)); }
@Test public void testLimit_EdgeOverLargest() { long limit = REC_D_400K.getNewEntry().getUncompressedSize() + 1; TotalRecompressionLimiter limiter = new TotalRecompressionLimiter(limit); List<QualifiedRecommendation> expected = new ArrayList<QualifiedRecommendation>(); expected.add(REC_D_400K); expected.addAll(suppressed(REC_A_100K, REC_B_200K, REC_C_300K)); expected.addAll(ALL_IGNORED_RECS); assertEquivalence(expected, limiter.getModifiedRecommendations(OLD_FILE, NEW_FILE, ALL_RECS)); }
@Test public void testLimit_Complex() { long limit = REC_D_400K.getNewEntry().getUncompressedSize() + REC_B_200K.getNewEntry().getUncompressedSize(); TotalRecompressionLimiter limiter = new TotalRecompressionLimiter(limit); List<QualifiedRecommendation> expected = new ArrayList<QualifiedRecommendation>(); expected.add(REC_B_200K); expected.add(REC_D_400K); expected.addAll(suppressed(REC_A_100K, REC_C_300K)); expected.addAll(ALL_IGNORED_RECS); assertEquivalence(expected, limiter.getModifiedRecommendations(OLD_FILE, NEW_FILE, ALL_RECS)); }