001/* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache license, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the license for the specific language governing permissions and 015 * limitations under the license. 016 */ 017 018package org.apache.logging.log4j.util; 019 020import org.apache.logging.log4j.message.Message; 021import org.apache.logging.log4j.message.MessageFactory; 022 023 024/** 025 * Utility class for lambda support. 026 */ 027public final class LambdaUtil { 028 /** 029 * Private constructor: this class is not intended to be instantiated. 030 */ 031 private LambdaUtil() { 032 } 033 034 /** 035 * Converts an array of lambda expressions into an array of their evaluation results. 036 * 037 * @param suppliers an array of lambda expressions or {@code null} 038 * @return an array containing the results of evaluating the lambda expressions (or {@code null} if the suppliers 039 * array was {@code null} 040 */ 041 public static Object[] getAll(final Supplier<?>... suppliers) { 042 if (suppliers == null) { 043 return null; 044 } 045 final Object[] result = new Object[suppliers.length]; 046 for (int i = 0; i < result.length; i++) { 047 result[i] = get(suppliers[i]); 048 } 049 return result; 050 } 051 052 /** 053 * Returns the result of evaluating the specified function. If the supplied value is of type Message, this method 054 * returns the result of calling {@code #getFormattedMessage} on that Message. 055 * @param supplier a lambda expression or {@code null} 056 * @return the results of evaluating the lambda expression (or {@code null} if the supplier 057 * was {@code null} 058 */ 059 public static Object get(final Supplier<?> supplier) { 060 if (supplier == null) { 061 return null; 062 } 063 final Object result = supplier.get(); 064 return result instanceof Message ? ((Message) result).getFormattedMessage() : result; 065 } 066 067 /** 068 * Returns the Message supplied by the specified function. 069 * @param supplier a lambda expression or {@code null} 070 * @return the Message resulting from evaluating the lambda expression (or {@code null} if the supplier was 071 * {@code null} 072 */ 073 public static Message get(final MessageSupplier supplier) { 074 if (supplier == null) { 075 return null; 076 } 077 return supplier.get(); 078 } 079 080 /** 081 * Returns a Message, either the value supplied by the specified function, or a new Message created by the specified 082 * Factory. 083 * @param supplier a lambda expression or {@code null} 084 * @return the Message resulting from evaluating the lambda expression or the Message created by the factory for 085 * supplied values that are not of type Message 086 */ 087 public static Message getMessage(final Supplier<?> supplier, final MessageFactory messageFactory) { 088 if (supplier == null) { 089 return null; 090 } 091 final Object result = supplier.get(); 092 return result instanceof Message ? (Message) result : messageFactory.newMessage(result); 093 } 094}