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;
}
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);
}
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);
}
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);
}
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);
}
Aggregations