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