BiStringLookup.java

  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.  *      http://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. package org.apache.commons.text.lookup;

  18. import java.util.function.BiFunction;
  19. import java.util.function.Function;

  20. /**
  21.  * Lookups a String key for a String value.
  22.  * <p>
  23.  * This class represents the simplest form of a string to string map. It has a benefit over a map in that it can create
  24.  * the result on demand based on the key.
  25.  * </p>
  26.  * <p>
  27.  * For example, it would be possible to implement a lookup that used the key as a primary key, and looked up the value
  28.  * on demand from the database.
  29.  * </p>
  30.  * <p>
  31.  * Like {@link BiFunction} is a variant of {@link Function}, this {@code BiStringLookup} is a variant of
  32.  * {@link StringLookup}.
  33.  * </p>
  34.  *
  35.  * @param <U> The second argument type.
  36.  * @since 1.9
  37.  */
  38. @FunctionalInterface
  39. public interface BiStringLookup<U> extends StringLookup {

  40.     /**
  41.      * Looks up a String key to provide a String value.
  42.      * <p>
  43.      * The internal implementation may use any mechanism to return the value. The simplest implementation is to use a
  44.      * Map. However, virtually any implementation is possible.
  45.      * </p>
  46.      * <p>
  47.      * For example, it would be possible to implement a lookup that used the key as a primary key, and looked up the
  48.      * value on demand from the database Or, a numeric based implementation could be created that treats the key as an
  49.      * integer, increments the value and return the result as a string - converting 1 to 2, 15 to 16 etc.
  50.      * </p>
  51.      * <p>
  52.      * This method always returns a String, regardless of the underlying data, by converting it as necessary. For
  53.      * example:
  54.      * </p>
  55.      *
  56.      * <pre>
  57.      * Map&lt;String, Object&gt; map = new HashMap&lt;String, Object&gt;();
  58.      * map.put("number", Integer.valueOf(2));
  59.      * assertEquals("2", StringLookupFactory.biFunctionStringLookup(map).lookup("number", "A context object"));
  60.      * </pre>
  61.      *
  62.      * @param key the key to look up, may be null.
  63.      * @param object ignored by default.
  64.      * @return The matching value, null if no match.
  65.      */
  66.     default String lookup(final String key, final U object) {
  67.         return lookup(key);
  68.     }

  69. }