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 *     http://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    @Override
037    public Set<String> getAttributes(final T node) {
038        return getDecoratedNodeHandler().getAttributes(node);
039    }
040
041    @Override
042    public Object getAttributeValue(final T node, final String name) {
043        return getDecoratedNodeHandler().getAttributeValue(node, name);
044    }
045
046    @Override
047    public T getChild(final T node, final int index) {
048        return getDecoratedNodeHandler().getChild(node, index);
049    }
050
051    @Override
052    public List<T> getChildren(final T node) {
053        return getDecoratedNodeHandler().getChildren(node);
054    }
055
056    @Override
057    public List<T> getChildren(final T node, final String name) {
058        return getDecoratedNodeHandler().getChildren(node, name);
059    }
060
061    @Override
062    public int getChildrenCount(final T node, final String name) {
063        return getDecoratedNodeHandler().getChildrenCount(node, name);
064    }
065
066    /**
067     * Gets the {@code NodeHandler} object that is decorated by this instance. All method calls are delegated to this
068     * object.
069     *
070     * @return the decorated {@code NodeHandler}
071     */
072    protected abstract NodeHandler<T> getDecoratedNodeHandler();
073
074    @Override
075    public <C> List<T> getMatchingChildren(final T node, final NodeMatcher<C> matcher, final C criterion) {
076        return getDecoratedNodeHandler().getMatchingChildren(node, matcher, criterion);
077    }
078
079    @Override
080    public <C> int getMatchingChildrenCount(final T node, final NodeMatcher<C> matcher, final C criterion) {
081        return getDecoratedNodeHandler().getMatchingChildrenCount(node, matcher, criterion);
082    }
083
084    @Override
085    public T getParent(final T node) {
086        return getDecoratedNodeHandler().getParent(node);
087    }
088
089    @Override
090    public T getRootNode() {
091        return getDecoratedNodeHandler().getRootNode();
092    }
093
094    @Override
095    public Object getValue(final T node) {
096        return getDecoratedNodeHandler().getValue(node);
097    }
098
099    @Override
100    public boolean hasAttributes(final T node) {
101        return getDecoratedNodeHandler().hasAttributes(node);
102    }
103
104    @Override
105    public int indexOfChild(final T parent, final T child) {
106        return getDecoratedNodeHandler().indexOfChild(parent, child);
107    }
108
109    @Override
110    public boolean isDefined(final T node) {
111        return getDecoratedNodeHandler().isDefined(node);
112    }
113
114    @Override
115    public String nodeName(final T node) {
116        return getDecoratedNodeHandler().nodeName(node);
117    }
118}