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