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