MergeCombiner.java

  1. /*
  2.  * Licensed to the Apache Software Foundation (ASF) under one or more
  3.  * contributor license agreements.  See the NOTICE file distributed with
  4.  * this work for additional information regarding copyright ownership.
  5.  * The ASF licenses this file to You under the Apache License, Version 2.0
  6.  * (the "License"); you may not use this file except in compliance with
  7.  * the License.  You may obtain a copy of the License at
  8.  *
  9.  *     http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */
  17. package org.apache.commons.configuration2.tree;

  18. import java.util.ArrayList;
  19. import java.util.HashMap;
  20. import java.util.LinkedList;
  21. import java.util.List;
  22. import java.util.Map;
  23. import java.util.Objects;

  24. /**
  25.  * <p>
  26.  * A specialized implementation of the {@code NodeCombiner} interface that performs a merge from two passed in node
  27.  * hierarchies.
  28.  * </p>
  29.  * <p>
  30.  * This combiner performs the merge using a few rules:
  31.  * </p>
  32.  * <ol>
  33.  * <li>Nodes can be merged when attributes that appear in both have the same value.</li>
  34.  * <li>Only a single node in the second file is considered a match to the node in the first file.</li>
  35.  * <li>Attributes in nodes that match are merged.
  36.  * <li>Nodes in both files that do not match are added to the result.</li>
  37.  * </ol>
  38.  *
  39.  * @since 1.7
  40.  */
  41. public class MergeCombiner extends NodeCombiner {
  42.     /**
  43.      * Checks whether the attributes of the passed in node are compatible.
  44.      *
  45.      * @param attrs1 the attributes of the first node
  46.      * @param node the 2nd node
  47.      * @return a flag whether these nodes can be combined regarding their attributes
  48.      */
  49.     private static boolean matchAttributes(final Map<String, Object> attrs1, final ImmutableNode node) {
  50.         final Map<String, Object> attrs2 = node.getAttributes();
  51.         for (final Map.Entry<String, Object> e : attrs1.entrySet()) {
  52.             if (attrs2.containsKey(e.getKey()) && !Objects.equals(e.getValue(), attrs2.get(e.getKey()))) {
  53.                 return false;
  54.             }
  55.         }
  56.         return true;
  57.     }

  58.     /**
  59.      * Handles the attributes during a combination process. First all attributes of the first node will be added to the
  60.      * result. Then all attributes of the second node, which are not contained in the first node, will also be added.
  61.      *
  62.      * @param result the builder for the resulting node
  63.      * @param node1 the first node
  64.      * @param node2 the second node
  65.      */
  66.     protected void addAttributes(final ImmutableNode.Builder result, final ImmutableNode node1, final ImmutableNode node2) {
  67.         final Map<String, Object> attributes = new HashMap<>(node1.getAttributes());
  68.         node2.getAttributes().forEach(attributes::putIfAbsent);
  69.         result.addAttributes(attributes);
  70.     }

  71.     /**
  72.      * Tests if the first node can be combined with the second node. A node can only be combined if its attributes are all
  73.      * present in the second node and they all have the same value.
  74.      *
  75.      * @param node2 the second node
  76.      * @param child the child node (of the first node)
  77.      * @param children2 the children of the 2nd node
  78.      * @return a child of the second node, with which a combination is possible
  79.      */
  80.     protected ImmutableNode canCombine(final ImmutableNode node2, final ImmutableNode child, final List<ImmutableNode> children2) {
  81.         final Map<String, Object> attrs1 = child.getAttributes();
  82.         final List<ImmutableNode> nodes = new ArrayList<>();

  83.         final List<ImmutableNode> children = HANDLER.getChildren(node2, child.getNodeName());
  84.         children.forEach(node -> {
  85.             if (matchAttributes(attrs1, node)) {
  86.                 nodes.add(node);
  87.             }
  88.         });

  89.         if (nodes.size() == 1) {
  90.             return nodes.get(0);
  91.         }
  92.         if (nodes.size() > 1 && !isListNode(child)) {
  93.             nodes.forEach(children2::remove);
  94.         }

  95.         return null;
  96.     }

  97.     /**
  98.      * Combines the given nodes to a new union node.
  99.      *
  100.      * @param node1 the first source node
  101.      * @param node2 the second source node
  102.      * @return the union node
  103.      */

  104.     @Override
  105.     public ImmutableNode combine(final ImmutableNode node1, final ImmutableNode node2) {
  106.         final ImmutableNode.Builder result = new ImmutableNode.Builder();
  107.         result.name(node1.getNodeName());
  108.         result.value(node1.getValue());
  109.         addAttributes(result, node1, node2);

  110.         // Check if nodes can be combined
  111.         final List<ImmutableNode> children2 = new LinkedList<>(node2.getChildren());
  112.         node1.forEach(child1 -> {
  113.             final ImmutableNode child2 = canCombine(node2, child1, children2);
  114.             if (child2 != null) {
  115.                 result.addChild(combine(child1, child2));
  116.                 children2.remove(child2);
  117.             } else {
  118.                 result.addChild(child1);
  119.             }
  120.         });

  121.         // Add remaining children of node 2
  122.         children2.forEach(result::addChild);
  123.         return result.create();
  124.     }
  125. }