TrackedNodeHandler.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. /**
  19.  * <p>
  20.  * A special {@code NodeHandler} implementation for tracked nodes.
  21.  * </p>
  22.  * <p>
  23.  * While basic access to a tracked node works in the same way as for usual immutable nodes, there are differences for
  24.  * other operations. For instance, the root node of the hierarchy is always the tracked node itself. Also the parent
  25.  * mapping requires some special attention: as long as the node is not detached, the parent mapping of the model to
  26.  * which the node belongs can be used.
  27.  * </p>
  28.  * <p>
  29.  * This class inherits the major part of the {@code NodeHandler} implementation from its base class. In order to
  30.  * implement the parent mapping, an underlying {@code NodeHandler} object has to be passed at construction time which
  31.  * contains this information; requests for a node's parent are delegated to this handler. Further, the root node has to
  32.  * be provided explicitly.
  33.  * </p>
  34.  *
  35.  * @since 2.0
  36.  */
  37. final class TrackedNodeHandler extends AbstractImmutableNodeHandler {
  38.     /** The root node. */
  39.     private final ImmutableNode rootNode;

  40.     /** The handler for querying the parent mapping. */
  41.     private final NodeHandler<ImmutableNode> parentHandler;

  42.     /**
  43.      * Creates a new instance of {@code TrackedNodeHandler} and initializes it with all required information.
  44.      *
  45.      * @param root the root node of the represented hierarchy
  46.      * @param handler an underlying handler for delegation
  47.      */
  48.     public TrackedNodeHandler(final ImmutableNode root, final NodeHandler<ImmutableNode> handler) {
  49.         rootNode = root;
  50.         parentHandler = handler;
  51.     }

  52.     /**
  53.      * {@inheritDoc} This implementation delegates to the handler with the parent mapping.
  54.      */
  55.     @Override
  56.     public ImmutableNode getParent(final ImmutableNode node) {
  57.         return getParentHandler().getParent(node);
  58.     }

  59.     /**
  60.      * Gets the parent handler. This is the {@code NodeHandler} which is consulted for determining a node's parent node.
  61.      *
  62.      * @return the parent {@code NodeHandler}
  63.      */
  64.     public NodeHandler<ImmutableNode> getParentHandler() {
  65.         return parentHandler;
  66.     }

  67.     /**
  68.      * {@inheritDoc} This implementation returns the root node passed at construction time.
  69.      */
  70.     @Override
  71.     public ImmutableNode getRootNode() {
  72.         return rootNode;
  73.     }
  74. }