FunctionStringLookup.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.Function;

  21. /**
  22.  * A function-based lookup where the request for a lookup is answered by applying that function with a key.
  23.  *
  24.  * @param <V> A function's input type
  25.  * @since 1.9
  26.  */
  27. final class FunctionStringLookup<V> extends AbstractStringLookup {

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

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

  48.     /**
  49.      * Function.
  50.      */
  51.     private final Function<String, V> function;

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

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

  85.     @Override
  86.     public String toString() {
  87.         return super.toString() + " [function=" + function + "]";
  88.     }

  89. }