    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 {@code Configuration} object to resolve
    024 * variables.
    025 * </p>
    026 * <p>
    027 * This class is passed an {@link ImmutableConfiguration} object at construction time. In its implementation of the
    028 * {@code lookup()} method it simply queries this configuration for the passed in variable name. So the keys passed to
    029 * {@code lookup()} are mapped directly to configuration properties.
    030 * </p>
    031 *
    032 * @since 2.0
    033 */
    034public class ConfigurationLookup implements Lookup {
    035    /** The configuration to which lookups are delegated. */
    036    private final ImmutableConfiguration configuration;
    037
    038    /**
    039     * Creates a new instance of {@code ConfigurationLookup} and sets the associated {@code ImmutableConfiguration}.
    040     *
    041     * @param config the configuration to use for lookups (must not be <b>null</b>)
    042     * @throws IllegalArgumentException if the configuration is <b>null</b>
    043     */
    044    public ConfigurationLookup(final ImmutableConfiguration config) {
    045        if (config == null) {
    046            throw new IllegalArgumentException("Configuration must not be null!");
    047        }
    048        configuration = config;
    049    }
    050
    051    /**
    052     * Returns the {@code ImmutableConfiguration} used by this object.
    053     *
    054     * @return the associated {@code ImmutableConfiguration}
    055     */
    056    public ImmutableConfiguration getConfiguration() {
    057        return configuration;
    058    }
    059
    060    /**
    061     * {@inheritDoc} This implementation calls {@code getProperty()} on the associated configuration. The return value is
    062     * directly returned. Note that this may be a complex object, e.g. a collection or an array.
    063     */
    064    @Override
    065    public Object lookup(final String variable) {
    066        return getConfiguration().getProperty(variable);
    067    }
    068}