NodeAddData.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.Collection;
  20. import java.util.Collections;
  21. import java.util.List;

  22. /**
  23.  * <p>
  24.  * A simple data class used by {@link ExpressionEngine} to store the results of the {@code prepareAdd()} operation.
  25.  * </p>
  26.  * <p>
  27.  * If a new property is to be added to a configuration, the affected {@code Configuration} object must know, where in
  28.  * its hierarchy of configuration nodes new elements have to be added. This information is obtained by an
  29.  * {@code ExpressionEngine} object that interprets the key of the new property. This expression engine will pack all
  30.  * information necessary for the configuration to perform the add operation in an instance of this class.
  31.  * </p>
  32.  * <p>
  33.  * Information managed by this class contains:
  34.  * </p>
  35.  * <ul>
  36.  * <li>the configuration node, to which new elements must be added</li>
  37.  * <li>the name of the new node</li>
  38.  * <li>whether the new node is a child node or an attribute node</li>
  39.  * <li>if a whole branch is to be added at once, the names of all nodes between the parent node (the target of the add
  40.  * operation) and the new node</li>
  41.  * </ul>
  42.  *
  43.  * @param <T> the type of nodes this class can handle
  44.  * @since 1.3
  45.  */
  46. public class NodeAddData<T> {
  47.     /**
  48.      * Creates the list with path nodes. Handles null input.
  49.      *
  50.      * @param intermediateNodes the nodes passed to the constructor
  51.      * @return an unmodifiable list of path nodes
  52.      */
  53.     private static List<String> createPathNodes(final Collection<String> intermediateNodes) {
  54.         if (intermediateNodes == null) {
  55.             return Collections.emptyList();
  56.         }
  57.         return Collections.unmodifiableList(new ArrayList<>(intermediateNodes));
  58.     }

  59.     /** Stores the parent node of the add operation. */
  60.     private final T parent;

  61.     /**
  62.      * Stores a list with the names of nodes that are on the path between the parent node and the new node.
  63.      */
  64.     private final List<String> pathNodes;

  65.     /** Stores the name of the new node. */
  66.     private final String newNodeName;

  67.     /** Stores the attribute flag. */
  68.     private final boolean attribute;

  69.     /**
  70.      * Creates a new instance of {@code NodeAddData} and initializes it.
  71.      *
  72.      * @param parentNode the parent node of the add operation
  73.      * @param newName the name of the new node
  74.      * @param isAttr flag whether the new node is an attribute
  75.      * @param intermediateNodes an optional collection with path nodes
  76.      */
  77.     public NodeAddData(final T parentNode, final String newName, final boolean isAttr, final Collection<String> intermediateNodes) {
  78.         parent = parentNode;
  79.         newNodeName = newName;
  80.         attribute = isAttr;
  81.         pathNodes = createPathNodes(intermediateNodes);
  82.     }

  83.     /**
  84.      * Gets the name of the new node.
  85.      *
  86.      * @return the new node's name
  87.      */
  88.     public String getNewNodeName() {
  89.         return newNodeName;
  90.     }

  91.     /**
  92.      * Gets the parent node.
  93.      *
  94.      * @return the parent node
  95.      */
  96.     public T getParent() {
  97.         return parent;
  98.     }

  99.     /**
  100.      * Gets a list with further nodes that must be added. This is needed if a complete branch is to be added at once. For
  101.      * instance, imagine that there exists only a node {@code database}. Now the key
  102.      * {@code database.connection.settings.username} (assuming the syntax of the default expression engine) is to be added.
  103.      * Then {@code username} is the name of the new node, but the nodes {@code connection} and {@code settings} must be
  104.      * added to the parent node first. In this example these names would be returned by this method.
  105.      *
  106.      * @return a list with the names of nodes that must be added as parents of the new node (never <strong>null</strong>)
  107.      */
  108.     public List<String> getPathNodes() {
  109.         return pathNodes;
  110.     }

  111.     /**
  112.      * Returns a flag if the new node to be added is an attribute.
  113.      *
  114.      * @return <strong>true</strong> for an attribute node, <strong>false</strong> for a child node
  115.      */
  116.     public boolean isAttribute() {
  117.         return attribute;
  118.     }
  119. }