use of java.util.function.BinaryOperator in project rest.li by linkedin.
the class SchemaToAvroJsonEncoder method produceFieldProperties.
@SuppressWarnings("unchecked")
static /* package private */
Map<String, ?> produceFieldProperties(RecordDataSchema.Field field, DataToAvroSchemaTranslationOptions options) {
Stream<Map.Entry<String, Object>> toBeFiltered = field.getProperties().entrySet().stream();
// and merge with record field's properties.
if (field.getType().getType() == DataSchema.Type.TYPEREF) {
toBeFiltered = Stream.concat(toBeFiltered, ((TyperefDataSchema) field.getType()).getMergedTyperefProperties().entrySet().stream()).filter(entry -> !options.getTyperefPropertiesExcludeSet().contains(entry.getKey()));
}
// Property merge rule:
// For property content inherited from TypeRef that appears to be have same property name as the record field:
// if the two property contents are Map type, they will be merged at this level,
// otherwise Typeref field property content will be overridden by record field property's content.
BinaryOperator<Object> propertyMergeLogic = (originalPropertyContent, inheritedPropertyContent) -> {
if (originalPropertyContent instanceof Map && inheritedPropertyContent instanceof Map) {
Map<String, Object> mergedMap = new DataMap((Map<String, Object>) originalPropertyContent);
((Map<String, Object>) inheritedPropertyContent).forEach(mergedMap::putIfAbsent);
return mergedMap;
} else {
return originalPropertyContent;
}
};
return toBeFiltered.filter(entry -> !RESERVED_DATA_PROPERTIES.contains(entry.getKey())).collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, propertyMergeLogic));
}
use of java.util.function.BinaryOperator in project Gaffer by gchq.
the class SchemaElementDefinitionValidatorTest method shouldValidateAndReturnTrueWhenAggregatorIsValid.
@Test
public void shouldValidateAndReturnTrueWhenAggregatorIsValid() {
// Given
final SchemaElementDefinitionValidator validator = new SchemaElementDefinitionValidator();
final BinaryOperator<Integer> function1 = mock(BinaryOperator.class);
final BinaryOperator function2 = mock(BinaryOperator.class);
final Schema schema = new Schema.Builder().entity(TestGroups.ENTITY, new SchemaEntityDefinition.Builder().vertex("int1").property(TestPropertyNames.PROP_1, "int1").property(TestPropertyNames.PROP_2, "int2").build()).type("int1", new TypeDefinition.Builder().clazz(Integer.class).aggregateFunction(function1).build()).type("int2", new TypeDefinition.Builder().clazz(Integer.class).aggregateFunction(function2).build()).build();
// When
final ValidationResult result = validator.validate(schema.getEntity(TestGroups.ENTITY));
// Then
assertTrue(result.isValid());
}
use of java.util.function.BinaryOperator in project Gaffer by gchq.
the class ElementAggregatorTest method shouldAggregatePropertiesWithMultipleSelection.
@Test
public void shouldAggregatePropertiesWithMultipleSelection() {
// Given
final BinaryOperator<Tuple3<Integer, Integer, Integer>> maxMinRange = (t1, t2) -> new Tuple3<>(Math.max(t1.get0(), t2.get0()), Math.min(t1.get1(), t2.get1()), Math.max(t1.get0(), t2.get0()) - Math.min(t1.get1(), t2.get1()));
final ElementAggregator aggregator = new ElementAggregator.Builder().select("max", "min", "range").execute(maxMinRange).build();
final Properties properties1 = new Properties();
properties1.put("max", 10);
properties1.put("min", 10);
final Properties properties2 = new Properties();
properties2.put("max", 100);
properties2.put("min", 100);
final Properties properties3 = new Properties();
properties3.put("max", 1000);
properties3.put("min", 1000);
// When
Properties state = aggregator.apply(properties1, properties2);
state = aggregator.apply(state, properties3);
// Then
assertThat(state).hasFieldOrPropertyWithValue("max", 1000).hasFieldOrPropertyWithValue("min", 10).hasFieldOrPropertyWithValue("range", 990);
}
use of java.util.function.BinaryOperator in project Gaffer by gchq.
the class ElementAggregatorTest method shouldBuildAggregator.
@Test
public void shouldBuildAggregator() {
// Given
final String property1 = "property 1";
final String property2a = "property 2a";
final String property2b = "property 2b";
final String property3 = "property 3";
final BinaryOperator func1 = mock(BinaryOperator.class);
final BinaryOperator func2 = mock(BinaryOperator.class);
final BinaryOperator func3 = mock(BinaryOperator.class);
// When - check you can build the selection/function in any order,
// although normally it will be done - select then execute.
final ElementAggregator aggregator = new ElementAggregator.Builder().select(property1).execute(func1).select(property2a, property2b).execute(func2).select(property3).execute(func3).build();
// Then
int i = 0;
TupleAdaptedBinaryOperator<String, ?> adaptedFunction = aggregator.getComponents().get(i++);
assertEquals(1, adaptedFunction.getSelection().length);
assertEquals(property1, adaptedFunction.getSelection()[0]);
assertSame(func1, adaptedFunction.getBinaryOperator());
adaptedFunction = aggregator.getComponents().get(i++);
assertThat(adaptedFunction.getSelection()).hasSize(2);
assertEquals(property2a, adaptedFunction.getSelection()[0]);
assertEquals(property2b, adaptedFunction.getSelection()[1]);
assertSame(func2, adaptedFunction.getBinaryOperator());
adaptedFunction = aggregator.getComponents().get(i++);
assertSame(func3, adaptedFunction.getBinaryOperator());
assertThat(adaptedFunction.getSelection()).hasSize(1);
assertEquals(property3, adaptedFunction.getSelection()[0]);
assertEquals(i, aggregator.getComponents().size());
}
use of java.util.function.BinaryOperator in project Gaffer by gchq.
the class AggregateValidator method validateElementAggregator.
private ValidationResult validateElementAggregator(final Map.Entry<String, ?> entry, final Schema schema) {
final ValidationResult result = new ValidationResult();
List<TupleAdaptedBinaryOperator<String, ?>> aggregateFunctions = new ArrayList<>();
final SchemaElementDefinition schemaElement = schema.getElement(entry.getKey());
if (null != schemaElement) {
aggregateFunctions = schemaElement.getOriginalAggregateFunctions();
}
List<BinaryOperator<?>> schemaOperators = new ArrayList<>();
if (null != aggregateFunctions) {
schemaOperators = Streams.toStream(aggregateFunctions).map(AdaptedBinaryOperator::getBinaryOperator).collect(Collectors.toList());
}
if (schemaOperators.contains(null)) {
result.addError("Schema contains an ElementAggregator with a null function.");
}
final AggregatePair pair = (AggregatePair) entry.getValue();
final ElementAggregator aggregator = pair.getElementAggregator();
if (null != aggregator && null != aggregator.getComponents()) {
for (final TupleAdaptedBinaryOperator<String, ?> adaptedFunction : aggregator.getComponents()) {
if (null == adaptedFunction.getBinaryOperator()) {
result.addError(aggregator.getClass().getSimpleName() + " contains a null function.");
}
}
}
return result;
}
Aggregations