ElementPropertyComparator implements ElementComparator { @Override public int compare(final Element e1, final Element e2) { count++; if (null == e1) { if (null == e2) { return 0; } return 1; } if (null == e2) { return -1; } if (!groups.contains(e1.getGroup())) { if (!groups.contains(e2.getGroup())) { return 0; } return 1; } if (!groups.contains(e2.getGroup())) { return -1; } return _compare(e1.getProperty(property), e2.getProperty(property)); }  @Override int compare(final Element e1, final Element e2); int _compare(final Object val1, final Object val2); @Override Set<Pair<String, String>> getComparableGroupPropertyPairs(); String getProperty(); void setProperty(final String property); Set<String> getGroups(); void setGroups(final Set<String> groups); boolean isReversed(); void setReversed(final boolean reversed); @JsonTypeInfo(use = JsonTypeInfo.Id.CLASS, include = JsonTypeInfo.As.PROPERTY, property = "class") Comparator getComparator(); void setComparator(final Comparator comparator); static int count; }
@Test public void shouldCompare() { final Entity smallEntity = makeEntity(TestGroups.ENTITY, TestPropertyNames.PROP_1, 1); final Entity largeEntity = makeEntity(TestGroups.ENTITY, TestPropertyNames.PROP_1, 2); final ElementPropertyComparator comparator = new ElementPropertyComparator.Builder() .groups(TestGroups.ENTITY) .property(TestPropertyNames.PROP_1) .comparator(new ComparatorImpl()) .build(); final int result = comparator.compare(smallEntity, largeEntity); assertThat(result, lessThan(0)); }
@Test public void shouldCompareWhenBothElementsHaveMissingProperties() { final Entity smallEntity = makeEntity(TestGroups.ENTITY, TestPropertyNames.PROP_1, 1); final Entity largeEntity = makeEntity(TestGroups.ENTITY, TestPropertyNames.PROP_1, 2); final ElementPropertyComparator comparator = new ElementPropertyComparator.Builder() .groups(TestGroups.ENTITY) .property(TestPropertyNames.PROP_2) .comparator(new ComparatorImpl()) .build(); final int result = comparator.compare(smallEntity, largeEntity); assertThat(result, equalTo(0)); }
@Test public void shouldCompareWhenFirstElementHasMissingProperties() { final Entity smallEntity = makeEntity(TestGroups.ENTITY, TestPropertyNames.PROP_1, 1); final Entity largeEntity = makeEntity(TestGroups.ENTITY, TestPropertyNames.PROP_2, 2); final ElementPropertyComparator comparator = new ElementPropertyComparator.Builder() .groups(TestGroups.ENTITY) .property(TestPropertyNames.PROP_2) .comparator(new ComparatorImpl()) .build(); final int result = comparator.compare(smallEntity, largeEntity); assertThat(result, greaterThan(0)); }
@Test public void shouldCompareWhenSecondElementHasMissingProperties() { final Entity smallEntity = makeEntity(TestGroups.ENTITY, TestPropertyNames.PROP_2, 1); final Entity largeEntity = makeEntity(TestGroups.ENTITY, TestPropertyNames.PROP_1, 2); final ElementPropertyComparator comparator = new ElementPropertyComparator.Builder() .groups(TestGroups.ENTITY) .property(TestPropertyNames.PROP_2) .comparator(new ComparatorImpl()) .build(); final int result = comparator.compare(smallEntity, largeEntity); assertThat(result, lessThan(0)); }
@Test public void shouldCompareWhenBothElementsHaveWrongGroup() { final Entity smallEntity = makeEntity(TestGroups.ENTITY, TestPropertyNames.PROP_1, 1); final Entity largeEntity = makeEntity(TestGroups.ENTITY, TestPropertyNames.PROP_1, 2); final ElementPropertyComparator comparator = new ElementPropertyComparator.Builder() .groups(TestGroups.ENTITY_2) .property(TestPropertyNames.PROP_1) .comparator(new ComparatorImpl()) .build(); final int result = comparator.compare(smallEntity, largeEntity); assertThat(result, equalTo(0)); }
@Test public void shouldCompareWhenFirstElementsHasWrongGroup() { final Entity smallEntity = makeEntity(TestGroups.ENTITY, TestPropertyNames.PROP_1, 1); final Entity largeEntity = makeEntity(TestGroups.ENTITY_2, TestPropertyNames.PROP_1, 2); final ElementPropertyComparator comparator = new ElementPropertyComparator.Builder() .groups(TestGroups.ENTITY_2) .property(TestPropertyNames.PROP_1) .comparator(new ComparatorImpl()) .build(); final int result = comparator.compare(smallEntity, largeEntity); assertThat(result, greaterThan(0)); }
@Test public void shouldCompareWhenSecondElementsHasWrongGroup() { final Entity smallEntity = makeEntity(TestGroups.ENTITY_2, TestPropertyNames.PROP_1, 1); final Entity largeEntity = makeEntity(TestGroups.ENTITY, TestPropertyNames.PROP_1, 2); final ElementPropertyComparator comparator = new ElementPropertyComparator.Builder() .groups(TestGroups.ENTITY_2) .property(TestPropertyNames.PROP_1) .comparator(new ComparatorImpl()) .build(); final int result = comparator.compare(smallEntity, largeEntity); assertThat(result, lessThan(0)); }
@Test public void shouldCompareReversed() { final Entity smallEntity = makeEntity(TestGroups.ENTITY, TestPropertyNames.PROP_1, 1); final Entity largeEntity = makeEntity(TestGroups.ENTITY, TestPropertyNames.PROP_1, 2); final ElementComparator comparator = new ElementPropertyComparator.Builder() .groups(TestGroups.ENTITY) .property(TestPropertyNames.PROP_1) .comparator(new ComparatorImpl()) .reverse(true) .build(); final int result = comparator.compare(smallEntity, largeEntity); assertThat(result, greaterThan(0)); }
@Test public void shouldCompareWithNoProvidedComparatorInstance() { final Entity smallEntity = makeEntityWithPropertyValueAsPrimitiveInt(1); final Entity largeEntity = makeEntityWithPropertyValueAsPrimitiveInt(2); final ElementPropertyComparator comparator = new ElementPropertyComparator.Builder() .groups(TestGroups.ENTITY) .property(TestPropertyNames.PROP_1) .build(); final int result = comparator.compare(smallEntity, largeEntity); assertThat(result, lessThan(0)); }