View Javadoc
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    *     https://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  import java.util.List;
20  import java.util.Set;
21  
22  /**
23   * <p>
24   * An abstract base class for decorators of a {@code NodeHandler}.
25   * </p>
26   * <p>
27   * This class implements all methods of the {@code NodeHandler} interface by delegating to another instance. This is
28   * convenient if specific functionality of a {@code NodeHandler} is to be adapted for a special use case. Concrete sub
29   * classes have to implement the {@code getDecoratedNodeHandler()} method to provide the underlying handler.
30   * </p>
31   *
32   * @param <T> the type of the nodes supported by this handler
33   * @since 2.0
34   */
35  public abstract class NodeHandlerDecorator<T> implements NodeHandler<T> {
36  
37      /**
38       * Constructs a new instance.
39       */
40      public NodeHandlerDecorator() {
41          // empty
42      }
43  
44      @Override
45      public Set<String> getAttributes(final T node) {
46          return getDecoratedNodeHandler().getAttributes(node);
47      }
48  
49      @Override
50      public Object getAttributeValue(final T node, final String name) {
51          return getDecoratedNodeHandler().getAttributeValue(node, name);
52      }
53  
54      @Override
55      public T getChild(final T node, final int index) {
56          return getDecoratedNodeHandler().getChild(node, index);
57      }
58  
59      @Override
60      public List<T> getChildren(final T node) {
61          return getDecoratedNodeHandler().getChildren(node);
62      }
63  
64      @Override
65      public List<T> getChildren(final T node, final String name) {
66          return getDecoratedNodeHandler().getChildren(node, name);
67      }
68  
69      @Override
70      public int getChildrenCount(final T node, final String name) {
71          return getDecoratedNodeHandler().getChildrenCount(node, name);
72      }
73  
74      /**
75       * Gets the {@code NodeHandler} object that is decorated by this instance. All method calls are delegated to this
76       * object.
77       *
78       * @return the decorated {@code NodeHandler}
79       */
80      protected abstract NodeHandler<T> getDecoratedNodeHandler();
81  
82      @Override
83      public <C> List<T> getMatchingChildren(final T node, final NodeMatcher<C> matcher, final C criterion) {
84          return getDecoratedNodeHandler().getMatchingChildren(node, matcher, criterion);
85      }
86  
87      @Override
88      public <C> int getMatchingChildrenCount(final T node, final NodeMatcher<C> matcher, final C criterion) {
89          return getDecoratedNodeHandler().getMatchingChildrenCount(node, matcher, criterion);
90      }
91  
92      @Override
93      public T getParent(final T node) {
94          return getDecoratedNodeHandler().getParent(node);
95      }
96  
97      @Override
98      public T getRootNode() {
99          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 }