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.web;
18  
19  import java.util.Collection;
20  
21  import org.apache.commons.configuration2.AbstractConfiguration;
22  
23  /**
24   * <p>
25   * An abstract base class for all web configurations.
26   * </p>
27   * <p>
28   * This class implements common functionality used by all web based configurations. E.g. some methods are not supported
29   * by configurations of this type, so they throw a {@code UnsupportedOperationException} exception.
30   * </p>
31   *
32   * @since 1.2
33   */
34  abstract class BaseWebConfiguration extends AbstractConfiguration {
35  
36      /**
37       * Adds a property to this configuration. <strong>This operation is not supported and will throw an
38       * UnsupportedOperationException.</strong>
39       *
40       * @param key the key of the property
41       * @param obj the value to be added
42       * @throws UnsupportedOperationException because this operation is not allowed
43       */
44      @Override
45      protected void addPropertyDirect(final String key, final Object obj) {
46          throw new UnsupportedOperationException("Read only configuration");
47      }
48  
49      /**
50       * Removes the property with the given key. <strong>This operation is not supported and will throw an
51       * UnsupportedOperationException.</strong>
52       *
53       * @param key the key of the property to be removed
54       * @throws UnsupportedOperationException because this operation is not allowed
55       */
56      @Override
57      protected void clearPropertyDirect(final String key) {
58          throw new UnsupportedOperationException("Read only configuration");
59      }
60  
61      /**
62       * Checks whether the specified key is stored in this configuration.
63       *
64       * @param key the key
65       * @return a flag whether this key exists in this configuration
66       */
67      @Override
68      protected boolean containsKeyInternal(final String key) {
69          return getPropertyInternal(key) != null;
70      }
71  
72      /**
73       * Tests whether this configuration contains one or more matches to this value. This operation stops at first match
74       * but may be more expensive than the containsKey method
75       *
76       * @since 2.11.0
77       */
78      @Override
79      protected boolean containsValueInternal(final Object value) {
80          return contains(getKeys(), value);
81      }
82  
83      /**
84       * Takes care of list delimiters in property values. This method checks if delimiter parsing is enabled and the passed
85       * in value contains a delimiter character. If this is the case, a split operation is performed.
86       *
87       * @param value the property value to be examined
88       * @return the processed value
89       */
90      protected Object handleDelimiters(Object value) {
91          if (value instanceof String) {
92              final Collection<String> values = getListDelimiterHandler().split((String) value, true);
93              value = values.size() > 1 ? values : values.iterator().next();
94          }
95  
96          return value;
97      }
98  
99      /**
100      * Checks if this configuration is empty. This implementation makes use of the {@code getKeys()} method (which must be
101      * defined by concrete sub classes) to find out whether properties exist.
102      *
103      * @return a flag whether this configuration is empty
104      */
105     @Override
106     protected boolean isEmptyInternal() {
107         return !getKeysInternal().hasNext();
108     }
109 }