001/* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * https://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017package org.apache.commons.configuration2.builder.combined; 018 019import java.util.Map; 020 021import org.apache.commons.configuration2.ConfigurationUtils; 022import org.apache.commons.configuration2.builder.BasicBuilderParameters; 023import org.apache.commons.configuration2.builder.BuilderParameters; 024 025/** 026 * <p> 027 * A specialized parameters object for {@link MultiFileConfigurationBuilder}. 028 * </p> 029 * <p> 030 * A parameters object of this type is used by a configuration builder with manages multiple file-based configurations. 031 * Such a builder is a bit special because it does not create a configuration on its own, but delegates to a file-based 032 * builder for this purpose. Therefore, parameters inherited from the super class are treated differently: 033 * </p> 034 * <ul> 035 * <li>The {@link org.apache.commons.configuration2.interpol.ConfigurationInterpolator ConfigurationInterpolator} is 036 * needed by a {@code MultiFileConfigurationBuilder} to resolve the file pattern. It is expected to be set and will not 037 * be passed to sub configurations created by the builder.</li> 038 * <li>All other parameters are evaluated when creating sub configurations. However, it is preferred to use the 039 * {@link #setManagedBuilderParameters(BuilderParameters)} method to define all properties of sub configurations in a 040 * single place. If such a parameters object is set, its properties take precedence.</li> 041 * </ul> 042 * <p> 043 * This class is not thread-safe. It is intended that an instance is constructed and initialized by a single thread 044 * during configuration of a {@code ConfigurationBuilder}. 045 * </p> 046 * 047 * @since 2.0 048 */ 049public class MultiFileBuilderParametersImpl extends BasicBuilderParameters implements MultiFileBuilderProperties<MultiFileBuilderParametersImpl> { 050 /** Constant for the key in the parameters map used by this class. */ 051 private static final String PARAM_KEY = RESERVED_PARAMETER_PREFIX + MultiFileBuilderParametersImpl.class.getName(); 052 053 /** 054 * Obtains an instance of this class from the given map with parameters. If this map does not contain an instance, 055 * result is <strong>null</strong>. This is equivalent to {@code fromParameters(params, false)}. 056 * 057 * @param params the map with parameters (must not be <strong>null</strong>) 058 * @return an instance of this class fetched from the map or <strong>null</strong> 059 * @throws NullPointerException if the map with parameters is <strong>null</strong> 060 */ 061 public static MultiFileBuilderParametersImpl fromParameters(final Map<String, Object> params) { 062 return fromParameters(params, false); 063 } 064 065 /** 066 * Obtains an instance of this class from the given map with parameters and creates a new object if such an instance 067 * cannot be found. This method can be used to obtain an instance from a map which has been created using the 068 * {@code getParameters()} method. If the map does not contain an instance under the expected key and the 069 * {@code createIfMissing} parameter is <strong>true</strong>, a new instance is created. Otherwise, result is <strong>null</strong>. 070 * 071 * @param params the map with parameters (must not be <strong>null</strong>) 072 * @param createIfMissing a flag whether a new instance should be created if necessary 073 * @return an instance of this class fetched from the map or <strong>null</strong> 074 * @throws NullPointerException if the map with parameters is <strong>null</strong> 075 */ 076 public static MultiFileBuilderParametersImpl fromParameters(final Map<String, Object> params, final boolean createIfMissing) { 077 MultiFileBuilderParametersImpl instance = (MultiFileBuilderParametersImpl) params.get(PARAM_KEY); 078 if (instance == null && createIfMissing) { 079 instance = new MultiFileBuilderParametersImpl(); 080 } 081 return instance; 082 } 083 084 /** The parameters object for managed builders. */ 085 private BuilderParameters managedBuilderParameters; 086 087 /** The file pattern. */ 088 private String filePattern; 089 090 /** 091 * Constructs a new instance. 092 */ 093 public MultiFileBuilderParametersImpl() { 094 // empty 095 } 096 097 /** 098 * {@inheritDoc} This implementation also tries to clone the parameters object for managed builders if possible. 099 */ 100 @Override 101 public MultiFileBuilderParametersImpl clone() { 102 final MultiFileBuilderParametersImpl copy = (MultiFileBuilderParametersImpl) super.clone(); 103 copy.setManagedBuilderParameters((BuilderParameters) ConfigurationUtils.cloneIfPossible(getManagedBuilderParameters())); 104 return copy; 105 } 106 107 /** 108 * Gets the pattern for determining file names for managed configurations. 109 * 110 * @return the file pattern 111 */ 112 public String getFilePattern() { 113 return filePattern; 114 } 115 116 /** 117 * Gets the parameters object for managed configuration builders. 118 * 119 * @return the parameters for sub configurations 120 */ 121 public BuilderParameters getManagedBuilderParameters() { 122 return managedBuilderParameters; 123 } 124 125 /** 126 * {@inheritDoc} This implementation puts a reference to this object under a reserved key in the resulting parameters 127 * map. 128 */ 129 @Override 130 public Map<String, Object> getParameters() { 131 final Map<String, Object> params = super.getParameters(); 132 params.put(PARAM_KEY, this); 133 return params; 134 } 135 136 @Override 137 public MultiFileBuilderParametersImpl setFilePattern(final String p) { 138 filePattern = p; 139 return this; 140 } 141 142 @Override 143 public MultiFileBuilderParametersImpl setManagedBuilderParameters(final BuilderParameters p) { 144 managedBuilderParameters = p; 145 return this; 146 } 147}