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