Search in sources :

Example 6 with ElementPropertyComparator

use of uk.gov.gchq.gaffer.data.element.comparison.ElementPropertyComparator in project Gaffer by gchq.

the class MaxTest method builderShouldCreatePopulatedOperation.

@Test
@Override
public void builderShouldCreatePopulatedOperation() {
    // Given
    final Max max = new Max.Builder().input(new Entity.Builder().group(TestGroups.ENTITY).property("property", 1).build(), new Entity.Builder().group(TestGroups.ENTITY).property("property", 2).build()).comparators(new ElementPropertyComparator() {

        @Override
        public int compare(final Element e1, final Element e2) {
            return 0;
        }
    }).build();
    // Then
    assertThat(max.getInput()).hasSize(2);
    List properties = Streams.toStream(max.getInput()).map(e -> e.getProperty("property")).collect(toList());
    assertThat(properties).containsOnly(1, 2);
}
Also used : OperationTest(uk.gov.gchq.gaffer.operation.OperationTest) ElementPropertyComparator(uk.gov.gchq.gaffer.data.element.comparison.ElementPropertyComparator) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Set(java.util.Set) Entity(uk.gov.gchq.gaffer.data.element.Entity) Assertions.assertNotSame(org.junit.jupiter.api.Assertions.assertNotSame) Sets(com.google.common.collect.Sets) Element(uk.gov.gchq.gaffer.data.element.Element) Test(org.junit.jupiter.api.Test) List(java.util.List) Collectors.toList(java.util.stream.Collectors.toList) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) Streams(uk.gov.gchq.gaffer.commonutil.stream.Streams) TestGroups(uk.gov.gchq.gaffer.commonutil.TestGroups) Element(uk.gov.gchq.gaffer.data.element.Element) List(java.util.List) Collectors.toList(java.util.stream.Collectors.toList) ElementPropertyComparator(uk.gov.gchq.gaffer.data.element.comparison.ElementPropertyComparator) OperationTest(uk.gov.gchq.gaffer.operation.OperationTest) Test(org.junit.jupiter.api.Test)

Example 7 with ElementPropertyComparator

use of uk.gov.gchq.gaffer.data.element.comparison.ElementPropertyComparator in project Gaffer by gchq.

the class SortTest method shouldShallowCloneOperation.

@Test
@Override
public void shouldShallowCloneOperation() {
    // Given
    final Entity input = new Entity.Builder().group(TestGroups.ENTITY).property("property", 1).build();
    final ElementPropertyComparator comparator = new ElementPropertyComparator();
    final Boolean deDuplicate = false;
    final int resultLimit = 5;
    final Sort sort = new Sort.Builder().input(input).comparators(comparator).resultLimit(resultLimit).deduplicate(deDuplicate).build();
    // When
    Sort clone = sort.shallowClone();
    // Then
    assertNotSame(sort, clone);
    assertThat(clone.getInput().iterator().next()).isEqualTo(input);
    assertThat(clone.getComparators().iterator().next()).isEqualTo(comparator);
    assertEquals(deDuplicate, clone.isDeduplicate());
    assertTrue(clone.getResultLimit().equals(resultLimit));
}
Also used : Builder(uk.gov.gchq.gaffer.operation.impl.compare.Sort.Builder) Entity(uk.gov.gchq.gaffer.data.element.Entity) ElementPropertyComparator(uk.gov.gchq.gaffer.data.element.comparison.ElementPropertyComparator) OperationTest(uk.gov.gchq.gaffer.operation.OperationTest) Test(org.junit.jupiter.api.Test)

Example 8 with ElementPropertyComparator

use of uk.gov.gchq.gaffer.data.element.comparison.ElementPropertyComparator in project Gaffer by gchq.

the class MinHandler method getMin.

private Element getMin(final Iterable<? extends Element> elements, final Min operation) {
    Element minElement = null;
    final List<Comparator<Element>> comparators = operation.getComparators();
    if (1 == comparators.size() && comparators.get(0) instanceof ElementPropertyComparator) {
        final ElementPropertyComparator propertyComparator = (ElementPropertyComparator) comparators.get(0);
        Object minProperty = null;
        for (final Element element : elements) {
            if (null == element || !propertyComparator.getGroups().contains(element.getGroup())) {
                continue;
            }
            final Object property = element.getProperty(propertyComparator.getProperty());
            if (null == property) {
                continue;
            }
            if (null == minElement || propertyComparator._compare(property, minProperty) < 0) {
                minElement = element;
                minProperty = property;
            }
        }
    } else {
        final Comparator<Element> combinedComparator = operation.getCombinedComparator();
        if (null != combinedComparator) {
            for (final Element element : elements) {
                if (null == element) {
                    continue;
                }
                if (null == minElement) {
                    minElement = element;
                }
                if (combinedComparator.compare(element, minElement) < 0) {
                    minElement = element;
                }
            }
        }
    }
    return minElement;
}
Also used : Element(uk.gov.gchq.gaffer.data.element.Element) ElementPropertyComparator(uk.gov.gchq.gaffer.data.element.comparison.ElementPropertyComparator) ElementPropertyComparator(uk.gov.gchq.gaffer.data.element.comparison.ElementPropertyComparator) Comparator(java.util.Comparator)

Aggregations

ElementPropertyComparator (uk.gov.gchq.gaffer.data.element.comparison.ElementPropertyComparator)8 Test (org.junit.jupiter.api.Test)6 Entity (uk.gov.gchq.gaffer.data.element.Entity)6 OperationTest (uk.gov.gchq.gaffer.operation.OperationTest)6 Element (uk.gov.gchq.gaffer.data.element.Element)5 Sets (com.google.common.collect.Sets)3 List (java.util.List)3 Set (java.util.Set)3 Collectors.toList (java.util.stream.Collectors.toList)3 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)3 Assertions.assertEquals (org.junit.jupiter.api.Assertions.assertEquals)3 Assertions.assertNotSame (org.junit.jupiter.api.Assertions.assertNotSame)3 TestGroups (uk.gov.gchq.gaffer.commonutil.TestGroups)3 Streams (uk.gov.gchq.gaffer.commonutil.stream.Streams)3 Comparator (java.util.Comparator)2 Builder (uk.gov.gchq.gaffer.operation.impl.compare.Min.Builder)2 Builder (uk.gov.gchq.gaffer.operation.impl.compare.Sort.Builder)2 Assertions.assertTrue (org.junit.jupiter.api.Assertions.assertTrue)1