    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;
    018
    019import org.apache.commons.configuration2.interpol.Lookup;
    020
    021/**
    022 * <p>
    023 * A specialized implementation of the {@code Lookup} interface which uses a
    024 * {@code Configuration} object to resolve variables.
    025 * </p>
    026 * <p>
    027 * This class is passed an {@link ImmutableConfiguration} object at construction
    028 * time. In its implementation of the {@code lookup()} method it simply queries
    029 * this configuration for the passed in variable name. So the keys passed to
    030 * {@code lookup()} are mapped directly to configuration properties.
    031 * </p>
    032 *
    033 * @version $Id: ConfigurationLookup.java 1842194 2018-09-27 22:24:23Z ggregory $
    034 * @since 2.0
    035 */
    036public class ConfigurationLookup implements Lookup
    037{
    038    /** The configuration to which lookups are delegated. */
    039    private final ImmutableConfiguration configuration;
    040
    041    /**
    042     * Creates a new instance of {@code ConfigurationLookup} and sets the
    043     * associated {@code ImmutableConfiguration}.
    044     *
    045     * @param config the configuration to use for lookups (must not be
    046     *        <b>null</b>)
    047     * @throws IllegalArgumentException if the configuration is <b>null</b>
    048     */
    049    public ConfigurationLookup(final ImmutableConfiguration config)
    050    {
    051        if (config == null)
    052        {
    053            throw new IllegalArgumentException(
    054                    "Configuration must not be null!");
    055        }
    056        configuration = config;
    057    }
    058
    059    /**
    060     * Returns the {@code ImmutableConfiguration} used by this object.
    061     *
    062     * @return the associated {@code ImmutableConfiguration}
    063     */
    064    public ImmutableConfiguration getConfiguration()
    065    {
    066        return configuration;
    067    }
    068
    069    /**
    070     * {@inheritDoc} This implementation calls {@code getProperty()} on the
    071     * associated configuration. The return value is directly returned. Note
    072     * that this may be a complex object, e.g. a collection or an array.
    073     */
    074    @Override
    075    public Object lookup(final String variable)
    076    {
    077        return getConfiguration().getProperty(variable);
    078    }
    079}