ConfigurationLookup.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;

  18. import org.apache.commons.configuration2.interpol.Lookup;

  19. /**
  20.  * <p>
  21.  * A specialized implementation of the {@code Lookup} interface which uses a {@code Configuration} object to resolve
  22.  * variables.
  23.  * </p>
  24.  * <p>
  25.  * This class is passed an {@link ImmutableConfiguration} object at construction time. In its implementation of the
  26.  * {@code lookup()} method it simply queries this configuration for the passed in variable name. So the keys passed to
  27.  * {@code lookup()} are mapped directly to configuration properties.
  28.  * </p>
  29.  *
  30.  * @since 2.0
  31.  */
  32. public class ConfigurationLookup implements Lookup {
  33.     /** The configuration to which lookups are delegated. */
  34.     private final ImmutableConfiguration configuration;

  35.     /**
  36.      * Creates a new instance of {@code ConfigurationLookup} and sets the associated {@code ImmutableConfiguration}.
  37.      *
  38.      * @param config the configuration to use for lookups (must not be <strong>null</strong>)
  39.      * @throws IllegalArgumentException if the configuration is <strong>null</strong>
  40.      */
  41.     public ConfigurationLookup(final ImmutableConfiguration config) {
  42.         if (config == null) {
  43.             throw new IllegalArgumentException("Configuration must not be null!");
  44.         }
  45.         configuration = config;
  46.     }

  47.     /**
  48.      * Gets the {@code ImmutableConfiguration} used by this object.
  49.      *
  50.      * @return the associated {@code ImmutableConfiguration}
  51.      */
  52.     public ImmutableConfiguration getConfiguration() {
  53.         return configuration;
  54.     }

  55.     /**
  56.      * {@inheritDoc} This implementation calls {@code getProperty()} on the associated configuration. The return value is
  57.      * directly returned. Note that this may be a complex object, for example a collection or an array.
  58.      */
  59.     @Override
  60.     public Object lookup(final String variable) {
  61.         return getConfiguration().getProperty(variable);
  62.     }
  63. }