Java 类org.springframework.util.comparator.CompoundComparator 实例源码

项目:spring4-understanding    文件:PropertyComparatorTests.java   
@SuppressWarnings("unchecked")
@Test
public void testCompoundComparator() {
    CompoundComparator<Dog> c = new CompoundComparator<Dog>();
    c.addComparator(new PropertyComparator("lastName", false, true));

    Dog dog1 = new Dog();
    dog1.setFirstName("macy");
    dog1.setLastName("grayspots");

    Dog dog2 = new Dog();
    dog2.setFirstName("biscuit");
    dog2.setLastName("grayspots");

    assertTrue(c.compare(dog1, dog2) == 0);

    c.addComparator(new PropertyComparator("firstName", false, true));
    assertTrue(c.compare(dog1, dog2) > 0);

    dog2.setLastName("konikk dog");
    assertTrue(c.compare(dog2, dog1) > 0);
}
项目:spring4-understanding    文件:PropertyComparatorTests.java   
@SuppressWarnings("unchecked")
@Test
public void testCompoundComparatorInvert() {
    CompoundComparator<Dog> c = new CompoundComparator<Dog>();
    c.addComparator(new PropertyComparator("lastName", false, true));
    c.addComparator(new PropertyComparator("firstName", false, true));
    Dog dog1 = new Dog();
    dog1.setFirstName("macy");
    dog1.setLastName("grayspots");

    Dog dog2 = new Dog();
    dog2.setFirstName("biscuit");
    dog2.setLastName("grayspots");

    assertTrue(c.compare(dog1, dog2) > 0);
    c.invertOrder();
    assertTrue(c.compare(dog1, dog2) < 0);
}
项目:class-guard    文件:PropertyComparatorTests.java   
@SuppressWarnings("unchecked")
@Test
public void testCompoundComparator() {
    CompoundComparator<Dog> c = new CompoundComparator<Dog>();
    c.addComparator(new PropertyComparator("lastName", false, true));

    Dog dog1 = new Dog();
    dog1.setFirstName("macy");
    dog1.setLastName("grayspots");

    Dog dog2 = new Dog();
    dog2.setFirstName("biscuit");
    dog2.setLastName("grayspots");

    assertTrue(c.compare(dog1, dog2) == 0);

    c.addComparator(new PropertyComparator("firstName", false, true));
    assertTrue(c.compare(dog1, dog2) > 0);

    dog2.setLastName("konikk dog");
    assertTrue(c.compare(dog2, dog1) > 0);
}
项目:class-guard    文件:PropertyComparatorTests.java   
@SuppressWarnings("unchecked")
@Test
public void testCompoundComparatorInvert() {
    CompoundComparator<Dog> c = new CompoundComparator<Dog>();
    c.addComparator(new PropertyComparator("lastName", false, true));
    c.addComparator(new PropertyComparator("firstName", false, true));
    Dog dog1 = new Dog();
    dog1.setFirstName("macy");
    dog1.setLastName("grayspots");

    Dog dog2 = new Dog();
    dog2.setFirstName("biscuit");
    dog2.setLastName("grayspots");

    assertTrue(c.compare(dog1, dog2) > 0);
    c.invertOrder();
    assertTrue(c.compare(dog1, dog2) < 0);
}
项目:lams    文件:MediaType.java   
/**
 * Sorts the given list of {@code MediaType} objects by specificity as the
 * primary criteria and quality value the secondary.
 * @see MediaType#sortBySpecificity(List)
 * @see MediaType#sortByQualityValue(List)
 */
public static void sortBySpecificityAndQuality(List<MediaType> mediaTypes) {
    Assert.notNull(mediaTypes, "'mediaTypes' must not be null");
    if (mediaTypes.size() > 1) {
        Collections.sort(mediaTypes, new CompoundComparator<MediaType>(
                MediaType.SPECIFICITY_COMPARATOR, MediaType.QUALITY_VALUE_COMPARATOR));
    }
}
项目:spring-data-mapdb    文件:MapDbSortAccessor.java   
/**
 * Sort on a sequence of fields, possibly none.
 *
 * @param query
 *            If not null, will contain one of more {@link Sort.Order}
 *            objects.
 * @return A sequence of comparators or {@code null}
 */
public Comparator<?> resolve(KeyValueQuery<?> query) {

    if (query == null || query.getSort() == null) {
        return null;
    }

    CompoundComparator<Object> compoundComparator = new CompoundComparator<>();

    for (Order order : query.getSort()) {

        if (order.getProperty().indexOf('.') > -1) {
            throw new UnsupportedOperationException("Embedded fields not implemented: " + order);
        }

        if (order.isIgnoreCase()) {
            throw new UnsupportedOperationException("Ignore case not implemented: " + order);
        }

        if (NullHandling.NATIVE != order.getNullHandling()) {
            throw new UnsupportedOperationException("Null handling not implemented: " + order);
        }

        PropertyComparator propertyComparator = new PropertyComparator(order.getProperty(),
                order.isAscending());

        compoundComparator.addComparator(propertyComparator);
    }

    return compoundComparator;
}
项目:spring4-understanding    文件:MediaType.java   
/**
 * Sorts the given list of {@code MediaType} objects by specificity as the
 * primary criteria and quality value the secondary.
 * @see MediaType#sortBySpecificity(List)
 * @see MediaType#sortByQualityValue(List)
 */
public static void sortBySpecificityAndQuality(List<MediaType> mediaTypes) {
    Assert.notNull(mediaTypes, "'mediaTypes' must not be null");
    if (mediaTypes.size() > 1) {
        Collections.sort(mediaTypes, new CompoundComparator<MediaType>(
                MediaType.SPECIFICITY_COMPARATOR, MediaType.QUALITY_VALUE_COMPARATOR));
    }
}
项目:class-guard    文件:MediaType.java   
/**
 * Sorts the given list of {@code MediaType} objects by specificity as the
 * primary criteria and quality value the secondary.
 * @see MediaType#sortBySpecificity(List)
 * @see MediaType#sortByQualityValue(List)
 */
public static void sortBySpecificityAndQuality(List<MediaType> mediaTypes) {
    Assert.notNull(mediaTypes, "'mediaTypes' must not be null");
    if (mediaTypes.size() > 1) {
        Collections.sort(mediaTypes, new CompoundComparator<MediaType>(
                MediaType.SPECIFICITY_COMPARATOR, MediaType.QUALITY_VALUE_COMPARATOR));
    }
}
项目:spring-data-hazelcast    文件:HazelcastSortAccessor.java   
/**
 * <P>
 * Sort on a sequence of fields, possibly none.
 * </P>
 *
 * @param query If not null, will contain one of more {@link Sort.Order} objects.
 * @return A sequence of comparators or {@code null}
 */
public Comparator<Entry<?, ?>> resolve(KeyValueQuery<?> query) {

    if (query == null || query.getSort() == null) {
        return null;
    }

    CompoundComparator<Entry<?, ?>> compoundComparator = new CompoundComparator<>();

    for (Order order : query.getSort()) {

        if (order.getProperty().indexOf('.') > -1) {
            throw new UnsupportedOperationException("Embedded fields not implemented: " + order);
        }

        if (order.isIgnoreCase()) {
            throw new UnsupportedOperationException("Ignore case not implemented: " + order);
        }

        if (NullHandling.NATIVE != order.getNullHandling()) {
            throw new UnsupportedOperationException("Null handling not implemented: " + order);
        }

        HazelcastPropertyComparator hazelcastPropertyComparator = new HazelcastPropertyComparator(order.getProperty(),
                order.isAscending());

        compoundComparator.addComparator(hazelcastPropertyComparator);
    }

    return compoundComparator;
}
项目:spring-rich-client    文件:LabeledEnumComboBoxBinding.java   
protected void doBindControl(ListModel bindingModel) {
    setRenderer(new LabeledEnumListRenderer(getMessageSource()));
    setEditor(new LabeledEnumComboBoxEditor(getMessageSource(), getEditor()));
    CompoundComparator comparator = new CompoundComparator();
    comparator.addComparator(LabeledEnum.LABEL_ORDER);
    comparator.addComparator(new ComparableComparator());
    setComparator(comparator);
    super.doBindControl(bindingModel);
}
项目:spring-rich-client    文件:DefaultComponentFactory.java   
public void configureForEnum(JComboBox comboBox, Class enumType) {
    Collection enumValues = getEnumResolver().getLabeledEnumSet(enumType);
    if (logger.isDebugEnabled()) {
        logger.debug("Populating combo box model with enums of type '" + enumType.getName() + "'; enums are ["
                + enumValues + "]");
    }
    CompoundComparator comparator = new CompoundComparator();
    comparator.addComparator(LabeledEnum.LABEL_ORDER);
    comparator.addComparator(new ComparableComparator());
    comboBox.setModel(new ComboBoxListModel(new ArrayList(enumValues), comparator));
    comboBox.setRenderer(new LabeledEnumListRenderer(messageSource));
    comboBox.setEditor(new LabeledEnumComboBoxEditor(messageSource, comboBox.getEditor()));
}
项目:spring-richclient    文件:LabeledEnumComboBoxBinding.java   
protected void doBindControl(ListModel bindingModel) {
    setRenderer(new LabeledEnumListRenderer(getMessageSource()));
    setEditor(new LabeledEnumComboBoxEditor(getMessageSource(), getEditor()));
    CompoundComparator comparator = new CompoundComparator();
    comparator.addComparator(LabeledEnum.LABEL_ORDER);
    comparator.addComparator(new ComparableComparator());
    setComparator(comparator);
    super.doBindControl(bindingModel);
}
项目:spring-richclient    文件:DefaultComponentFactory.java   
public void configureForEnum(JComboBox comboBox, Class enumType) {
    Collection enumValues = getEnumResolver().getLabeledEnumSet(enumType);
    if (logger.isDebugEnabled()) {
        logger.debug("Populating combo box model with enums of type '" + enumType.getName() + "'; enums are ["
                + enumValues + "]");
    }
    CompoundComparator comparator = new CompoundComparator();
    comparator.addComparator(LabeledEnum.LABEL_ORDER);
    comparator.addComparator(new ComparableComparator());
    comboBox.setModel(new ComboBoxListModel(new ArrayList(enumValues), comparator));
    comboBox.setRenderer(new LabeledEnumListRenderer(messageSource));
    comboBox.setEditor(new LabeledEnumComboBoxEditor(messageSource, comboBox.getEditor()));
}
项目:spring-boot-readiness    文件:SortCompare.java   
public Comparator<HealthResponse> getComparator(@NotNull Sort sort) {
    return stream(checkNotNull(sort).spliterator(), false)
            .map(orderCompare)
            .collect(CompoundComparator::new, CompoundComparator::addComparator, CompoundComparator::addComparator);
}