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  
18  package org.apache.logging.log4j.util;
19  
20  import org.apache.logging.log4j.message.Message;
21  
22  
23  /**
24   * Utility class for lambda support.
25   */
26  public class LambdaUtil {
27      /**
28       * Private constructor: this class is not intended to be instantiated.
29       */
30      private LambdaUtil() {
31      }
32  
33      /**
34       * Converts an array of lambda expressions into an array of their evaluation results.
35       * 
36       * @param suppliers an array of lambda expressions or {@code null}
37       * @return an array containing the results of evaluating the lambda expressions (or {@code null} if the suppliers
38       *         array was {@code null}
39       */
40      public static Object[] getAll(final Supplier<?>... suppliers) {
41          if (suppliers == null) {
42              return null;
43          }
44          final Object[] result = new Object[suppliers.length];
45          for (int i = 0; i < result.length; i++) {
46              result[i] = get(suppliers[i]);
47          }
48          return result;
49      }
50  
51      /**
52       * Returns the result of evaluating the specified function.
53       * @param supplier a lambda expression or {@code null}
54       * @return the results of evaluating the lambda expression (or {@code null} if the supplier
55       *         was {@code null}
56       */
57      public static Object get(final Supplier<?> supplier) {
58          if (supplier == null) {
59              return null;
60          }
61          return supplier.get();
62      }
63  
64      /**
65       * Returns the Message supplied by the specified function.
66       * @param supplier a lambda expression or {@code null}
67       * @return the Message resulting from evaluating the lambda expression (or {@code null} if the supplier was
68       * {@code null}
69       */
70      public static Message get(final MessageSupplier supplier) {
71          if (supplier == null) {
72              return null;
73          }
74          return supplier.get();
75      }
76  }