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.ArrayList;
020import java.util.Collection;
021import java.util.List;
022
023/**
024 * <p>
025 * A specialized implementation of the {@code ListDelimiterHandler} interface which disables list splitting.
026 * </p>
027 * <p>
028 * This class does not recognize any list delimiters; passed in strings are returned unchanged. Also the
029 * {@code escape()} method is a dummy - there is no need for escaping delimiter characters as none are supported. Note
030 * that the method for escaping a list throws an {@code UnsupportedOperationException}. If list delimiters are not
031 * supported, there is no point in squashing multiple values into a single one.
032 * </p>
033 * <p>
034 * Implementation note: An instance of this class can be shared between multiple configuration objects. It is state-less
035 * and thread-safe.
036 * </p>
037 *
038 * @since 2.0
039 */
040public class DisabledListDelimiterHandler extends AbstractListDelimiterHandler {
041    /**
042     * A default instance of this class. Because it is safe to share {@code DisabledListDelimiterHandler} objects this
043     * instance can be used whenever such an object is needed.
044     */
045    public static final ListDelimiterHandler INSTANCE = new DisabledListDelimiterHandler();
046
047    /**
048     * {@inheritDoc} This implementation always throws an {@code UnsupportedOperationException} exception.
049     */
050    @Override
051    public Object escapeList(final List<?> values, final ValueTransformer transformer) {
052        throw new UnsupportedOperationException("Escaping lists is not supported!");
053    }
054
055    /**
056     * {@inheritDoc} This implementation always returns a collection containing the passed in string as its single element.
057     * The string is not changed, the {@code trim} flag is ignored. (The {@code trim} flag refers to the components
058     * extracted from the string. Because no components are extracted nothing is trimmed.)
059     */
060    @Override
061    protected Collection<String> splitString(final String s, final boolean trim) {
062        final Collection<String> result = new ArrayList<>(1);
063        result.add(s);
064        return result;
065    }
066
067    /**
068     * {@inheritDoc} This implementation returns the passed in string without any changes.
069     */
070    @Override
071    protected String escapeString(final String s) {
072        return s;
073    }
074}