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.UnaryOperator; 021 022/** 023 * Lookups a String key for a String value. 024 * <p> 025 * This class represents the simplest form of a string to string map. It has a benefit over a map in that it can create 026 * the result on demand based on the key. 027 * </p> 028 * <p> 029 * For example, it would be possible to implement a lookup that used the key as a primary key, and looked up the value 030 * on demand from the database. 031 * </p> 032 * 033 * @since 1.3 034 */ 035@FunctionalInterface 036public interface StringLookup extends UnaryOperator<String> { 037 038 /** 039 * Looks up a String key to provide a String value. 040 * <p> 041 * The internal implementation may use any mechanism to return the value. The simplest implementation is to use a 042 * Map. However, virtually any implementation is possible. 043 * </p> 044 * <p> 045 * For example, it would be possible to implement a lookup that used the key as a primary key, and looked up the 046 * value on demand from the database Or, a numeric based implementation could be created that treats the key as an 047 * integer, increments the value and return the result as a string - converting 1 to 2, 15 to 16 etc. 048 * </p> 049 * <p> 050 * This method always returns a String, regardless of the underlying data, by converting it as necessary. For 051 * example: 052 * </p> 053 * 054 * <pre> 055 * Map<String, Object> map = new HashMap<String, Object>(); 056 * map.put("number", Integer.valueOf(2)); 057 * assertEquals("2", StringLookupFactory.mapStringLookup(map).lookup("number")); 058 * </pre> 059 * 060 * @param key the key to look up, may be null. 061 * @return The matching value, null if no match. 062 * @since 1.14.0 063 */ 064 @Override 065 default String apply(final String key) { 066 return lookup(key); 067 } 068 069 /** 070 * Looks up a String key to provide a String value. 071 * <p> 072 * The internal implementation may use any mechanism to return the value. The simplest implementation is to use a 073 * Map. However, virtually any implementation is possible. 074 * </p> 075 * <p> 076 * For example, it would be possible to implement a lookup that used the key as a primary key, and looked up the 077 * value on demand from the database Or, a numeric based implementation could be created that treats the key as an 078 * integer, increments the value and return the result as a string - converting 1 to 2, 15 to 16 etc. 079 * </p> 080 * <p> 081 * This method always returns a String, regardless of the underlying data, by converting it as necessary. For 082 * example: 083 * </p> 084 * 085 * <pre> 086 * Map<String, Object> map = new HashMap<String, Object>(); 087 * map.put("number", Integer.valueOf(2)); 088 * assertEquals("2", StringLookupFactory.mapStringLookup(map).lookup("number")); 089 * </pre> 090 * 091 * @param key the key to look up, may be null. 092 * @return The matching value, null if no match. 093 * @deprecated Use {@link #apply(String)}. 094 */ 095 @Deprecated 096 String lookup(String key); 097}