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