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.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       * Adds a property to this configuration. <strong>This operation is not supported and will throw an
37       * UnsupportedOperationException.</strong>
38       *
39       * @param key the key of the property
40       * @param obj the value to be added
41       * @throws UnsupportedOperationException because this operation is not allowed
42       */
43      @Override
44      protected void addPropertyDirect(final String key, final Object obj) {
45          throw new UnsupportedOperationException("Read only configuration");
46      }
47  
48      /**
49       * Removes the property with the given key. <strong>This operation is not supported and will throw an
50       * UnsupportedOperationException.</strong>
51       *
52       * @param key the key of the property to be removed
53       * @throws UnsupportedOperationException because this operation is not allowed
54       */
55      @Override
56      protected void clearPropertyDirect(final String key) {
57          throw new UnsupportedOperationException("Read only configuration");
58      }
59  
60      /**
61       * Checks whether the specified key is stored in this configuration.
62       *
63       * @param key the key
64       * @return a flag whether this key exists in this configuration
65       */
66      @Override
67      protected boolean containsKeyInternal(final String key) {
68          return getPropertyInternal(key) != null;
69      }
70  
71      /**
72       * Tests whether this configuration contains one or more matches to this value. This operation stops at first match
73       * but may be more expensive than the containsKey method
74       * @since 2.11.0
75       */
76      @Override
77      protected boolean containsValueInternal(final Object value) {
78          return contains(getKeys(), value);
79      }
80  
81      /**
82       * Takes care of list delimiters in property values. This method checks if delimiter parsing is enabled and the passed
83       * in value contains a delimiter character. If this is the case, a split operation is performed.
84       *
85       * @param value the property value to be examined
86       * @return the processed value
87       */
88      protected Object handleDelimiters(Object value) {
89          if (value instanceof String) {
90              final Collection<String> values = getListDelimiterHandler().split((String) value, true);
91              value = values.size() > 1 ? values : values.iterator().next();
92          }
93  
94          return value;
95      }
96  
97      /**
98       * Checks if this configuration is empty. This implementation makes use of the {@code getKeys()} method (which must be
99       * defined by concrete sub classes) to find out whether properties exist.
100      *
101      * @return a flag whether this configuration is empty
102      */
103     @Override
104     protected boolean isEmptyInternal() {
105         return !getKeysInternal().hasNext();
106     }
107 }