View Javadoc
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  
19  import java.util.ArrayList;
20  import java.util.Collection;
21  import java.util.List;
22  
23  /**
24   * <p>
25   * A specialized implementation of the {@code ListDelimiterHandler} interface which disables list splitting.
26   * </p>
27   * <p>
28   * This class does not recognize any list delimiters; passed in strings are returned unchanged. Also the
29   * {@code escape()} method is a dummy - there is no need for escaping delimiter characters as none are supported. Note
30   * that the method for escaping a list throws an {@code UnsupportedOperationException}. If list delimiters are not
31   * supported, there is no point in squashing multiple values into a single one.
32   * </p>
33   * <p>
34   * Implementation note: An instance of this class can be shared between multiple configuration objects. It is state-less
35   * and thread-safe.
36   * </p>
37   *
38   * @since 2.0
39   */
40  public class DisabledListDelimiterHandler extends AbstractListDelimiterHandler {
41      /**
42       * A default instance of this class. Because it is safe to share {@code DisabledListDelimiterHandler} objects this
43       * instance can be used whenever such an object is needed.
44       */
45      public static final ListDelimiterHandler INSTANCE = new DisabledListDelimiterHandler();
46  
47      /**
48       * {@inheritDoc} This implementation always throws an {@code UnsupportedOperationException} exception.
49       */
50      @Override
51      public Object escapeList(final List<?> values, final ValueTransformer transformer) {
52          throw new UnsupportedOperationException("Escaping lists is not supported!");
53      }
54  
55      /**
56       * {@inheritDoc} This implementation always returns a collection containing the passed in string as its single element.
57       * The string is not changed, the {@code trim} flag is ignored. (The {@code trim} flag refers to the components
58       * extracted from the string. Because no components are extracted nothing is trimmed.)
59       */
60      @Override
61      protected Collection<String> splitString(final String s, final boolean trim) {
62          final Collection<String> result = new ArrayList<>(1);
63          result.add(s);
64          return result;
65      }
66  
67      /**
68       * {@inheritDoc} This implementation returns the passed in string without any changes.
69       */
70      @Override
71      protected String escapeString(final String s) {
72          return s;
73      }
74  }