    001/*
    002 * Licensed to the Apache Software Foundation (ASF) under one or more
    003 * contributor license agreements.  See the NOTICE file distributed with
    004 * this work for additional information regarding copyright ownership.
    005 * The ASF licenses this file to You under the Apache License, Version 2.0
    006 * (the "License"); you may not use this file except in compliance with
    007 * the License.  You may obtain a copy of the License at
    008 *
    009 *     https://www.apache.org/licenses/LICENSE-2.0
    010 *
    011 * Unless required by applicable law or agreed to in writing, software
    012 * distributed under the License is distributed on an "AS IS" BASIS,
    013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    014 * See the License for the specific language governing permissions and
    015 * limitations under the License.
    016 */
    017package org.apache.commons.configuration2.tree;
    018
    019import java.util.List;
    020import java.util.Set;
    021
    022/**
    023 * <p>
    024 * An abstract base class for decorators of a {@code NodeHandler}.
    025 * </p>
    026 * <p>
    027 * This class implements all methods of the {@code NodeHandler} interface by delegating to another instance. This is
    028 * convenient if specific functionality of a {@code NodeHandler} is to be adapted for a special use case. Concrete sub
    029 * classes have to implement the {@code getDecoratedNodeHandler()} method to provide the underlying handler.
    030 * </p>
    031 *
    032 * @param <T> the type of the nodes supported by this handler
    033 * @since 2.0
    034 */
    035public abstract class NodeHandlerDecorator<T> implements NodeHandler<T> {
    036
    037    /**
    038     * Constructs a new instance.
    039     */
    040    public NodeHandlerDecorator() {
    041        // empty
    042    }
    043
    044    @Override
    045    public Set<String> getAttributes(final T node) {
    046        return getDecoratedNodeHandler().getAttributes(node);
    047    }
    048
    049    @Override
    050    public Object getAttributeValue(final T node, final String name) {
    051        return getDecoratedNodeHandler().getAttributeValue(node, name);
    052    }
    053
    054    @Override
    055    public T getChild(final T node, final int index) {
    056        return getDecoratedNodeHandler().getChild(node, index);
    057    }
    058
    059    @Override
    060    public List<T> getChildren(final T node) {
    061        return getDecoratedNodeHandler().getChildren(node);
    062    }
    063
    064    @Override
    065    public List<T> getChildren(final T node, final String name) {
    066        return getDecoratedNodeHandler().getChildren(node, name);
    067    }
    068
    069    @Override
    070    public int getChildrenCount(final T node, final String name) {
    071        return getDecoratedNodeHandler().getChildrenCount(node, name);
    072    }
    073
    074    /**
    075     * Gets the {@code NodeHandler} object that is decorated by this instance. All method calls are delegated to this
    076     * object.
    077     *
    078     * @return the decorated {@code NodeHandler}
    079     */
    080    protected abstract NodeHandler<T> getDecoratedNodeHandler();
    081
    082    @Override
    083    public <C> List<T> getMatchingChildren(final T node, final NodeMatcher<C> matcher, final C criterion) {
    084        return getDecoratedNodeHandler().getMatchingChildren(node, matcher, criterion);
    085    }
    086
    087    @Override
    088    public <C> int getMatchingChildrenCount(final T node, final NodeMatcher<C> matcher, final C criterion) {
    089        return getDecoratedNodeHandler().getMatchingChildrenCount(node, matcher, criterion);
    090    }
    091
    092    @Override
    093    public T getParent(final T node) {
    094        return getDecoratedNodeHandler().getParent(node);
    095    }
    096
    097    @Override
    098    public T getRootNode() {
    099        return getDecoratedNodeHandler().getRootNode();
    100    }
    101
    102    @Override
    103    public Object getValue(final T node) {
    104        return getDecoratedNodeHandler().getValue(node);
    105    }
    106
    107    @Override
    108    public boolean hasAttributes(final T node) {
    109        return getDecoratedNodeHandler().hasAttributes(node);
    110    }
    111
    112    @Override
    113    public int indexOfChild(final T parent, final T child) {
    114        return getDecoratedNodeHandler().indexOfChild(parent, child);
    115    }
    116
    117    @Override
    118    public boolean isDefined(final T node) {
    119        return getDecoratedNodeHandler().isDefined(node);
    120    }
    121
    122    @Override
    123    public String nodeName(final T node) {
    124        return getDecoratedNodeHandler().nodeName(node);
    125    }
    126}