DisabledListDelimiterHandler.java

  1. /*
  2.  * Licensed to the Apache Software Foundation (ASF) under one or more
  3.  * contributor license agreements.  See the NOTICE file distributed with
  4.  * this work for additional information regarding copyright ownership.
  5.  * The ASF licenses this file to You under the Apache License, Version 2.0
  6.  * (the "License"); you may not use this file except in compliance with
  7.  * the License.  You may obtain a copy of the License at
  8.  *
  9.  *     http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */
  17. package org.apache.commons.configuration2.convert;

  18. import java.util.ArrayList;
  19. import java.util.Collection;
  20. import java.util.List;

  21. /**
  22.  * <p>
  23.  * A specialized implementation of the {@code ListDelimiterHandler} interface which disables list splitting.
  24.  * </p>
  25.  * <p>
  26.  * This class does not recognize any list delimiters; passed in strings are returned unchanged. Also the
  27.  * {@code escape()} method is a dummy - there is no need for escaping delimiter characters as none are supported. Note
  28.  * that the method for escaping a list throws an {@code UnsupportedOperationException}. If list delimiters are not
  29.  * supported, there is no point in squashing multiple values into a single one.
  30.  * </p>
  31.  * <p>
  32.  * Implementation note: An instance of this class can be shared between multiple configuration objects. It is state-less
  33.  * and thread-safe.
  34.  * </p>
  35.  *
  36.  * @since 2.0
  37.  */
  38. public class DisabledListDelimiterHandler extends AbstractListDelimiterHandler {
  39.     /**
  40.      * A default instance of this class. Because it is safe to share {@code DisabledListDelimiterHandler} objects this
  41.      * instance can be used whenever such an object is needed.
  42.      */
  43.     public static final ListDelimiterHandler INSTANCE = new DisabledListDelimiterHandler();

  44.     /**
  45.      * {@inheritDoc} This implementation always throws an {@code UnsupportedOperationException} exception.
  46.      */
  47.     @Override
  48.     public Object escapeList(final List<?> values, final ValueTransformer transformer) {
  49.         throw new UnsupportedOperationException("Escaping lists is not supported!");
  50.     }

  51.     /**
  52.      * {@inheritDoc} This implementation returns the passed in string without any changes.
  53.      */
  54.     @Override
  55.     protected String escapeString(final String s) {
  56.         return s;
  57.     }

  58.     /**
  59.      * {@inheritDoc} This implementation always returns a collection containing the passed in string as its single element.
  60.      * The string is not changed, the {@code trim} flag is ignored. (The {@code trim} flag refers to the components
  61.      * extracted from the string. Because no components are extracted nothing is trimmed.)
  62.      */
  63.     @Override
  64.     protected Collection<String> splitString(final String s, final boolean trim) {
  65.         final Collection<String> result = new ArrayList<>(1);
  66.         result.add(s);
  67.         return result;
  68.     }
  69. }