NodeHandlerDecorator.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.List;
  19. import java.util.Set;

  20. /**
  21.  * <p>
  22.  * An abstract base class for decorators of a {@code NodeHandler}.
  23.  * </p>
  24.  * <p>
  25.  * This class implements all methods of the {@code NodeHandler} interface by delegating to another instance. This is
  26.  * convenient if specific functionality of a {@code NodeHandler} is to be adapted for a special use case. Concrete sub
  27.  * classes have to implement the {@code getDecoratedNodeHandler()} method to provide the underlying handler.
  28.  * </p>
  29.  *
  30.  * @param <T> the type of the nodes supported by this handler
  31.  * @since 2.0
  32.  */
  33. public abstract class NodeHandlerDecorator<T> implements NodeHandler<T> {
  34.     @Override
  35.     public Set<String> getAttributes(final T node) {
  36.         return getDecoratedNodeHandler().getAttributes(node);
  37.     }

  38.     @Override
  39.     public Object getAttributeValue(final T node, final String name) {
  40.         return getDecoratedNodeHandler().getAttributeValue(node, name);
  41.     }

  42.     @Override
  43.     public T getChild(final T node, final int index) {
  44.         return getDecoratedNodeHandler().getChild(node, index);
  45.     }

  46.     @Override
  47.     public List<T> getChildren(final T node) {
  48.         return getDecoratedNodeHandler().getChildren(node);
  49.     }

  50.     @Override
  51.     public List<T> getChildren(final T node, final String name) {
  52.         return getDecoratedNodeHandler().getChildren(node, name);
  53.     }

  54.     @Override
  55.     public int getChildrenCount(final T node, final String name) {
  56.         return getDecoratedNodeHandler().getChildrenCount(node, name);
  57.     }

  58.     /**
  59.      * Gets the {@code NodeHandler} object that is decorated by this instance. All method calls are delegated to this
  60.      * object.
  61.      *
  62.      * @return the decorated {@code NodeHandler}
  63.      */
  64.     protected abstract NodeHandler<T> getDecoratedNodeHandler();

  65.     @Override
  66.     public <C> List<T> getMatchingChildren(final T node, final NodeMatcher<C> matcher, final C criterion) {
  67.         return getDecoratedNodeHandler().getMatchingChildren(node, matcher, criterion);
  68.     }

  69.     @Override
  70.     public <C> int getMatchingChildrenCount(final T node, final NodeMatcher<C> matcher, final C criterion) {
  71.         return getDecoratedNodeHandler().getMatchingChildrenCount(node, matcher, criterion);
  72.     }

  73.     @Override
  74.     public T getParent(final T node) {
  75.         return getDecoratedNodeHandler().getParent(node);
  76.     }

  77.     @Override
  78.     public T getRootNode() {
  79.         return getDecoratedNodeHandler().getRootNode();
  80.     }

  81.     @Override
  82.     public Object getValue(final T node) {
  83.         return getDecoratedNodeHandler().getValue(node);
  84.     }

  85.     @Override
  86.     public boolean hasAttributes(final T node) {
  87.         return getDecoratedNodeHandler().hasAttributes(node);
  88.     }

  89.     @Override
  90.     public int indexOfChild(final T parent, final T child) {
  91.         return getDecoratedNodeHandler().indexOfChild(parent, child);
  92.     }

  93.     @Override
  94.     public boolean isDefined(final T node) {
  95.         return getDecoratedNodeHandler().isDefined(node);
  96.     }

  97.     @Override
  98.     public String nodeName(final T node) {
  99.         return getDecoratedNodeHandler().nodeName(node);
  100.     }
  101. }