BaseWebConfiguration.java

  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. import java.util.Collection;

  19. import org.apache.commons.configuration2.AbstractConfiguration;

  20. /**
  21.  * <p>
  22.  * An abstract base class for all web configurations.
  23.  * </p>
  24.  * <p>
  25.  * This class implements common functionality used by all web based configurations. E.g. some methods are not supported
  26.  * by configurations of this type, so they throw a {@code UnsupportedOperationException} exception.
  27.  * </p>
  28.  *
  29.  * @since 1.2
  30.  */
  31. abstract class BaseWebConfiguration extends AbstractConfiguration {
  32.     /**
  33.      * Adds a property to this configuration. <strong>This operation is not supported and will throw an
  34.      * UnsupportedOperationException.</strong>
  35.      *
  36.      * @param key the key of the property
  37.      * @param obj the value to be added
  38.      * @throws UnsupportedOperationException because this operation is not allowed
  39.      */
  40.     @Override
  41.     protected void addPropertyDirect(final String key, final Object obj) {
  42.         throw new UnsupportedOperationException("Read only configuration");
  43.     }

  44.     /**
  45.      * Removes the property with the given key. <strong>This operation is not supported and will throw an
  46.      * UnsupportedOperationException.</strong>
  47.      *
  48.      * @param key the key of the property to be removed
  49.      * @throws UnsupportedOperationException because this operation is not allowed
  50.      */
  51.     @Override
  52.     protected void clearPropertyDirect(final String key) {
  53.         throw new UnsupportedOperationException("Read only configuration");
  54.     }

  55.     /**
  56.      * Checks whether the specified key is stored in this configuration.
  57.      *
  58.      * @param key the key
  59.      * @return a flag whether this key exists in this configuration
  60.      */
  61.     @Override
  62.     protected boolean containsKeyInternal(final String key) {
  63.         return getPropertyInternal(key) != null;
  64.     }

  65.     /**
  66.      * Tests whether this configuration contains one or more matches to this value. This operation stops at first match
  67.      * but may be more expensive than the containsKey method
  68.      * @since 2.11.0
  69.      */
  70.     @Override
  71.     protected boolean containsValueInternal(final Object value) {
  72.         return contains(getKeys(), value);
  73.     }

  74.     /**
  75.      * Takes care of list delimiters in property values. This method checks if delimiter parsing is enabled and the passed
  76.      * in value contains a delimiter character. If this is the case, a split operation is performed.
  77.      *
  78.      * @param value the property value to be examined
  79.      * @return the processed value
  80.      */
  81.     protected Object handleDelimiters(Object value) {
  82.         if (value instanceof String) {
  83.             final Collection<String> values = getListDelimiterHandler().split((String) value, true);
  84.             value = values.size() > 1 ? values : values.iterator().next();
  85.         }

  86.         return value;
  87.     }

  88.     /**
  89.      * Checks if this configuration is empty. This implementation makes use of the {@code getKeys()} method (which must be
  90.      * defined by concrete sub classes) to find out whether properties exist.
  91.      *
  92.      * @return a flag whether this configuration is empty
  93.      */
  94.     @Override
  95.     protected boolean isEmptyInternal() {
  96.         return !getKeysInternal().hasNext();
  97.     }
  98. }