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 import org.apache.logging.log4j.message.MessageFactory;
22
23
24 /**
25 * Utility class for lambda support.
26 */
27 public final class LambdaUtil {
28 /**
29 * Private constructor: this class is not intended to be instantiated.
30 */
31 private LambdaUtil() {
32 }
33
34 /**
35 * Converts an array of lambda expressions into an array of their evaluation results.
36 *
37 * @param suppliers an array of lambda expressions or {@code null}
38 * @return an array containing the results of evaluating the lambda expressions (or {@code null} if the suppliers
39 * array was {@code null}
40 */
41 public static Object[] getAll(final Supplier<?>... suppliers) {
42 if (suppliers == null) {
43 return null;
44 }
45 final Object[] result = new Object[suppliers.length];
46 for (int i = 0; i < result.length; i++) {
47 result[i] = get(suppliers[i]);
48 }
49 return result;
50 }
51
52 /**
53 * Returns the result of evaluating the specified function. If the supplied value is of type Message, this method
54 * returns the result of calling {@code #getFormattedMessage} on that Message.
55 * @param supplier a lambda expression or {@code null}
56 * @return the results of evaluating the lambda expression (or {@code null} if the supplier
57 * was {@code null}
58 */
59 public static Object get(final Supplier<?> supplier) {
60 if (supplier == null) {
61 return null;
62 }
63 final Object result = supplier.get();
64 return result instanceof Message ? ((Message) result).getFormattedMessage() : result;
65 }
66
67 /**
68 * Returns the Message supplied by the specified function.
69 * @param supplier a lambda expression or {@code null}
70 * @return the Message resulting from evaluating the lambda expression (or {@code null} if the supplier was
71 * {@code null}
72 */
73 public static Message get(final MessageSupplier supplier) {
74 if (supplier == null) {
75 return null;
76 }
77 return supplier.get();
78 }
79
80 /**
81 * Returns a Message, either the value supplied by the specified function, or a new Message created by the specified
82 * Factory.
83 * @param supplier a lambda expression or {@code null}
84 * @return the Message resulting from evaluating the lambda expression or the Message created by the factory for
85 * supplied values that are not of type Message
86 */
87 public static Message getMessage(final Supplier<?> supplier, final MessageFactory messageFactory) {
88 if (supplier == null) {
89 return null;
90 }
91 final Object result = supplier.get();
92 return result instanceof Message ? (Message) result : messageFactory.newMessage(result);
93 }
94 }