InterpolatorSpecification.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.interpol;

  18. import java.util.ArrayList;
  19. import java.util.Collection;
  20. import java.util.Collections;
  21. import java.util.HashMap;
  22. import java.util.LinkedList;
  23. import java.util.Map;
  24. import java.util.function.Function;

  25. /**
  26.  * <p>
  27.  * A simple value class defining a {@link ConfigurationInterpolator}.
  28.  * </p>
  29.  * <p>
  30.  * Objects of this class can be used for creating new {@code ConfigurationInterpolator} instances; they contain all
  31.  * required properties. It is either possible to set a fully initialized {@code ConfigurationInterpolator} directly
  32.  * which can be used as is. Alternatively, some or all properties of an instance to be newly created can be set. These
  33.  * properties include
  34.  * </p>
  35.  * <ul>
  36.  * <li>a map with {@code Lookup} objects associated with a specific prefix</li>
  37.  * <li>a collection with default {@code Lookup} objects (without a prefix)</li>
  38.  * <li>a parent {@code ConfigurationInterpolator}</li>
  39.  * <li>a function used to convert interpolated values into strings</li>
  40.  * </ul>
  41.  * <p>
  42.  * When setting up a configuration it is possible to define the {@code ConfigurationInterpolator} in terms of this
  43.  * class. The configuration will then either use the {@code ConfigurationInterpolator} instance explicitly defined in
  44.  * the {@code InterpolatorSpecification} instance or create a new one.
  45.  * </p>
  46.  * <p>
  47.  * Instances are not created directly, but using the nested {@code Builder} class. They are then immutable.
  48.  * </p>
  49.  *
  50.  * @since 2.0
  51.  */
  52. public final class InterpolatorSpecification {
  53.     /**
  54.      * <p>
  55.      * A <em>builder</em> class for creating instances of {@code InterpolatorSpecification}.
  56.      * </p>
  57.      * <p>
  58.      * This class provides a fluent API for defining the various properties of an {@code InterpolatorSpecification} object.
  59.      * <em>Note:</em> This builder class is not thread-safe.
  60.      * </p>
  61.      */
  62.     public static class Builder {
  63.         /**
  64.          * Helper method for checking a lookup. Throws an exception if the lookup is <strong>null</strong>.
  65.          *
  66.          * @param lookup the lookup to be checked
  67.          * @throws IllegalArgumentException if the lookup is <strong>null</strong>
  68.          */
  69.         private static void checkLookup(final Lookup lookup) {
  70.             if (lookup == null) {
  71.                 throw new IllegalArgumentException("Lookup must not be null!");
  72.             }
  73.         }

  74.         /** A map with prefix lookups. */
  75.         private final Map<String, Lookup> prefixLookups;

  76.         /** A collection with default lookups. */
  77.         private final Collection<Lookup> defLookups;

  78.         /** The {@code ConfigurationInterpolator}. */
  79.         private ConfigurationInterpolator interpolator;

  80.         /** The parent {@code ConfigurationInterpolator}. */
  81.         private ConfigurationInterpolator parentInterpolator;

  82.         /** Function used to convert interpolated values to strings. */
  83.         private Function<Object, String> stringConverter;

  84.         /**
  85.          * Constructs a new instance.
  86.          */
  87.         public Builder() {
  88.             prefixLookups = new HashMap<>();
  89.             defLookups = new LinkedList<>();
  90.         }

  91.         /**
  92.          * Creates a new {@code InterpolatorSpecification} instance with the properties set so far. After that this builder
  93.          * instance is reset so that it can be reused for creating further specification objects.
  94.          *
  95.          * @return the newly created {@code InterpolatorSpecification}
  96.          */
  97.         public InterpolatorSpecification create() {
  98.             final InterpolatorSpecification spec = new InterpolatorSpecification(this);
  99.             reset();
  100.             return spec;
  101.         }

  102.         /**
  103.          * Removes all data from this builder. Afterwards it can be used to define a brand new {@code InterpolatorSpecification}
  104.          * object.
  105.          */
  106.         public void reset() {
  107.             interpolator = null;
  108.             parentInterpolator = null;
  109.             prefixLookups.clear();
  110.             defLookups.clear();
  111.             stringConverter = null;
  112.         }

  113.         /**
  114.          * Adds the given {@code Lookup} object to the list of default lookups.
  115.          *
  116.          * @param lookup the {@code Lookup} (must not be <strong>null</strong>)
  117.          * @return a reference to this builder for method chaining
  118.          * @throws IllegalArgumentException if the {@code Lookup} is <strong>null</strong>
  119.          */
  120.         public Builder withDefaultLookup(final Lookup lookup) {
  121.             checkLookup(lookup);
  122.             defLookups.add(lookup);
  123.             return this;
  124.         }

  125.         /**
  126.          * Adds the content of the given collection to the default lookups managed by this builder. The collection can be
  127.          * <strong>null</strong>, then this method has no effect.
  128.          *
  129.          * @param lookups the collection with lookups to be added
  130.          * @return a reference to this builder for method chaining
  131.          * @throws IllegalArgumentException if the collection contains <strong>null</strong> entries
  132.          */
  133.         public Builder withDefaultLookups(final Collection<? extends Lookup> lookups) {
  134.             if (lookups != null) {
  135.                 lookups.forEach(this::withDefaultLookup);
  136.             }
  137.             return this;
  138.         }

  139.         /**
  140.          * Sets the {@code ConfigurationInterpolator} instance for the {@code InterpolatorSpecification}. This means that a
  141.          * {@code ConfigurationInterpolator} has been created and set up externally and can be used directly.
  142.          *
  143.          * @param ci the {@code ConfigurationInterpolator} (can be <strong>null</strong>)
  144.          * @return a reference to this builder for method chaining
  145.          */
  146.         public Builder withInterpolator(final ConfigurationInterpolator ci) {
  147.             interpolator = ci;
  148.             return this;
  149.         }

  150.         /**
  151.          * Sets an optional parent {@code ConfigurationInterpolator}. If defined, this object is set as parent of a newly
  152.          * created {@code ConfigurationInterpolator} instance.
  153.          *
  154.          * @param parent the parent {@code ConfigurationInterpolator} (can be <strong>null</strong>)
  155.          * @return a reference to this builder for method chaining
  156.          */
  157.         public Builder withParentInterpolator(final ConfigurationInterpolator parent) {
  158.             parentInterpolator = parent;
  159.             return this;
  160.         }

  161.         /**
  162.          * Adds a {@code Lookup} object for a given prefix.
  163.          *
  164.          * @param prefix the prefix (must not be <strong>null</strong>)
  165.          * @param lookup the {@code Lookup} (must not be <strong>null</strong>)
  166.          * @return a reference to this builder for method chaining
  167.          * @throws IllegalArgumentException if a required parameter is missing
  168.          */
  169.         public Builder withPrefixLookup(final String prefix, final Lookup lookup) {
  170.             if (prefix == null) {
  171.                 throw new IllegalArgumentException("Prefix must not be null!");
  172.             }
  173.             checkLookup(lookup);
  174.             prefixLookups.put(prefix, lookup);
  175.             return this;
  176.         }

  177.         /**
  178.          * Adds the content of the given map to the prefix lookups managed by this builder. The map can be <strong>null</strong>, then
  179.          * this method has no effect.
  180.          *
  181.          * @param lookups the map with prefix lookups to be added
  182.          * @return a reference to this builder for method chaining
  183.          * @throws IllegalArgumentException if the map contains <strong>null</strong> values
  184.          */
  185.         public Builder withPrefixLookups(final Map<String, ? extends Lookup> lookups) {
  186.             if (lookups != null) {
  187.                 lookups.forEach(this::withPrefixLookup);
  188.             }
  189.             return this;
  190.         }

  191.         /**
  192.          * Sets the function used to convert interpolated values to strings. Pass {@code null}
  193.          * if the default conversion function is to be used.
  194.          *
  195.          * @param fn function used to convert interpolated values to string or {@code null} if the
  196.          *      default conversion function is to be used
  197.          * @return a reference to this builder for method chaining
  198.          */
  199.         public Builder withStringConverter(final Function<Object, String> fn) {
  200.             this.stringConverter = fn;
  201.             return this;
  202.         }
  203.     }

  204.     /** The {@code ConfigurationInterpolator} instance to be used directly. */
  205.     private final ConfigurationInterpolator interpolator;

  206.     /** The parent {@code ConfigurationInterpolator}. */
  207.     private final ConfigurationInterpolator parentInterpolator;

  208.     /** The map with prefix lookups. */
  209.     private final Map<String, Lookup> prefixLookups;

  210.     /** The collection with default lookups. */
  211.     private final Collection<Lookup> defaultLookups;

  212.     /** Function used to convert interpolated values to strings. */
  213.     private final Function<Object, String> stringConverter;

  214.     /**
  215.      * Creates a new instance of {@code InterpolatorSpecification} with the properties defined by the given builder object.
  216.      *
  217.      * @param builder the builder
  218.      */
  219.     private InterpolatorSpecification(final Builder builder) {
  220.         interpolator = builder.interpolator;
  221.         parentInterpolator = builder.parentInterpolator;
  222.         prefixLookups = Collections.unmodifiableMap(new HashMap<>(builder.prefixLookups));
  223.         defaultLookups = Collections.unmodifiableCollection(new ArrayList<>(builder.defLookups));
  224.         stringConverter = builder.stringConverter;
  225.     }

  226.     /**
  227.      * Gets a collection with the default lookups.
  228.      *
  229.      * @return the default lookups for a new {@code ConfigurationInterpolator} instance (never <strong>null</strong>)
  230.      */
  231.     public Collection<Lookup> getDefaultLookups() {
  232.         return defaultLookups;
  233.     }

  234.     /**
  235.      * Gets the {@code ConfigurationInterpolator} instance to be used directly.
  236.      *
  237.      * @return the {@code ConfigurationInterpolator} (can be <strong>null</strong>)
  238.      */
  239.     public ConfigurationInterpolator getInterpolator() {
  240.         return interpolator;
  241.     }

  242.     /**
  243.      * Gets the parent {@code ConfigurationInterpolator} object.
  244.      *
  245.      * @return the parent {@code ConfigurationInterpolator} (can be <strong>null</strong>)
  246.      */
  247.     public ConfigurationInterpolator getParentInterpolator() {
  248.         return parentInterpolator;
  249.     }

  250.     /**
  251.      * Gets a map with prefix lookups. The keys of the map are the prefix strings, its values are the corresponding
  252.      * {@code Lookup} objects.
  253.      *
  254.      * @return the prefix lookups for a new {@code ConfigurationInterpolator} instance (never <strong>null</strong>)
  255.      */
  256.     public Map<String, Lookup> getPrefixLookups() {
  257.         return prefixLookups;
  258.     }

  259.     /**
  260.      * Gets the function used to convert interpolated values to strings or {@code null}
  261.      * if the default conversion function is to be used.
  262.      *
  263.      * @return function used to convert interpolated values to strings or {@code null} if
  264.      *      the default conversion function is to be used
  265.      */
  266.     public Function<Object, String> getStringConverter() {
  267.         return stringConverter;
  268.     }
  269. }