View Javadoc

    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.configuration.tree;
    18
    19  import java.util.Collections;
    20  import java.util.HashSet;
    21  import java.util.Set;
    22
    23  /***
    24   * <p>
    25   * A base class for node combiner implementations.
    26   * </p>
    27   * <p>
    28   * A <em>node combiner</em> is an object that knows how two hierarchical node
    29   * structures can be combined into a single one. Of course, there are many
    30   * possible ways of implementing such a combination, e.g. constructing a union,
    31   * an intersection, or an "override" structure (were nodes in the first
    32   * hierarchy take precedence over nodes in the second hierarchy). This abstract
    33   * base class only provides some helper methods and defines the common interface
    34   * for node combiners. Concrete sub classes will implement the diverse
    35   * combination algorithms.
    36   * </p>
    37   * <p>
    38   * For some concrete combiner implementations it is important to distinguish
    39   * whether a node is a single node or whether it belongs to a list structure.
    40   * Alone from the input structures, the combiner will not always be able to make
    41   * this decision. So sometimes it may be necessary for the developer to
    42   * configure the combiner and tell it, which nodes should be treated as list
    43   * nodes. For this purpose the <code>addListNode()</code> method exists. It
    44   * can be passed the name of a node, which should be considered a list node.
    45   * </p>
    46   *
    47   * @author <a
    48   * href="http://commons.apache.org/configuration/team-list.html">Commons
    49   * Configuration team</a>
    50   * @version $Id: NodeCombiner.java 561230 2007-07-31 04:17:09Z rahul $
    51   * @since 1.3
    52   */
    53  public abstract class NodeCombiner
    54  {
    55      /*** Stores a list with node names that are known to be list nodes. */
    56      protected Set listNodes;
    57
    58      /***
    59       * Creates a new instance of <code>NodeCombiner</code>.
    60       */
    61      public NodeCombiner()
    62      {
    63          listNodes = new HashSet();
    64      }
    65
    66      /***
    67       * Adds the name of a node to the list of known list nodes. This means that
    68       * nodes with this name will never be combined.
    69       *
    70       * @param nodeName the name to be added
    71       */
    72      public void addListNode(String nodeName)
    73      {
    74          listNodes.add(nodeName);
    75      }
    76
    77      /***
    78       * Returns a set with the names of nodes that are known to be list nodes.
    79       *
    80       * @return a set with the names of list nodes
    81       */
    82      public Set getListNodes()
    83      {
    84          return Collections.unmodifiableSet(listNodes);
    85      }
    86
    87      /***
    88       * Checks if a node is a list node. This implementation tests if the given
    89       * node name is contained in the set of known list nodes. Derived classes
    90       * which use different criteria may overload this method.
    91       *
    92       * @param node the node to be tested
    93       * @return a flag whether this is a list node
    94       */
    95      public boolean isListNode(ConfigurationNode node)
    96      {
    97          return listNodes.contains(node.getName());
    98      }
    99
    100     /***
    101      * Combines the hierarchies represented by the given root nodes. This method
    102      * must be defined in concrete sub classes with the implementation of a
    103      * specific combination algorithm.
    104      *
    105      * @param node1 the first root node
    106      * @param node2 the second root node
    107      * @return the resulting combined node structure
    108      */
    109     public abstract ConfigurationNode combine(ConfigurationNode node1,
    110             ConfigurationNode node2);
    111
    112     /***
    113      * Creates a new view node. This method will be called whenever a new view
    114      * node is to be created. It can be overriden to create special view nodes.
    115      * This base implementation returns a new instance of
    116      * <code>{@link ViewNode}</code>.
    117      *
    118      * @return the new view node
    119      */
    120     protected ViewNode createViewNode()
    121     {
    122         return new ViewNode();
    123     }
    124 }