FeedItemPermutors { @NonNull public static Permutor<FeedItem> getPermutor(@NonNull SortOrder sortOrder) { Comparator<FeedItem> comparator = null; Permutor<FeedItem> permutor = null; switch (sortOrder) { case EPISODE_TITLE_A_Z: comparator = (f1, f2) -> itemTitle(f1).compareTo(itemTitle(f2)); break; case EPISODE_TITLE_Z_A: comparator = (f1, f2) -> itemTitle(f2).compareTo(itemTitle(f1)); break; case DATE_OLD_NEW: comparator = (f1, f2) -> pubDate(f1).compareTo(pubDate(f2)); break; case DATE_NEW_OLD: comparator = (f1, f2) -> pubDate(f2).compareTo(pubDate(f1)); break; case DURATION_SHORT_LONG: comparator = (f1, f2) -> Integer.compare(duration(f1), duration(f2)); break; case DURATION_LONG_SHORT: comparator = (f1, f2) -> Integer.compare(duration(f2), duration(f1)); break; case FEED_TITLE_A_Z: comparator = (f1, f2) -> feedTitle(f1).compareTo(feedTitle(f2)); break; case FEED_TITLE_Z_A: comparator = (f1, f2) -> feedTitle(f2).compareTo(feedTitle(f1)); break; case RANDOM: permutor = Collections::shuffle; break; case SMART_SHUFFLE_OLD_NEW: permutor = (queue) -> smartShuffle(queue, true); break; case SMART_SHUFFLE_NEW_OLD: permutor = (queue) -> smartShuffle(queue, false); break; } if (comparator != null) { final Comparator<FeedItem> comparator2 = comparator; permutor = (queue) -> Collections.sort(queue, comparator2); } return permutor; }  @NonNull static Permutor<FeedItem> getPermutor(@NonNull SortOrder sortOrder);  }
@Test public void testEnsureNonNullPermutors() { for (SortOrder sortOrder : SortOrder.values()) { assertNotNull("The permutor for SortOrder " + sortOrder + " is unexpectedly null", FeedItemPermutors.getPermutor(sortOrder)); } }
@Test public void testPermutorForRule_FEED_TITLE_ASC() { Permutor<FeedItem> permutor = FeedItemPermutors.getPermutor(SortOrder.FEED_TITLE_A_Z); List<FeedItem> itemList = getTestList(); assertTrue(checkIdOrder(itemList, 1, 3, 2)); permutor.reorder(itemList); assertTrue(checkIdOrder(itemList, 1, 2, 3)); }
@Test public void testPermutorForRule_FEED_TITLE_DESC() { Permutor<FeedItem> permutor = FeedItemPermutors.getPermutor(SortOrder.FEED_TITLE_Z_A); List<FeedItem> itemList = getTestList(); assertTrue(checkIdOrder(itemList, 1, 3, 2)); permutor.reorder(itemList); assertTrue(checkIdOrder(itemList, 3, 2, 1)); }
@Test public void testPermutorForRule_FEED_TITLE_DESC_NullTitle() { Permutor<FeedItem> permutor = FeedItemPermutors.getPermutor(SortOrder.FEED_TITLE_Z_A); List<FeedItem> itemList = getTestList(); itemList.get(1) .getFeed().setTitle(null); assertTrue(checkIdOrder(itemList, 1, 3, 2)); permutor.reorder(itemList); assertTrue(checkIdOrder(itemList, 2, 1, 3)); }
@Test public void testPermutorForRule_EPISODE_TITLE_ASC() { Permutor<FeedItem> permutor = FeedItemPermutors.getPermutor(SortOrder.EPISODE_TITLE_A_Z); List<FeedItem> itemList = getTestList(); assertTrue(checkIdOrder(itemList, 1, 3, 2)); permutor.reorder(itemList); assertTrue(checkIdOrder(itemList, 1, 2, 3)); }
@Test public void testPermutorForRule_EPISODE_TITLE_ASC_NullTitle() { Permutor<FeedItem> permutor = FeedItemPermutors.getPermutor(SortOrder.EPISODE_TITLE_A_Z); List<FeedItem> itemList = getTestList(); itemList.get(2) .setTitle(null); assertTrue(checkIdOrder(itemList, 1, 3, 2)); permutor.reorder(itemList); assertTrue(checkIdOrder(itemList, 2, 1, 3)); }
@Test public void testPermutorForRule_EPISODE_TITLE_DESC() { Permutor<FeedItem> permutor = FeedItemPermutors.getPermutor(SortOrder.EPISODE_TITLE_Z_A); List<FeedItem> itemList = getTestList(); assertTrue(checkIdOrder(itemList, 1, 3, 2)); permutor.reorder(itemList); assertTrue(checkIdOrder(itemList, 3, 2, 1)); }
@Test public void testPermutorForRule_DATE_ASC() { Permutor<FeedItem> permutor = FeedItemPermutors.getPermutor(SortOrder.DATE_OLD_NEW); List<FeedItem> itemList = getTestList(); assertTrue(checkIdOrder(itemList, 1, 3, 2)); permutor.reorder(itemList); assertTrue(checkIdOrder(itemList, 1, 2, 3)); }
@Test public void testPermutorForRule_DATE_ASC_NulPubDatel() { Permutor<FeedItem> permutor = FeedItemPermutors.getPermutor(SortOrder.DATE_OLD_NEW); List<FeedItem> itemList = getTestList(); itemList.get(2) .setPubDate(null); assertTrue(checkIdOrder(itemList, 1, 3, 2)); permutor.reorder(itemList); assertTrue(checkIdOrder(itemList, 2, 1, 3)); }
@Test public void testPermutorForRule_DATE_DESC() { Permutor<FeedItem> permutor = FeedItemPermutors.getPermutor(SortOrder.DATE_NEW_OLD); List<FeedItem> itemList = getTestList(); assertTrue(checkIdOrder(itemList, 1, 3, 2)); permutor.reorder(itemList); assertTrue(checkIdOrder(itemList, 3, 2, 1)); }
@Test public void testPermutorForRule_DURATION_ASC() { Permutor<FeedItem> permutor = FeedItemPermutors.getPermutor(SortOrder.DURATION_SHORT_LONG); List<FeedItem> itemList = getTestList(); assertTrue(checkIdOrder(itemList, 1, 3, 2)); permutor.reorder(itemList); assertTrue(checkIdOrder(itemList, 1, 2, 3)); }
@Test public void testPermutorForRule_DURATION_DESC() { Permutor<FeedItem> permutor = FeedItemPermutors.getPermutor(SortOrder.DURATION_LONG_SHORT); List<FeedItem> itemList = getTestList(); assertTrue(checkIdOrder(itemList, 1, 3, 2)); permutor.reorder(itemList); assertTrue(checkIdOrder(itemList, 3, 2, 1)); }
@Test public void testPermutorForRule_DURATION_DESC_NullMedia() { Permutor<FeedItem> permutor = FeedItemPermutors.getPermutor(SortOrder.DURATION_LONG_SHORT); List<FeedItem> itemList = getTestList(); itemList.get(1) .setMedia(null); assertTrue(checkIdOrder(itemList, 1, 3, 2)); permutor.reorder(itemList); assertTrue(checkIdOrder(itemList, 2, 1, 3)); }