@Override public Object apply(final Element element) { return null != element ? element.getProperty(name) : null; }
@Override public Object get(final String reference) { if (ELEMENT.equals(reference)) { return element; } if (PROPERTIES.equals(reference)) { return element.getProperties(); } final IdentifierType idType = IdentifierType.fromName(reference); if (null == idType) { return element.getProperty(reference); } return element.getIdentifier(idType); }
@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)); }
/** * Attempts to find the value of a field from a given {@link Element}, * corresponding to a provided key, where the key is the name of the field. * * @param element the Element from which to retrieve a field value * @param key the name of the field to be retrieved * @return the value of the field */ private Object getFieldValue(final Element element, final String key) { final IdentifierType idType = IdentifierType.fromName(key); final Object value; if (null == idType) { if (GROUP.equals(key)) { value = element.getGroup(); } else { value = element.getProperty(key); } } else { value = element.getIdentifier(idType); } return value; }
/** * Attempts to find the value of a field from a given {@link Element}, * corresponding to a provided key, where the key is the name of the field. * * @param element the Element from which to retrieve a field value * @param key the name of the field to be retrieved * @return the value of the field */ private Object getFieldValue(final Element element, final String key) { final IdentifierType idType = IdentifierType.fromName(key); final Object value; if (null == idType) { if (GROUP.equals(key)) { value = element.getGroup(); } else { value = element.getProperty(key); } } else { value = element.getIdentifier(idType); } return value; }
if (!Objects.equals(element.getProperty(key), element2.getProperty(key))) { return false;
@Test public void shouldReturnValueOfProperty() { // Given final Element element = mock(Element.class); final String propName = "presentProperty"; final int propValue = 3; final ExtractProperty extractor = new ExtractProperty(propName); given(element.getProperty(propName)).willReturn(propValue); // When final Object result = extractor.apply(element); // Then assertEquals(propValue, result); }
@Test public void shouldRemoveProperty() { // Given final Element element1 = newElement("group"); element1.putProperty("property1", "propertyValue1"); element1.putProperty("property2", "propertyValue2"); // When element1.removeProperty("property1"); // Then assertEquals(1, element1.getProperties().size()); assertEquals(null, element1.getProperty("property1")); assertEquals("propertyValue2", element1.getProperty("property2")); }
@Test public void shouldCopyProperties() { // Given final Element element1 = newElement("group"); final Properties newProperties = new Properties("property1", "propertyValue1"); // When element1.copyProperties(newProperties); // Then assertEquals(1, element1.getProperties().size()); assertEquals("propertyValue1", element1.getProperty("property1")); }
@Test public void shouldTransformElementUsingMockFunction() { // Given final String selection = "reference1"; final String projection = "reference1"; final Integer valueResult = 3; final Function<String, Integer> function = mock(Function.class); given(function.apply("value1")).willReturn(valueResult); final ElementTransformer transformer = new ElementTransformer.Builder() .select(selection) .execute(function) .project(projection) .build(); final Edge edge = new Edge.Builder() .property(selection, "value1") .build(); // When final Element result = transformer.apply(edge); // Then assertEquals(valueResult, result.getProperty(projection)); }
@Test public void shouldTransformElementUsingInlineFunction() { // Given final Function<String, Integer> function = String::length; final ElementTransformer transformer = new ElementTransformer.Builder() .select("prop1") .execute(function) .project("prop3") .build(); final Entity element = new Entity.Builder() .group("test") .property("prop1", "value") .property("prop2", 1) .build(); // When final Element result = transformer.apply(element); // Then assertEquals("prop1".length(), result.getProperty("prop3")); }
@Test public void shouldAggregateElementUsingMockBinaryOperator() { // Given final String reference = "reference1"; final Integer valueResult = 3; final BinaryOperator<Integer> function = mock(BinaryOperator.class); given(function.apply(1, 2)).willReturn(valueResult); final ElementAggregator aggregator = new ElementAggregator.Builder() .select(reference) .execute(function) .build(); final Edge edge1 = new Edge.Builder() .property(reference, 1) .build(); final Edge edge2 = new Edge.Builder() .property(reference, 2) .build(); // When final Element result = aggregator.apply(edge1, edge2); // Then assertEquals(valueResult, result.getProperty(reference)); }
@Test public void shouldAggregateElementUsingLambdaBinaryOperator() { // Given final String reference = "reference1"; final BinaryOperator<String> function = (a, b) -> a + "," + b; final ElementAggregator aggregator = new ElementAggregator.Builder() .select(reference) .execute(function) .build(); final Edge edge1 = new Edge.Builder() .property(reference, "value1") .build(); final Edge edge2 = new Edge.Builder() .property(reference, "value2") .build(); // When final Element result = aggregator.apply(edge1, edge2); // Then assertEquals("value1,value2", result.getProperty(reference)); }
@Test public void shouldTransformElementUsingIdentityFunction() { // Given final ElementTransformer transformer = new ElementTransformer.Builder() .select("prop1") .execute(new Identity()) .project("prop3") .build(); final Entity element = new Entity.Builder() .group("test") .property("prop1", "value") .property("prop2", 1) .build(); // When final Element result = transformer.apply(element); // Then assertEquals(element.getProperty("prop1"), result.getProperty("prop3")); }
@Test public void shouldAggregateElementUsingKorypheBinaryOperator() { // Given final String reference = "reference1"; final BinaryOperator<Integer> function = new KorypheBinaryOperator<Integer>() { @Override public Integer _apply(final Integer a, final Integer b) { return a + b; } }; final ElementAggregator aggregator = new ElementAggregator.Builder() .select(reference) .execute(function) .build(); final Edge edge1 = new Edge.Builder() .property(reference, 1) .build(); final Edge edge2 = new Edge.Builder() .property(reference, 2) .build(); // When final Element result = aggregator.apply(edge1, edge2); // Then assertEquals(3, result.getProperty(reference)); }
@Override public Object apply(final Element element) { return null != element ? element.getProperty(name) : null; }
@Override public Person getObject(final Element element) { if (Group.PERSON.equals(element.getGroup()) && element instanceof Entity) { return new Person(((Entity) element).getVertex().toString(), element.getProperty(Property.NAME).toString(), (Integer) element.getProperty(Property.AGE)); } throw new UnsupportedOperationException("Cannot generate Person object from " + element); } }
@Override public Film getObject(final Element element) { if (Group.FILM.equals(element.getGroup()) && element instanceof Entity) { return new Film(((Entity) element).getVertex().toString(), element.getProperty(Property.NAME).toString(), (Certificate) element.getProperty(Property.CERTIFICATE)); } throw new UnsupportedOperationException("Cannot generate Film object from " + element); } }
private Boolean isVisible(final Element e) throws VisibilityParseException { if (e.getProperty(visibility) != null) { final VisibilityEvaluator visibilityEvaluator = new VisibilityEvaluator(auths); final ElementVisibility elementVisibility = new ElementVisibility((String) e.getProperty(visibility)); return visibilityEvaluator.evaluate(elementVisibility); } else { e.putProperty(visibility, new String()); return true; } } }
@SuppressFBWarnings(value = "BC_UNCONFIRMED_CAST", justification = "If an element is not an Entity it must be an Edge") @Override public Object getObject(final Element element) { if (element instanceof Entity) { return new DomainObject1((int) ((Entity) element).getVertex(), (int) element.getProperty("count")); } else { final Edge edge = (Edge) element; return new DomainObject2((int) edge.getSource(), (int) edge.getDestination(), (int) edge.getProperty("count")); } } }