Comparator
As for sorting Collection, it is more efficient to implement either comparator or comparable and put all the compare method in one place.
----------------------------------------------------------------------------------------------------------------------
Collections.sort(criticalAndImportantUpdates, new CriticalAndImportantUpdatesComparator(themeDisplay, contentStatusLocalService).acknowledgedComparator);
Collections.sort(criticalAndImportantUpdates, new CriticalAndImportantUpdatesComparator(themeDisplay, contentStatusLocalService).dateComparator);
----------------------------------------------------------------------------------------------------------------------
public class CriticalAndImportantUpdatesComparator {
private ThemeDisplay themeDisplay;
public CriticalAndImportantUpdatesComparator(ThemeDisplay themeDisplay, ContentStatusLocalService contentStatusLocalService) {
this.themeDisplay = themeDisplay;
this.contentStatusLocalService = contentStatusLocalService;
}
public Comparator<JournalArticle> acknowledgedComparator = new Comparator<JournalArticle>() {
public int compare(JournalArticle a, JournalArticle b) {
ContentStatus contentStatusA = contentStatusLocalService.fetchByU_C_C_First(
themeDisplay.getUserId(), JournalArticle.class.getName(),
a.getResourcePrimKey(), null);
ContentStatus contentStatusB = contentStatusLocalService.fetchByU_C_C_First(
themeDisplay.getUserId(), JournalArticle.class.getName(),
b.getResourcePrimKey(), null);
String acknowledgedA = contentStatusA.getAcknowledegedStatus();
String acknowledgedB = contentStatusB.getAcknowledegedStatus();
if (acknowledgedA.equals(CriticalDashboardPortletConstants.TRUE) &&
acknowledgedB.equals(CriticalDashboardPortletConstants.FALSE)) {
return -1;
} else if (acknowledgedA.equals(CriticalDashboardPortletConstants.FALSE) &&
acknowledgedB.equals(CriticalDashboardPortletConstants.TRUE)) {
return 1;
}
return 0;
}
};
public Comparator<JournalArticle> dateComparator = new Comparator<JournalArticle>() {
public int compare(JournalArticle a, JournalArticle b) {
return a.getModifiedDate().compareTo(b.getModifiedDate());
}
};
@Reference
private ContentStatusLocalService contentStatusLocalService;
}
Comments
Post a Comment