NodeCombiner.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.Collections;
  19. import java.util.HashSet;
  20. import java.util.Set;

  21. /**
  22.  * <p>
  23.  * A base class for node combiner implementations.
  24.  * </p>
  25.  * <p>
  26.  * A <em>node combiner</em> is an object that knows how two hierarchical node structures can be combined into a single
  27.  * one. Of course, there are many possible ways of implementing such a combination, for example constructing a union, an
  28.  * intersection, or an "override" structure (were nodes in the first hierarchy take precedence over nodes in the second
  29.  * hierarchy). This abstract base class only provides some helper methods and defines the common interface for node
  30.  * combiners. Concrete sub classes will implement the diverse combination algorithms.
  31.  * </p>
  32.  * <p>
  33.  * For some concrete combiner implementations it is important to distinguish whether a node is a single node or whether
  34.  * it belongs to a list structure. Alone from the input structures, the combiner will not always be able to make this
  35.  * decision. So sometimes it may be necessary for the developer to configure the combiner and tell it, which nodes
  36.  * should be treated as list nodes. For this purpose the {@code addListNode()} method exists. It can be passed the name
  37.  * of a node, which should be considered a list node.
  38.  * </p>
  39.  *
  40.  * @since 1.3
  41.  */
  42. public abstract class NodeCombiner {
  43.     /**
  44.      * A default handler object for immutable nodes. This object can be used by derived classes for dealing with nodes.
  45.      * However, it provides only limited functionality; it supports only operations on child nodes, but no references to
  46.      * parent nodes.
  47.      */
  48.     protected static final NodeHandler<ImmutableNode> HANDLER = createNodeHandler();

  49.     /**
  50.      * Creates a node handler object for immutable nodes which can be used by sub classes to perform advanced operations on
  51.      * nodes.
  52.      *
  53.      * @return the node handler implementation
  54.      */
  55.     private static NodeHandler<ImmutableNode> createNodeHandler() {
  56.         return new AbstractImmutableNodeHandler() {
  57.             @Override
  58.             public ImmutableNode getParent(final ImmutableNode node) {
  59.                 return null;
  60.             }

  61.             @Override
  62.             public ImmutableNode getRootNode() {
  63.                 return null;
  64.             }
  65.         };
  66.     }

  67.     /** Stores a list with node names that are known to be list nodes. */
  68.     private final Set<String> listNodes;

  69.     /**
  70.      * Creates a new instance of {@code NodeCombiner}.
  71.      */
  72.     public NodeCombiner() {
  73.         listNodes = new HashSet<>();
  74.     }

  75.     /**
  76.      * Adds the name of a node to the list of known list nodes. This means that nodes with this name will never be combined.
  77.      *
  78.      * @param nodeName the name to be added
  79.      */
  80.     public void addListNode(final String nodeName) {
  81.         listNodes.add(nodeName);
  82.     }

  83.     /**
  84.      * Combines the hierarchies represented by the given root nodes. This method must be defined in concrete sub classes
  85.      * with the implementation of a specific combination algorithm.
  86.      *
  87.      * @param node1 the first root node
  88.      * @param node2 the second root node
  89.      * @return the root node of the resulting combined node structure
  90.      */
  91.     public abstract ImmutableNode combine(ImmutableNode node1, ImmutableNode node2);

  92.     /**
  93.      * Gets a set with the names of nodes that are known to be list nodes.
  94.      *
  95.      * @return a set with the names of list nodes
  96.      */
  97.     public Set<String> getListNodes() {
  98.         return Collections.unmodifiableSet(listNodes);
  99.     }

  100.     /**
  101.      * Checks if a node is a list node. This implementation tests if the given node name is contained in the set of known
  102.      * list nodes. Derived classes which use different criteria may overload this method.
  103.      *
  104.      * @param node the node to be tested
  105.      * @return a flag whether this is a list node
  106.      */
  107.     public boolean isListNode(final ImmutableNode node) {
  108.         return listNodes.contains(node.getNodeName());
  109.     }
  110. }