1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache license, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 *
9 * https://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the license for the specific language governing permissions and
15 * limitations under the license.
16 */
17
18 package org.apache.commons.text.lookup;
19
20 import java.util.function.BiFunction;
21 import java.util.function.Function;
22
23 /**
24 * Lookups a String key for a String value.
25 * <p>
26 * This class represents the simplest form of a string to string map. It has a benefit over a map in that it can create
27 * the result on demand based on the key.
28 * </p>
29 * <p>
30 * For example, it would be possible to implement a lookup that used the key as a primary key, and looked up the value
31 * on demand from the database.
32 * </p>
33 * <p>
34 * Like {@link BiFunction} is a variant of {@link Function}, this {@code BiStringLookup} is a variant of
35 * {@link StringLookup}.
36 * </p>
37 *
38 * @param <U> The second argument type.
39 * @since 1.9
40 */
41 @FunctionalInterface
42 public interface BiStringLookup<U> extends StringLookup {
43
44 /**
45 * Looks up a String key to provide a String value.
46 * <p>
47 * The internal implementation may use any mechanism to return the value. The simplest implementation is to use a
48 * Map. However, virtually any implementation is possible.
49 * </p>
50 * <p>
51 * For example, it would be possible to implement a lookup that used the key as a primary key, and looked up the
52 * value on demand from the database Or, a numeric based implementation could be created that treats the key as an
53 * integer, increments the value and return the result as a string - converting 1 to 2, 15 to 16 etc.
54 * </p>
55 * <p>
56 * This method always returns a String, regardless of the underlying data, by converting it as necessary. For
57 * example:
58 * </p>
59 *
60 * <pre>
61 * Map<String, Object> map = new HashMap<String, Object>();
62 * map.put("number", Integer.valueOf(2));
63 * assertEquals("2", StringLookupFactory.biFunctionStringLookup(map).lookup("number", "A context object"));
64 * </pre>
65 *
66 * @param key the key to look up, may be null.
67 * @param object ignored by default.
68 * @return The matching value, null if no match.
69 */
70 default String lookup(final String key, final U object) {
71 return apply(key);
72 }
73
74 }