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 19 import java.util.Map; 20 import java.util.Objects; 21 import java.util.function.BiFunction; 22 23 /** 24 * A function-based lookup where the request for a lookup is answered by applying that function with a key. 25 * 26 * @param <R> A function's return type 27 * @param <P> A function's second input type 28 * @since 1.9 29 */ 30 final class BiFunctionStringLookup<P, R> implements BiStringLookup<P> { 31 32 /** 33 * Creates a new instance backed by a Function. 34 * 35 * @param <T> the function's input type 36 * @param biFunction the function, may be null. 37 * @return a new instance backed by the given function. 38 */ 39 static <U, T> BiFunctionStringLookup<U, T> on(final BiFunction<String, U, T> biFunction) { 40 return new BiFunctionStringLookup<>(biFunction); 41 } 42 43 /** 44 * Creates a new instance backed by a Map. Used by the default lookup. 45 * 46 * @param <T> the map's value type. 47 * @param map the map of keys to values, may be null. 48 * @return a new instance backed by the given map. 49 */ 50 static <U, T> BiFunctionStringLookup<U, T> on(final Map<String, T> map) { 51 return on((key, u) -> map.get(key)); 52 } 53 54 /** 55 * Function. 56 */ 57 private final BiFunction<String, P, R> biFunction; 58 59 /** 60 * Creates a new instance backed by a Function. 61 * 62 * @param biFunction the function, may be null. 63 */ 64 private BiFunctionStringLookup(final BiFunction<String, P, R> biFunction) { 65 this.biFunction = biFunction; 66 } 67 68 @Override 69 public String lookup(final String key) { 70 return lookup(key, null); 71 } 72 73 /** 74 * Looks up a String key by applying the function. 75 * <p> 76 * If the function is null, then null is returned. The function result object is converted to a string using 77 * toString(). 78 * </p> 79 * 80 * @param key the key to be looked up, may be null. 81 * @return The function result as a string, may be null. 82 */ 83 @Override 84 public String lookup(final String key, final P object) { 85 if (biFunction == null) { 86 return null; 87 } 88 final R obj; 89 try { 90 obj = biFunction.apply(key, object); 91 } catch (final SecurityException | NullPointerException | IllegalArgumentException e) { 92 // Squelched. All lookup(String) will return null. 93 // Could be a ConcurrentHashMap and a null key request 94 return null; 95 } 96 return Objects.toString(obj, null); 97 } 98 99 @Override 100 public String toString() { 101 return super.toString() + " [function=" + biFunction + "]"; 102 } 103 104 }