BiFunctionStringLookup.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.Map;
  19. import java.util.Objects;
  20. import java.util.function.BiFunction;

  21. /**
  22.  * A function-based lookup where the request for a lookup is answered by applying that function with a key.
  23.  *
  24.  * @param <R> A function's return type
  25.  * @param <P> A function's second input type
  26.  * @since 1.9
  27.  */
  28. final class BiFunctionStringLookup<P, R> implements BiStringLookup<P> {

  29.     /**
  30.      * Creates a new instance backed by a Function.
  31.      *
  32.      * @param <T> the function's input type
  33.      * @param biFunction the function, may be null.
  34.      * @return a new instance backed by the given function.
  35.      */
  36.     static <U, T> BiFunctionStringLookup<U, T> on(final BiFunction<String, U, T> biFunction) {
  37.         return new BiFunctionStringLookup<>(biFunction);
  38.     }

  39.     /**
  40.      * Creates a new instance backed by a Map. Used by the default lookup.
  41.      *
  42.      * @param <T> the map's value type.
  43.      * @param map the map of keys to values, may be null.
  44.      * @return a new instance backed by the given map.
  45.      */
  46.     static <U, T> BiFunctionStringLookup<U, T> on(final Map<String, T> map) {
  47.         return on((key, u) -> map.get(key));
  48.     }

  49.     /**
  50.      * Function.
  51.      */
  52.     private final BiFunction<String, P, R> biFunction;

  53.     /**
  54.      * Creates a new instance backed by a Function.
  55.      *
  56.      * @param biFunction the function, may be null.
  57.      */
  58.     private BiFunctionStringLookup(final BiFunction<String, P, R> biFunction) {
  59.         this.biFunction = biFunction;
  60.     }

  61.     @Override
  62.     public String lookup(final String key) {
  63.         return lookup(key, null);
  64.     }

  65.     /**
  66.      * Looks up a String key by applying the function.
  67.      * <p>
  68.      * If the function is null, then null is returned. The function result object is converted to a string using
  69.      * toString().
  70.      * </p>
  71.      *
  72.      * @param key the key to be looked up, may be null.
  73.      * @return The function result as a string, may be null.
  74.      */
  75.     @Override
  76.     public String lookup(final String key, final P object) {
  77.         if (biFunction == null) {
  78.             return null;
  79.         }
  80.         final R obj;
  81.         try {
  82.             obj = biFunction.apply(key, object);
  83.         } catch (final SecurityException | NullPointerException | IllegalArgumentException e) {
  84.             // Squelched. All lookup(String) will return null.
  85.             // Could be a ConcurrentHashMap and a null key request
  86.             return null;
  87.         }
  88.         return Objects.toString(obj, null);
  89.     }

  90.     @Override
  91.     public String toString() {
  92.         return super.toString() + " [function=" + biFunction + "]";
  93.     }

  94. }