Source:
protected final <T> Optional<Resource> merge(final Resource resource, final String destination, final Collection<String> sources, final Class<T> typeHint, final boolean allowDuplicates) throws PersistenceException { @SuppressWarnings("unchecked") final T[] emptyArray = (T[]) Array.newInstance(typeHint, 0); final String targetProperty; Collection<T> collectedValues = null; if (allowDuplicates) { collectedValues = new ArrayList<>(); } else { collectedValues = new LinkedHashSet<>(); } for (final String source : sources) { Resource sourceProperties = resource; String sourceParam = source; if (source.contains("/")) { sourceProperties = resource.getResourceResolver().getResource(resource, source.substring(0, source.lastIndexOf('/'))); sourceParam = source.substring(source.lastIndexOf('/') + 1); } T[] tmp = sourceProperties.adaptTo(ModifiableValueMap.class).get(sourceParam, emptyArray); collectedValues.addAll(Arrays.asList(tmp)); } Resource targetResource; if (destination.contains("/")) { targetProperty = destination.substring(destination.lastIndexOf('/') + 1); targetResource = resource.getResourceResolver().getResource(resource, destination.substring(0, destination.lastIndexOf('/'))); } else { targetProperty = destination; targetResource = resource; } ModifiableValueMap targetProperties = targetResource.adaptTo(ModifiableValueMap.class); final T[] currentValues = targetProperties.get(targetProperty, emptyArray); if (!collectedValues.equals(Arrays.asList(currentValues))) { targetProperties.put(targetProperty, collectedValues.toArray(emptyArray)); return Optional.of(targetResource); } else { return Optional.empty(); } }

Target:
protected final <T> Optional<Resource> merge(final Resource resource, final String destination, final Collection<String> sources, final Class<T> typeHint, final boolean allowDuplicates) throws PersistenceException { ResourceResolver rr = resource.getResourceResolver(); @SuppressWarnings("unchecked") final T[] emptyArray = (T[]) Array.newInstance(typeHint, 0); Collection<T> collectedValues; if (allowDuplicates) { collectedValues = new ArrayList<>(); } else { collectedValues = new LinkedHashSet<>(); } for (final String source : sources) { Resource sourceProperties = resource; String sourceParam = source; if (source.contains("/")) { sourceParam = StringUtils.substringAfterLast(source, "/"); sourceProperties = rr.getResource(resource, StringUtils.substringBeforeLast(source, "/")); } T[] tmp = sourceProperties.adaptTo(ModifiableValueMap.class).get(sourceParam, emptyArray); collectedValues.addAll(Arrays.asList(tmp)); } Resource targetResource = resource; String targetProperty = destination; if (destination.contains("/")) { targetProperty = StringUtils.substringAfterLast(destination, "/"); targetResource = rr.getResource(resource, StringUtils.substringBeforeLast(destination, "/")); } ModifiableValueMap targetProperties = targetResource.adaptTo(ModifiableValueMap.class); final T[] currentValues = targetProperties.get(targetProperty, emptyArray); if (!collectedValues.equals(Arrays.asList(currentValues))) { targetProperties.put(targetProperty, collectedValues.toArray(emptyArray)); return Optional.of(targetResource); } else { return Optional.empty(); } }