org.springframework.util.comparator.NullSafeComparator Java Examples

The following examples show how to use org.springframework.util.comparator.NullSafeComparator. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ScreenshotService.java    From mojito with Apache License 2.0 5 votes vote down vote up
void sortScreenshotBySequence(List<Screenshot> screenshots) {
    Collections.sort(screenshots, new Comparator<Screenshot>() {
        @Override
        public int compare(Screenshot o1, Screenshot o2) {
            return NullSafeComparator.NULLS_HIGH.compare(o1.getSequence(), o2.getSequence());
        }
    });
}
 
Example #2
Source File: UserSortNameComparator.java    From sakai with Educational Community License v2.0 5 votes vote down vote up
public int compare(User u1, User u2) {
    if (u1 == u2) return 0;
    if (u1 == null) return (nullsLow ? -1 : 1);
    if (u2 == null) return (nullsLow ? 1 : -1);

    return new NullSafeComparator<>(collator, nullsLow).compare(u1.getSortName(), u2.getSortName());
}
 
Example #3
Source File: StatsUpdateManagerImpl.java    From sakai with Educational Community License v2.0 5 votes vote down vote up
@Override
public int compareTo(SitePresenceConsolidation other) {
	int val = sitePresence.compareTo(other.sitePresence);
	if (val != 0) return val;
	val = NullSafeComparator.NULLS_HIGH.compare(firstPresEndDate, other.firstPresEndDate);
	if (val != 0) return val;
	return (firstEventIsPresEnd?1:0) - (other.firstEventIsPresEnd?1:0);
}
 
Example #4
Source File: SitePresenceImpl.java    From sakai with Educational Community License v2.0 5 votes vote down vote up
@Override
public int compareTo(SitePresence other) {
	int val = siteId.compareTo(other.getSiteId());
	if (val != 0) return val;
	val = userId.compareTo(other.getUserId());
	if (val != 0) return val;
	val = Long.signum(duration - other.getDuration());
	if (val != 0) return val;
	val = NullSafeComparator.NULLS_HIGH.compare(date, other.getDate());
	if (val != 0) return val;
	val = NullSafeComparator.NULLS_HIGH.compare(lastVisitStartTime, other.getLastVisitStartTime());
	if (val != 0) return val;
	val = Long.signum(id - other.getId());
	return val;
}
 
Example #5
Source File: UserSortNameComparator.java    From sakai with Educational Community License v2.0 5 votes vote down vote up
public int compare(User u1, User u2) {
    if (u1 == u2) return 0;
    if (u1 == null) return (nullsLow ? -1 : 1);
    if (u2 == null) return (nullsLow ? 1 : -1);

    return new NullSafeComparator<>(collator, nullsLow).compare(u1.getSortName(), u2.getSortName());
}
 
Example #6
Source File: StatsUpdateManagerImpl.java    From sakai with Educational Community License v2.0 5 votes vote down vote up
@Override
public int compareTo(SitePresenceConsolidation other) {
	int val = sitePresence.compareTo(other.sitePresence);
	if (val != 0) return val;
	val = NullSafeComparator.NULLS_HIGH.compare(firstPresEndDate, other.firstPresEndDate);
	if (val != 0) return val;
	return (firstEventIsPresEnd?1:0) - (other.firstEventIsPresEnd?1:0);
}
 
Example #7
Source File: SitePresenceImpl.java    From sakai with Educational Community License v2.0 5 votes vote down vote up
@Override
public int compareTo(SitePresence other) {
	int val = siteId.compareTo(other.getSiteId());
	if (val != 0) return val;
	val = userId.compareTo(other.getUserId());
	if (val != 0) return val;
	val = Long.signum(duration - other.getDuration());
	if (val != 0) return val;
	val = NullSafeComparator.NULLS_HIGH.compare(date, other.getDate());
	if (val != 0) return val;
	val = NullSafeComparator.NULLS_HIGH.compare(lastVisitStartTime, other.getLastVisitStartTime());
	if (val != 0) return val;
	val = Long.signum(id - other.getId());
	return val;
}
 
Example #8
Source File: UserDaoTestCase.java    From airsonic-advanced with GNU General Public License v3.0 4 votes vote down vote up
@Test
public void testUserSettings() {
    assertNull("Error in getUserSettings.", userDao.getUserSettings("sindre"));

    assertThatExceptionOfType(DataIntegrityViolationException.class)
            .isThrownBy(() -> updateUserSettings(new UserSettings("sindre")));

    userDao.createUser(new User("sindre", null), new UserCredential("sindre", "sindre", "secret", "noop", App.AIRSONIC));
    assertNull("Error in getUserSettings.", userDao.getUserSettings("sindre"));

    UserSettings settings = new UserSettings("sindre");
    userDao.updateUserSettings(settings);
    UserSettings userSettings = userDao.getUserSettings("sindre");
    assertThat(userSettings).usingComparatorForType(new NullSafeComparator<Instant>(new Comparator<Instant>() {
        // use a custom comparator to account for micro second differences
        // (Mysql only stores floats in json to a certain value)
        @Override
        public int compare(Instant o1, Instant o2) {
            if (o1.equals(o2) || Math.abs(ChronoUnit.MICROS.between(o1, o2)) <= 2) {
                return 0;
            }
            return o1.compareTo(o2);
        }
    }, true), Instant.class)
            .usingRecursiveComparison().isEqualTo(settings);

    settings = new UserSettings("sindre");
    settings.setLocale(Locale.SIMPLIFIED_CHINESE);
    settings.setThemeId("midnight");
    settings.setBetaVersionNotificationEnabled(true);
    settings.setSongNotificationEnabled(false);
    settings.setShowSideBar(true);
    settings.getMainVisibility().setBitRateVisible(true);
    settings.getPlaylistVisibility().setYearVisible(true);
    settings.setLastFmEnabled(true);
    settings.setListenBrainzEnabled(true);
    settings.setTranscodeScheme(TranscodeScheme.MAX_192);
    settings.setShowNowPlayingEnabled(false);
    settings.setSelectedMusicFolderId(3);
    settings.setPartyModeEnabled(true);
    settings.setNowPlayingAllowed(true);
    settings.setAvatarScheme(AvatarScheme.SYSTEM);
    settings.setSystemAvatarId(1);
    settings.setChanged(Instant.ofEpochMilli(9412L));
    settings.setKeyboardShortcutsEnabled(true);
    settings.setPaginationSize(120);

    userDao.updateUserSettings(settings);
    userSettings = userDao.getUserSettings("sindre");
    assertThat(userSettings).usingRecursiveComparison().isEqualTo(settings);

    userDao.deleteUser("sindre");
    assertNull("Error in cascading delete.", userDao.getUserSettings("sindre"));
}