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.Map;
021
022/**
023 * <p>
024 * Definition of an interface which allows resolving a (property) key for different manipulating operations.
025 * </p>
026 * <p>
027 * This interface is used when interacting with a node model. It is an abstraction over a concrete
028 * {@link ExpressionEngine} instance. It also implements some functionality for creating special helper objects for the
029 * processing of complex update operations.
030 * </p>
031 *
032 * @param <T> the type of the nodes supported by this resolver
033 * @since 2.0
034 */
035public interface NodeKeyResolver<T> {
036
037    /**
038     * Generates a unique key for the specified node. This method is used if keys have to be generated for nodes received as
039     * query results. An implementation must generate a canonical key which is compatible with the current expression
040     * engine. The passed in map can be used by an implementation as cache. It is created initially by the caller and then
041     * passed in subsequent calls. An implementation may use this to avoid that keys for nodes already encountered have to
042     * be generated again.
043     *
044     * @param node the node in question
045     * @param cache a map serving as cache
046     * @param handler the {@code NodeHandler}
047     * @return a key for the specified node
048     */
049    String nodeKey(T node, Map<T, String> cache, NodeHandler<T> handler);
050
051    /**
052     * Resolves a key of an add operation. Result is a {@code NodeAddData} object containing all information for actually
053     * performing the add operation at the specified key.
054     *
055     * @param root the root node
056     * @param key the key to be resolved
057     * @param handler the {@code NodeHandler}
058     * @return a {@code NodeAddData} object to be used for the add operation
059     */
060    NodeAddData<T> resolveAddKey(T root, String key, NodeHandler<T> handler);
061
062    /**
063     * Performs a query for the specified key on the given root node. This is a thin wrapper over the {@code query()} method
064     * of an {@link ExpressionEngine}.
065     *
066     * @param root the root node
067     * @param key the key to be resolved
068     * @param handler the {@code NodeHandler}
069     * @return a list with query results
070     */
071    List<QueryResult<T>> resolveKey(T root, String key, NodeHandler<T> handler);
072
073    /**
074     * Performs a query for the specified key on the given root node returning only node results. Some operations require
075     * results of type node and do not support attributes (for example for tracking nodes). This operation can be used in such
076     * cases. It works like {@code resolveKey()}, but filters only for results of type node.
077     *
078     * @param root the root node
079     * @param key the key to be resolved
080     * @param handler the {@code NodeHandler}
081     * @return a list with the resolved nodes
082     */
083    List<T> resolveNodeKey(T root, String key, NodeHandler<T> handler);
084
085    /**
086     * Resolves a key for an update operation. Result is a {@code NodeUpdateData} object containing all information for
087     * actually performing the update operation at the specified key using the provided new value object.
088     *
089     * @param root the root node
090     * @param key the key to be resolved
091     * @param newValue the new value for the key to be updated; this can be a single value or a container for multiple
092     *        values
093     * @param handler the {@code NodeHandler}
094     * @return a {@code NodeUpdateData} object to be used for this update operation
095     */
096    NodeUpdateData<T> resolveUpdateKey(T root, String key, Object newValue, NodeHandler<T> handler);
097}