MultiFileBuilderParametersImpl.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.builder.combined;

  18. import java.util.Map;

  19. import org.apache.commons.configuration2.ConfigurationUtils;
  20. import org.apache.commons.configuration2.builder.BasicBuilderParameters;
  21. import org.apache.commons.configuration2.builder.BuilderParameters;

  22. /**
  23.  * <p>
  24.  * A specialized parameters object for {@link MultiFileConfigurationBuilder}.
  25.  * </p>
  26.  * <p>
  27.  * A parameters object of this type is used by a configuration builder with manages multiple file-based configurations.
  28.  * Such a builder is a bit special because it does not create a configuration on its own, but delegates to a file-based
  29.  * builder for this purpose. Therefore, parameters inherited from the super class are treated differently:
  30.  * </p>
  31.  * <ul>
  32.  * <li>The {@link org.apache.commons.configuration2.interpol.ConfigurationInterpolator ConfigurationInterpolator} is
  33.  * needed by a {@code MultiFileConfigurationBuilder} to resolve the file pattern. It is expected to be set and will not
  34.  * be passed to sub configurations created by the builder.</li>
  35.  * <li>All other parameters are evaluated when creating sub configurations. However, it is preferred to use the
  36.  * {@link #setManagedBuilderParameters(BuilderParameters)} method to define all properties of sub configurations in a
  37.  * single place. If such a parameters object is set, its properties take precedence.</li>
  38.  * </ul>
  39.  * <p>
  40.  * This class is not thread-safe. It is intended that an instance is constructed and initialized by a single thread
  41.  * during configuration of a {@code ConfigurationBuilder}.
  42.  * </p>
  43.  *
  44.  * @since 2.0
  45.  */
  46. public class MultiFileBuilderParametersImpl extends BasicBuilderParameters implements MultiFileBuilderProperties<MultiFileBuilderParametersImpl> {
  47.     /** Constant for the key in the parameters map used by this class. */
  48.     private static final String PARAM_KEY = RESERVED_PARAMETER_PREFIX + MultiFileBuilderParametersImpl.class.getName();

  49.     /**
  50.      * Obtains an instance of this class from the given map with parameters. If this map does not contain an instance,
  51.      * result is <strong>null</strong>. This is equivalent to {@code fromParameters(params, false)}.
  52.      *
  53.      * @param params the map with parameters (must not be <strong>null</strong>)
  54.      * @return an instance of this class fetched from the map or <strong>null</strong>
  55.      * @throws NullPointerException if the map with parameters is <strong>null</strong>
  56.      */
  57.     public static MultiFileBuilderParametersImpl fromParameters(final Map<String, Object> params) {
  58.         return fromParameters(params, false);
  59.     }

  60.     /**
  61.      * Obtains an instance of this class from the given map with parameters and creates a new object if such an instance
  62.      * cannot be found. This method can be used to obtain an instance from a map which has been created using the
  63.      * {@code getParameters()} method. If the map does not contain an instance under the expected key and the
  64.      * {@code createIfMissing} parameter is <strong>true</strong>, a new instance is created. Otherwise, result is <strong>null</strong>.
  65.      *
  66.      * @param params the map with parameters (must not be <strong>null</strong>)
  67.      * @param createIfMissing a flag whether a new instance should be created if necessary
  68.      * @return an instance of this class fetched from the map or <strong>null</strong>
  69.      * @throws NullPointerException if the map with parameters is <strong>null</strong>
  70.      */
  71.     public static MultiFileBuilderParametersImpl fromParameters(final Map<String, Object> params, final boolean createIfMissing) {
  72.         MultiFileBuilderParametersImpl instance = (MultiFileBuilderParametersImpl) params.get(PARAM_KEY);
  73.         if (instance == null && createIfMissing) {
  74.             instance = new MultiFileBuilderParametersImpl();
  75.         }
  76.         return instance;
  77.     }

  78.     /** The parameters object for managed builders. */
  79.     private BuilderParameters managedBuilderParameters;

  80.     /** The file pattern. */
  81.     private String filePattern;

  82.     /**
  83.      * {@inheritDoc} This implementation also tries to clone the parameters object for managed builders if possible.
  84.      */
  85.     @Override
  86.     public MultiFileBuilderParametersImpl clone() {
  87.         final MultiFileBuilderParametersImpl copy = (MultiFileBuilderParametersImpl) super.clone();
  88.         copy.setManagedBuilderParameters((BuilderParameters) ConfigurationUtils.cloneIfPossible(getManagedBuilderParameters()));
  89.         return copy;
  90.     }

  91.     /**
  92.      * Gets the pattern for determining file names for managed configurations.
  93.      *
  94.      * @return the file pattern
  95.      */
  96.     public String getFilePattern() {
  97.         return filePattern;
  98.     }

  99.     /**
  100.      * Gets the parameters object for managed configuration builders.
  101.      *
  102.      * @return the parameters for sub configurations
  103.      */
  104.     public BuilderParameters getManagedBuilderParameters() {
  105.         return managedBuilderParameters;
  106.     }

  107.     /**
  108.      * {@inheritDoc} This implementation puts a reference to this object under a reserved key in the resulting parameters
  109.      * map.
  110.      */
  111.     @Override
  112.     public Map<String, Object> getParameters() {
  113.         final Map<String, Object> params = super.getParameters();
  114.         params.put(PARAM_KEY, this);
  115.         return params;
  116.     }

  117.     @Override
  118.     public MultiFileBuilderParametersImpl setFilePattern(final String p) {
  119.         filePattern = p;
  120.         return this;
  121.     }

  122.     @Override
  123.     public MultiFileBuilderParametersImpl setManagedBuilderParameters(final BuilderParameters p) {
  124.         managedBuilderParameters = p;
  125.         return this;
  126.     }
  127. }