Search in sources :

Example 1 with ElementPropertyComparator

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

the class MaxHandler method getMax.

private Element getMax(final Iterable<? extends Element> elements, final Max operation) {
    Element maxElement = 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 maxProperty = 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 == maxElement || propertyComparator._compare(property, maxProperty) > 0) {
                maxElement = element;
                maxProperty = property;
            }
        }
    } else {
        final Comparator<Element> combinedComparator = operation.getCombinedComparator();
        if (null != combinedComparator) {
            for (final Element element : elements) {
                if (null == element) {
                    continue;
                }
                if (null == maxElement) {
                    maxElement = element;
                }
                if (combinedComparator.compare(element, maxElement) > 0) {
                    maxElement = element;
                }
            }
        }
    }
    return maxElement;
}
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)

Example 2 with ElementPropertyComparator

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

the class MinTest 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 Min min = new Min.Builder().input(input).comparators(comparator).build();
    // When
    Min clone = min.shallowClone();
    // Then
    assertNotSame(min, clone);
    assertThat(clone.getInput().iterator().next()).isEqualTo(input);
    assertThat(clone.getComparators().iterator().next()).isEqualTo(comparator);
}
Also used : Entity(uk.gov.gchq.gaffer.data.element.Entity) Builder(uk.gov.gchq.gaffer.operation.impl.compare.Min.Builder) ElementPropertyComparator(uk.gov.gchq.gaffer.data.element.comparison.ElementPropertyComparator) OperationTest(uk.gov.gchq.gaffer.operation.OperationTest) Test(org.junit.jupiter.api.Test)

Example 3 with ElementPropertyComparator

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

the class MinTest method builderShouldCreatePopulatedOperation.

@Test
@Override
public void builderShouldCreatePopulatedOperation() {
    // Given
    final Min min = new 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(min.getInput()).hasSize(2);
    List properties = Streams.toStream(min.getInput()).map(e -> e.getProperty("property")).collect(toList());
    assertThat(properties).containsOnly(1, 2);
}
Also used : Builder(uk.gov.gchq.gaffer.operation.impl.compare.Min.Builder) 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) Builder(uk.gov.gchq.gaffer.operation.impl.compare.Min.Builder) 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 4 with ElementPropertyComparator

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

the class SortTest method builderShouldCreatePopulatedOperation.

@Test
@Override
public void builderShouldCreatePopulatedOperation() {
    // Given
    final Sort sort = new 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(sort.getInput()).hasSize(2);
    List properties = Streams.toStream(sort.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) Builder(uk.gov.gchq.gaffer.operation.impl.compare.Sort.Builder) List(java.util.List) Collectors.toList(java.util.stream.Collectors.toList) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) Streams(uk.gov.gchq.gaffer.commonutil.stream.Streams) TestGroups(uk.gov.gchq.gaffer.commonutil.TestGroups) Builder(uk.gov.gchq.gaffer.operation.impl.compare.Sort.Builder) 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 5 with ElementPropertyComparator

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

the class MaxTest 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 Max max = new Max.Builder().input(input).comparators(comparator).build();
    // When
    Max clone = max.shallowClone();
    // Then
    assertNotSame(max, clone);
    assertThat(clone.getInput().iterator().next()).isEqualTo(input);
    assertThat(clone.getComparators().iterator().next()).isEqualTo(comparator);
}
Also used : 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)

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