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