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