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