@Test public void newProperty_IsMultiValuedArray_whenStartingFromAnArray() { assertThat((String[])GraphUtil.getNewPropertyValue(new String[]{"1", "2"}, "3"), is(new String[]{"1", "2", "3"})); }
static Object getNewPropertyValue(Object originalValue, Object newValue) { Class<?> clazz = checkNotNull(newValue).getClass(); boolean reduceToString = false; if (null != originalValue && originalValue.getClass().isArray()) { Class<?> originalClazz = Array.get(originalValue, 0).getClass(); if (!originalClazz.equals(clazz)) { reduceToString = true; clazz = String.class; } newValue = reduceToString ? newValue.toString() : newValue; Object newArray = Array.newInstance(clazz, Array.getLength(originalValue) + 1); for (int i = 0; i < Array.getLength(originalValue); i++) { Object val = Array.get(originalValue, i); if (newValue.equals(val)) { return originalValue; } Array.set(newArray, i, reduceToString ? val.toString() : val); } Array.set(newArray, Array.getLength(originalValue), newValue); return newArray; } else if (null != originalValue) { if (!clazz.equals(originalValue.getClass())) { reduceToString = true; clazz = String.class; } originalValue = reduceToString ? originalValue.toString() : originalValue; newValue = reduceToString ? newValue.toString() : newValue; if (!originalValue.equals(newValue)) { Object newArray = Array.newInstance(clazz, 2); Array.set(newArray, 0, originalValue); Array.set(newArray, 1, newValue); return newArray; } else { return originalValue; } } else { return newValue; } }
GraphUtil { static Object getNewPropertyValue(Object originalValue, Object newValue) { Class<?> clazz = checkNotNull(newValue).getClass(); boolean reduceToString = false; if (null != originalValue && originalValue.getClass().isArray()) { Class<?> originalClazz = Array.get(originalValue, 0).getClass(); if (!originalClazz.equals(clazz)) { reduceToString = true; clazz = String.class; } newValue = reduceToString ? newValue.toString() : newValue; Object newArray = Array.newInstance(clazz, Array.getLength(originalValue) + 1); for (int i = 0; i < Array.getLength(originalValue); i++) { Object val = Array.get(originalValue, i); if (newValue.equals(val)) { return originalValue; } Array.set(newArray, i, reduceToString ? val.toString() : val); } Array.set(newArray, Array.getLength(originalValue), newValue); return newArray; } else if (null != originalValue) { if (!clazz.equals(originalValue.getClass())) { reduceToString = true; clazz = String.class; } originalValue = reduceToString ? originalValue.toString() : originalValue; newValue = reduceToString ? newValue.toString() : newValue; if (!originalValue.equals(newValue)) { Object newArray = Array.newInstance(clazz, 2); Array.set(newArray, 0, originalValue); Array.set(newArray, 1, newValue); return newArray; } else { return originalValue; } } else { return newValue; } } }
GraphUtil { static Object getNewPropertyValue(Object originalValue, Object newValue) { Class<?> clazz = checkNotNull(newValue).getClass(); boolean reduceToString = false; if (null != originalValue && originalValue.getClass().isArray()) { Class<?> originalClazz = Array.get(originalValue, 0).getClass(); if (!originalClazz.equals(clazz)) { reduceToString = true; clazz = String.class; } newValue = reduceToString ? newValue.toString() : newValue; Object newArray = Array.newInstance(clazz, Array.getLength(originalValue) + 1); for (int i = 0; i < Array.getLength(originalValue); i++) { Object val = Array.get(originalValue, i); if (newValue.equals(val)) { return originalValue; } Array.set(newArray, i, reduceToString ? val.toString() : val); } Array.set(newArray, Array.getLength(originalValue), newValue); return newArray; } else if (null != originalValue) { if (!clazz.equals(originalValue.getClass())) { reduceToString = true; clazz = String.class; } originalValue = reduceToString ? originalValue.toString() : originalValue; newValue = reduceToString ? newValue.toString() : newValue; if (!originalValue.equals(newValue)) { Object newArray = Array.newInstance(clazz, 2); Array.set(newArray, 0, originalValue); Array.set(newArray, 1, newValue); return newArray; } else { return originalValue; } } else { return newValue; } }  }
GraphUtil { static Object getNewPropertyValue(Object originalValue, Object newValue) { Class<?> clazz = checkNotNull(newValue).getClass(); boolean reduceToString = false; if (null != originalValue && originalValue.getClass().isArray()) { Class<?> originalClazz = Array.get(originalValue, 0).getClass(); if (!originalClazz.equals(clazz)) { reduceToString = true; clazz = String.class; } newValue = reduceToString ? newValue.toString() : newValue; Object newArray = Array.newInstance(clazz, Array.getLength(originalValue) + 1); for (int i = 0; i < Array.getLength(originalValue); i++) { Object val = Array.get(originalValue, i); if (newValue.equals(val)) { return originalValue; } Array.set(newArray, i, reduceToString ? val.toString() : val); } Array.set(newArray, Array.getLength(originalValue), newValue); return newArray; } else if (null != originalValue) { if (!clazz.equals(originalValue.getClass())) { reduceToString = true; clazz = String.class; } originalValue = reduceToString ? originalValue.toString() : originalValue; newValue = reduceToString ? newValue.toString() : newValue; if (!originalValue.equals(newValue)) { Object newArray = Array.newInstance(clazz, 2); Array.set(newArray, 0, originalValue); Array.set(newArray, 1, newValue); return newArray; } else { return originalValue; } } else { return newValue; } }  static void addProperty(PropertyContainer container, String property, Object value); static Optional<T> getProperty(PropertyContainer container, String property,
      Class<T> type); static Collection<T> getProperties(PropertyContainer container, String property,
      Class<T> type); static Iterable<Relationship> getRelationships(final Node a, final Node b,
      RelationshipType type, final boolean directed); static Iterable<Relationship> getRelationships(final Node a, final Node b,
      RelationshipType type); static boolean ignoreProperty(Object value); }
GraphUtil { static Object getNewPropertyValue(Object originalValue, Object newValue) { Class<?> clazz = checkNotNull(newValue).getClass(); boolean reduceToString = false; if (null != originalValue && originalValue.getClass().isArray()) { Class<?> originalClazz = Array.get(originalValue, 0).getClass(); if (!originalClazz.equals(clazz)) { reduceToString = true; clazz = String.class; } newValue = reduceToString ? newValue.toString() : newValue; Object newArray = Array.newInstance(clazz, Array.getLength(originalValue) + 1); for (int i = 0; i < Array.getLength(originalValue); i++) { Object val = Array.get(originalValue, i); if (newValue.equals(val)) { return originalValue; } Array.set(newArray, i, reduceToString ? val.toString() : val); } Array.set(newArray, Array.getLength(originalValue), newValue); return newArray; } else if (null != originalValue) { if (!clazz.equals(originalValue.getClass())) { reduceToString = true; clazz = String.class; } originalValue = reduceToString ? originalValue.toString() : originalValue; newValue = reduceToString ? newValue.toString() : newValue; if (!originalValue.equals(newValue)) { Object newArray = Array.newInstance(clazz, 2); Array.set(newArray, 0, originalValue); Array.set(newArray, 1, newValue); return newArray; } else { return originalValue; } } else { return newValue; } }  static void addProperty(PropertyContainer container, String property, Object value); static Optional<T> getProperty(PropertyContainer container, String property,
      Class<T> type); static Collection<T> getProperties(PropertyContainer container, String property,
      Class<T> type); static Iterable<Relationship> getRelationships(final Node a, final Node b,
      RelationshipType type, final boolean directed); static Iterable<Relationship> getRelationships(final Node a, final Node b,
      RelationshipType type); static boolean ignoreProperty(Object value);  }