    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 * @since 2.0
    034 */
    035public class ConfigurationLookup implements Lookup
    036{
    037    /** The configuration to which lookups are delegated. */
    038    private final ImmutableConfiguration configuration;
    039
    040    /**
    041     * Creates a new instance of {@code ConfigurationLookup} and sets the
    042     * associated {@code ImmutableConfiguration}.
    043     *
    044     * @param config the configuration to use for lookups (must not be
    045     *        <b>null</b>)
    046     * @throws IllegalArgumentException if the configuration is <b>null</b>
    047     */
    048    public ConfigurationLookup(final ImmutableConfiguration config)
    049    {
    050        if (config == null)
    051        {
    052            throw new IllegalArgumentException(
    053                    "Configuration must not be null!");
    054        }
    055        configuration = config;
    056    }
    057
    058    /**
    059     * Returns the {@code ImmutableConfiguration} used by this object.
    060     *
    061     * @return the associated {@code ImmutableConfiguration}
    062     */
    063    public ImmutableConfiguration getConfiguration()
    064    {
    065        return configuration;
    066    }
    067
    068    /**
    069     * {@inheritDoc} This implementation calls {@code getProperty()} on the
    070     * associated configuration. The return value is directly returned. Note
    071     * that this may be a complex object, e.g. a collection or an array.
    072     */
    073    @Override
    074    public Object lookup(final String variable)
    075    {
    076        return getConfiguration().getProperty(variable);
    077    }
    078}