    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 */
    017
    018package org.apache.commons.text.lookup;
    019
    020import java.util.function.BiFunction;
    021import java.util.function.Function;
    022
    023/**
    024 * Lookups a String key for a String value.
    025 * <p>
    026 * This class represents the simplest form of a string to string map. It has a benefit over a map in that it can create
    027 * the result on demand based on the key.
    028 * </p>
    029 * <p>
    030 * For example, it would be possible to implement a lookup that used the key as a primary key, and looked up the value
    031 * on demand from the database.
    032 * </p>
    033 * <p>
    034 * Like {@link BiFunction} is a variant of {@link Function}, this {@code BiStringLookup} is a variant of
    035 * {@link StringLookup}.
    036 * </p>
    037 *
    038 * @param <U> The second argument type.
    039 * @since 1.9
    040 */
    041@FunctionalInterface
    042public interface BiStringLookup<U> extends StringLookup {
    043
    044    /**
    045     * Looks up a String key to provide a String value.
    046     * <p>
    047     * The internal implementation may use any mechanism to return the value. The simplest implementation is to use a
    048     * Map. However, virtually any implementation is possible.
    049     * </p>
    050     * <p>
    051     * For example, it would be possible to implement a lookup that used the key as a primary key, and looked up the
    052     * value on demand from the database Or, a numeric based implementation could be created that treats the key as an
    053     * integer, increments the value and return the result as a string - converting 1 to 2, 15 to 16 etc.
    054     * </p>
    055     * <p>
    056     * This method always returns a String, regardless of the underlying data, by converting it as necessary. For
    057     * example:
    058     * </p>
    059     *
    060     * <pre>
    061     * Map&lt;String, Object&gt; map = new HashMap&lt;String, Object&gt;();
    062     * map.put("number", Integer.valueOf(2));
    063     * assertEquals("2", StringLookupFactory.biFunctionStringLookup(map).lookup("number", "A context object"));
    064     * </pre>
    065     *
    066     * @param key the key to look up, may be null.
    067     * @param object ignored by default.
    068     * @return The matching value, null if no match.
    069     */
    070    default String lookup(final String key, final U object) {
    071        return apply(key);
    072    }
    073
    074}