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 */
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 *
040 * @since 1.9
041 */
042@FunctionalInterface
043public interface BiStringLookup<U> extends StringLookup {
044
045    /**
046     * Looks up a String key to provide a String value.
047     * <p>
048     * The internal implementation may use any mechanism to return the value. The simplest implementation is to use a
049     * Map. However, virtually any implementation is possible.
050     * </p>
051     * <p>
052     * For example, it would be possible to implement a lookup that used the key as a primary key, and looked up the
053     * value on demand from the database Or, a numeric based implementation could be created that treats the key as an
054     * integer, increments the value and return the result as a string - converting 1 to 2, 15 to 16 etc.
055     * </p>
056     * <p>
057     * This method always returns a String, regardless of the underlying data, by converting it as necessary. For
058     * example:
059     * </p>
060     *
061     * <pre>
062     * Map&lt;String, Object&gt; map = new HashMap&lt;String, Object&gt;();
063     * map.put("number", new Integer(2));
064     * assertEquals("2", StringLookupFactory.biFunctionStringLookup(map).lookup("number", "A context object"));
065     * </pre>
066     *
067     * @param key the key to look up, may be null.
068     * @param object ignored by default.
069     * @return The matching value, null if no match.
070     */
071    default String lookup(final String key, final U object) {
072        return lookup(key);
073    }
074
075}