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    *     https://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      /**
43       * A default instance of this class. Because it is safe to share {@code DisabledListDelimiterHandler} objects this
44       * instance can be used whenever such an object is needed.
45       */
46      public static final ListDelimiterHandler INSTANCE = new DisabledListDelimiterHandler();
47  
48      /**
49       * Constructs a new instance.
50       */
51      public DisabledListDelimiterHandler() {
52          // empty
53      }
54  
55      /**
56       * {@inheritDoc} This implementation always throws an {@code UnsupportedOperationException} exception.
57       */
58      @Override
59      public Object escapeList(final List<?> values, final ValueTransformer transformer) {
60          throw new UnsupportedOperationException("Escaping lists is not supported.");
61      }
62  
63      /**
64       * {@inheritDoc} This implementation returns the passed in string without any changes.
65       */
66      @Override
67      protected String escapeString(final String s) {
68          return s;
69      }
70  
71      /**
72       * {@inheritDoc} This implementation always returns a collection containing the passed in string as its single element.
73       * The string is not changed, the {@code trim} flag is ignored. (The {@code trim} flag refers to the components
74       * extracted from the string. Because no components are extracted nothing is trimmed.)
75       */
76      @Override
77      protected Collection<String> splitString(final String s, final boolean trim) {
78          final Collection<String> result = new ArrayList<>(1);
79          result.add(s);
80          return result;
81      }
82  }