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.ArrayList;
20  import java.util.Collections;
21  import java.util.Iterator;
22  import java.util.LinkedList;
23  import java.util.List;
24  
25  import org.apache.commons.lang3.builder.ToStringBuilder;
26  
27  /**
28   * <p>
29   * A class for selecting a specific node based on a key or a set of keys.
30   * </p>
31   * <p>
32   * An instance of this class is initialized with the key of a node. It is also possible to concatenate multiple keys -
33   * for example if a sub key is to be constructed from another sub key. {@code NodeSelector} provides the {@code select()}
34   * method which evaluates the wrapped keys on a specified root node and returns the resulting unique target node. The
35   * class expects that the key(s) stored in an instance select exactly one target node. If this is not the case, result
36   * is <strong>null</strong> indicating that the selection criteria are not sufficient.
37   * </p>
38   * <p>
39   * Implementation node: Instances of this class are immutable. They can be shared between arbitrary components.
40   * </p>
41   *
42   * @since 2.0
43   */
44  public class NodeSelector {
45  
46      /** Stores the wrapped keys. */
47      private final List<String> nodeKeys;
48  
49      /**
50       * Creates a new instance of {@code NodeSelector} and initializes it with the list of keys to be used as selection
51       * criteria.
52       *
53       * @param keys the keys for selecting nodes
54       */
55      private NodeSelector(final List<String> keys) {
56          nodeKeys = keys;
57      }
58  
59      /**
60       * Creates a new instance of {@code NodeSelector} and initializes it with the key to the target node.
61       *
62       * @param key the key
63       */
64      public NodeSelector(final String key) {
65          this(Collections.singletonList(key));
66      }
67  
68      /**
69       * Compares this object with another one. Two instances of {@code NodeSelector} are considered equal if they have the
70       * same keys as selection criteria.
71       *
72       * @param obj the object to be compared
73       * @return a flag whether these objects are equal
74       */
75      @Override
76      public boolean equals(final Object obj) {
77          if (this == obj) {
78              return true;
79          }
80          if (!(obj instanceof NodeSelector)) {
81              return false;
82          }
83  
84          final NodeSelector c = (NodeSelector) obj;
85          return nodeKeys.equals(c.nodeKeys);
86      }
87  
88      /**
89       * Executes a query for a given key and filters the results for nodes only.
90       *
91       * @param root the root node for the query
92       * @param resolver the {@code NodeKeyResolver}
93       * @param handler the {@code NodeHandler}
94       * @param key the key
95       * @param nodes here the results are stored
96       */
97      private void getFilteredResults(final ImmutableNode root, final NodeKeyResolver<ImmutableNode> resolver, final NodeHandler<ImmutableNode> handler,
98          final String key, final List<ImmutableNode> nodes) {
99          final List<QueryResult<ImmutableNode>> results = resolver.resolveKey(root, key, handler);
100         results.forEach(result -> {
101             if (!result.isAttributeResult()) {
102                 nodes.add(result.getNode());
103             }
104         });
105     }
106 
107     /**
108      * Returns a hash code for this object.
109      *
110      * @return a hash code
111      */
112     @Override
113     public int hashCode() {
114         return nodeKeys.hashCode();
115     }
116 
117     /**
118      * Applies this {@code NodeSelector} on the specified root node. This method applies the selection criteria stored in
119      * this object and tries to determine a single target node. If this is successful, the target node is returned.
120      * Otherwise, result is <strong>null</strong>.
121      *
122      * @param root the root node on which to apply this selector
123      * @param resolver the {@code NodeKeyResolver}
124      * @param handler the {@code NodeHandler}
125      * @return the selected target node or <strong>null</strong>
126      */
127     public ImmutableNode select(final ImmutableNode root, final NodeKeyResolver<ImmutableNode> resolver, final NodeHandler<ImmutableNode> handler) {
128         List<ImmutableNode> nodes = new LinkedList<>();
129         final Iterator<String> itKeys = nodeKeys.iterator();
130         getFilteredResults(root, resolver, handler, itKeys.next(), nodes);
131 
132         while (itKeys.hasNext()) {
133             final String currentKey = itKeys.next();
134             final List<ImmutableNode> currentResults = new LinkedList<>();
135             nodes.forEach(currentRoot -> getFilteredResults(currentRoot, resolver, handler, currentKey, currentResults));
136             nodes = currentResults;
137         }
138 
139         return nodes.size() == 1 ? nodes.get(0) : null;
140     }
141 
142     /**
143      * Creates a sub {@code NodeSelector} object which uses the key(s) of this selector plus the specified key as selection
144      * criteria. This is useful when another selection is to be performed on the results of a first selector.
145      *
146      * @param subKey the additional key for the sub selector
147      * @return the sub {@code NodeSelector} instance
148      */
149     public NodeSelector subSelector(final String subKey) {
150         final List<String> keys = new ArrayList<>(nodeKeys.size() + 1);
151         keys.addAll(nodeKeys);
152         keys.add(subKey);
153         return new NodeSelector(keys);
154     }
155 
156     /**
157      * Returns a string representation for this object. This string contains the keys to be used as selection criteria.
158      *
159      * @return a string for this object
160      */
161     @Override
162     public String toString() {
163         return new ToStringBuilder(this).append("keys", nodeKeys).toString();
164     }
165 }