    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 *     http://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;
    018
    019import java.util.Collection;
    020import java.util.concurrent.CopyOnWriteArrayList;
    021
    022/**
    023 * <p>
    024 * A class for managing a set of {@link DefaultParametersHandler} objects.
    025 * </p>
    026 * <p>
    027 * This class provides functionality for registering and removing {@code DefaultParametersHandler} objects for arbitrary
    028 * parameters classes. The handlers registered at an instance can then be applied on a passed in parameters object, so
    029 * that it gets initialized with the provided default values.
    030 * </p>
    031 * <p>
    032 * Usage of this class is as follows: First the {@code DefaultParametersHandler} objects to be supported must be
    033 * registered using one of the {@code registerDefaultHandler()} methods. After that arbitrary parameters objects can be
    034 * passed to the {@code initializeParameters()} method. This causes all {@code DefaultParametersHandler} objects
    035 * supporting this parameters class to be invoked on this object.
    036 * </p>
    037 * <p>
    038 * Implementation note: This class is thread-safe.
    039 * </p>
    040 *
    041 * @since 2.0
    042 */
    043public class DefaultParametersManager {
    044    /** A collection with the registered default handlers. */
    045    private final Collection<DefaultHandlerData> defaultHandlers;
    046
    047    /**
    048     * Creates a new instance of {@code DefaultParametersManager}.
    049     */
    050    public DefaultParametersManager() {
    051        defaultHandlers = new CopyOnWriteArrayList<>();
    052    }
    053
    054    /**
    055     * Registers the specified {@code DefaultParametersHandler} object for the given parameters class. This means that this
    056     * handler object is invoked every time a parameters object of the specified class or one of its subclasses is
    057     * initialized. The handler can set arbitrary default values for the properties supported by this parameters object. If
    058     * there are multiple handlers registered supporting a specific parameters class, they are invoked in the order in which
    059     * they were registered. So handlers registered later may override the values set by handlers registered earlier.
    060     *
    061     * @param <T> the type of the parameters supported by this handler
    062     * @param paramsClass the parameters class supported by this handler (must not be <b>null</b>)
    063     * @param handler the {@code DefaultParametersHandler} to be registered (must not be <b>null</b>)
    064     * @throws IllegalArgumentException if a required parameter is missing
    065     */
    066    public <T> void registerDefaultsHandler(final Class<T> paramsClass, final DefaultParametersHandler<? super T> handler) {
    067        registerDefaultsHandler(paramsClass, handler, null);
    068    }
    069
    070    /**
    071     * Registers the specified {@code DefaultParametersHandler} object for the given parameters class and start class in the
    072     * inheritance hierarchy. This method works like {@link #registerDefaultsHandler(Class, DefaultParametersHandler)}, but
    073     * the defaults handler is only executed on parameter objects that are instances of the specified start class. Parameter
    074     * classes do not stand in a real inheritance hierarchy; however, there is a logic hierarchy defined by the methods
    075     * supported by the different parameter objects. A properties parameter object for instance supports all methods defined
    076     * for a file-based parameter object. So one can argue that
    077     * {@link org.apache.commons.configuration2.builder.fluent.FileBasedBuilderParameters FileBasedBuilderParameters} is a
    078     * base interface of {@link org.apache.commons.configuration2.builder.fluent.PropertiesBuilderParameters
    079     * PropertiesBuilderParameters} (although, for technical reasons, this relation is not reflected in the Java classes). A
    080     * {@link DefaultParametersHandler} object defined for a base interface can also deal with parameter objects "derived"
    081     * from this base interface (i.e. supporting a super set of the methods defined by the base interface). Now there may be
    082     * the use case that there is an implementation of {@code DefaultParametersHandler} for a base interface (e.g.
    083     * {@code FileBasedBuilderParameters}), but it should only process specific derived interfaces (say
    084     * {@code PropertiesBuilderParameters}, but not
    085     * {@link org.apache.commons.configuration2.builder.fluent.XMLBuilderParameters XMLBuilderParameters}). This can be
    086     * achieved by passing in {@code PropertiesBuilderParameters} as start class. In this case,
    087     * {@code DefaultParametersManager} ensures that the handler is only called on parameter objects having both the start
    088     * class and the actual type supported by the handler as base interfaces. The passed in start class can be <b>null</b>;
    089     * then the parameter class supported by the handler is used (which is the default behavior of the
    090     * {@link #registerDefaultsHandler(Class, DefaultParametersHandler)} method).
    091     *
    092     * @param <T> the type of the parameters supported by this handler
    093     * @param paramsClass the parameters class supported by this handler (must not be <b>null</b>)
    094     * @param handler the {@code DefaultParametersHandler} to be registered (must not be <b>null</b>)
    095     * @param startClass an optional start class in the hierarchy of parameter objects for which this handler should be
    096     *        applied
    097     * @throws IllegalArgumentException if a required parameter is missing
    098     */
    099    public <T> void registerDefaultsHandler(final Class<T> paramsClass, final DefaultParametersHandler<? super T> handler, final Class<?> startClass) {
    100        if (paramsClass == null) {
    101            throw new IllegalArgumentException("Parameters class must not be null!");
    102        }
    103        if (handler == null) {
    104            throw new IllegalArgumentException("DefaultParametersHandler must not be null!");
    105        }
    106        defaultHandlers.add(new DefaultHandlerData(handler, paramsClass, startClass));
    107    }
    108
    109    /**
    110     * Removes the specified {@code DefaultParametersHandler} from this instance. If this handler has been registered
    111     * multiple times for different start classes, all occurrences are removed.
    112     *
    113     * @param handler the {@code DefaultParametersHandler} to be removed
    114     */
    115    public void unregisterDefaultsHandler(final DefaultParametersHandler<?> handler) {
    116        unregisterDefaultsHandler(handler, null);
    117    }
    118
    119    /**
    120     * Removes the specified {@code DefaultParametersHandler} from this instance if it is in combination with the given
    121     * start class. If this handler has been registered multiple times for different start classes, only occurrences for the
    122     * given start class are removed. The {@code startClass} parameter can be <b>null</b>, then all occurrences of the
    123     * handler are removed.
    124     *
    125     * @param handler the {@code DefaultParametersHandler} to be removed
    126     * @param startClass the start class for which this handler is to be removed
    127     */
    128    public void unregisterDefaultsHandler(final DefaultParametersHandler<?> handler, final Class<?> startClass) {
    129        defaultHandlers.removeIf(dhd -> dhd.isOccurrence(handler, startClass));
    130    }
    131
    132    /**
    133     * Initializes the passed in {@code BuilderParameters} object by applying all matching {@link DefaultParametersHandler}
    134     * objects registered at this instance. Using this method the passed in parameters object can be populated with default
    135     * values.
    136     *
    137     * @param params the parameters object to be initialized (may be <b>null</b>, then this method has no effect)
    138     */
    139    public void initializeParameters(final BuilderParameters params) {
    140        if (params != null) {
    141            defaultHandlers.forEach(dhd -> dhd.applyHandlerIfMatching(params));
    142        }
    143    }
    144
    145    /**
    146     * A data class storing information about {@code DefaultParametersHandler} objects added to a {@code Parameters} object.
    147     * Using this class it is possible to find out which default handlers apply for a given parameters object and to invoke
    148     * them.
    149     */
    150    private static final class DefaultHandlerData {
    151        /** The handler object. */
    152        private final DefaultParametersHandler<?> handler;
    153
    154        /** The class supported by this handler. */
    155        private final Class<?> parameterClass;
    156
    157        /** The start class for applying this handler. */
    158        private final Class<?> startClass;
    159
    160        /**
    161         * Creates a new instance of {@code DefaultHandlerData}.
    162         *
    163         * @param h the {@code DefaultParametersHandler}
    164         * @param cls the handler's data class
    165         * @param startCls the start class
    166         */
    167        public DefaultHandlerData(final DefaultParametersHandler<?> h, final Class<?> cls, final Class<?> startCls) {
    168            handler = h;
    169            parameterClass = cls;
    170            startClass = startCls;
    171        }
    172
    173        /**
    174         * Checks whether the managed {@code DefaultParametersHandler} can be applied to the given parameters object. If this is
    175         * the case, it is executed on this object and can initialize it with default values.
    176         *
    177         * @param obj the parameters object to be initialized
    178         */
    179        @SuppressWarnings("unchecked")
    180        // There are explicit isInstance() checks, so there won't be
    181        // ClassCastExceptions
    182        public void applyHandlerIfMatching(final BuilderParameters obj) {
    183            if (parameterClass.isInstance(obj) && (startClass == null || startClass.isInstance(obj))) {
    184                @SuppressWarnings("rawtypes")
    185                final DefaultParametersHandler handlerUntyped = handler;
    186                handlerUntyped.initializeDefaults(obj);
    187            }
    188        }
    189
    190        /**
    191         * Tests whether this instance refers to the specified occurrence of a {@code DefaultParametersHandler}.
    192         *
    193         * @param h the handler to be checked
    194         * @param startCls the start class
    195         * @return <b>true</b> if this instance refers to this occurrence, <b>false</b> otherwise
    196         */
    197        public boolean isOccurrence(final DefaultParametersHandler<?> h, final Class<?> startCls) {
    198            return h == handler && (startCls == null || startCls.equals(startClass));
    199        }
    200    }
    201}