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.convert;
018
019import java.util.Collection;
020import java.util.Collections;
021import java.util.IdentityHashMap;
022import java.util.List;
023
024/**
025 * <p>
026 * Definition of an interface that controls the handling of list delimiters in configuration properties.
027 * </p>
028 * <p>
029 * {@link org.apache.commons.configuration2.AbstractConfiguration AbstractConfiguration} supports list delimiters in
030 * property values. If such a delimiter is found, the value actually contains multiple values and has to be split. This
031 * is useful for instance for {@link org.apache.commons.configuration2.PropertiesConfiguration PropertiesConfiguration}:
032 * properties files that have to be compatible with the {@link java.util.Properties} class cannot have multiple
033 * occurrences of a single property key, therefore a different storage scheme for multi-valued properties is needed. A
034 * possible storage scheme could look as follows:
035 * </p>
036 *
037 * <pre>
038 * myProperty=value1,value2,value3
039 * </pre>
040 *
041 * <p>
042 * Here a comma is used as list delimiter. When parsing this property (and using a corresponding
043 * {@code ListDelimiterHandler} implementation) the string value is split, and three values are added for the property
044 * key.
045 * </p>
046 * <p>
047 * A {@code ListDelimiterHandler} knows how to parse and to escape property values. It is called by concrete
048 * {@code Configuration} implementations when they have to deal with properties with multiple values.
049 * </p>
050 *
051 * @since 2.0
052 */
053public interface ListDelimiterHandler {
054    /**
055     * A specialized {@code ValueTransformer} implementation which does no transformation. The {@code transformValue()}
056     * method just returns the passed in object without changes. This instance can be used by configurations which do not
057     * require additional encoding.
058     */
059    ValueTransformer NOOP_TRANSFORMER = value -> value;
060
061    /**
062     * Escapes the specified single value object. This method is called for properties containing only a single value. So
063     * this method can rely on the fact that the passed in object is not a list. An implementation has to check whether the
064     * value contains list delimiter characters and - if so - escape them accordingly.
065     *
066     * @param value the value to be escaped
067     * @param transformer a {@code ValueTransformer} for an additional encoding (must not be <b>null</b>)
068     * @return the escaped value
069     */
070    Object escape(Object value, ValueTransformer transformer);
071
072    /**
073     * Escapes all values in the given list and concatenates them to a single string. This operation is required by
074     * configurations that have to represent properties with multiple values in a single line in their external
075     * configuration representation. This may require an advanced escaping in some cases.
076     *
077     * @param values the list with all the values to be converted to a single value
078     * @param transformer a {@code ValueTransformer} for an additional encoding (must not be <b>null</b>)
079     * @return the resulting escaped value
080     */
081    Object escapeList(List<?> values, ValueTransformer transformer);
082
083    /**
084     * Extracts all values contained in the specified object up to the given limit. The passed in object is evaluated (if
085     * necessary in a recursive way). If it is a complex object (e.g. a collection or an array), all its elements are
086     * processed recursively and added to a target collection. The process stops if the limit is reached, but depending on
087     * the input object, it might be exceeded. (The limit is just an indicator to stop the process to avoid unnecessary work
088     * if the caller is only interested in a few values.)
089     *
090     * @param value the value to be processed
091     * @param limit the limit for aborting the processing
092     * @return a &quot;flat&quot; collection containing all primitive values of the passed in object
093     * @since 2.9.0
094     */
095    default Collection<?> flatten(final Object value, final int limit) {
096        return AbstractListDelimiterHandler.flatten(this, value, limit, Collections.newSetFromMap(new IdentityHashMap<>()));
097    }
098
099    /**
100     * Parses the specified value for list delimiters and splits it if necessary. The passed in object can be either a
101     * single value or a complex one, e.g. a collection, an array, or an {@code Iterable}. It is the responsibility of this
102     * method to return an {@code Iterable} which contains all extracted values.
103     *
104     * @param value the value to be parsed
105     * @return an {@code Iterable} allowing access to all extracted values
106     */
107    Iterable<?> parse(Object value);
108
109    /**
110     * Splits the specified string at the list delimiter and returns a collection with all extracted components. A concrete
111     * implementation also has to deal with escape characters which might mask a list delimiter character at certain
112     * positions. The boolean {@code trim} flag determines whether each extracted component should be trimmed. This
113     * typically makes sense as the list delimiter may be surrounded by whitespace. However, there may be specific use cases
114     * in which automatic trimming is not desired.
115     *
116     * @param s the string to be split
117     * @param trim a flag whether each component of the string is to be trimmed
118     * @return a collection with all components extracted from the string
119     */
120    Collection<String> split(String s, boolean trim);
121
122}