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