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;
021
022
023/**
024 * Utility class for lambda support.
025 */
026public final class LambdaUtil {
027    /**
028     * Private constructor: this class is not intended to be instantiated.
029     */
030    private LambdaUtil() {
031    }
032
033    /**
034     * Converts an array of lambda expressions into an array of their evaluation results.
035     * 
036     * @param suppliers an array of lambda expressions or {@code null}
037     * @return an array containing the results of evaluating the lambda expressions (or {@code null} if the suppliers
038     *         array was {@code null}
039     */
040    public static Object[] getAll(final Supplier<?>... suppliers) {
041        if (suppliers == null) {
042            return null;
043        }
044        final Object[] result = new Object[suppliers.length];
045        for (int i = 0; i < result.length; i++) {
046            result[i] = get(suppliers[i]);
047        }
048        return result;
049    }
050
051    /**
052     * Returns the result of evaluating the specified function.
053     * @param supplier a lambda expression or {@code null}
054     * @return the results of evaluating the lambda expression (or {@code null} if the supplier
055     *         was {@code null}
056     */
057    public static Object get(final Supplier<?> supplier) {
058        if (supplier == null) {
059            return null;
060        }
061        return supplier.get();
062    }
063
064    /**
065     * Returns the Message supplied by the specified function.
066     * @param supplier a lambda expression or {@code null}
067     * @return the Message resulting from evaluating the lambda expression (or {@code null} if the supplier was
068     * {@code null}
069     */
070    public static Message get(final MessageSupplier supplier) {
071        if (supplier == null) {
072            return null;
073        }
074        return supplier.get();
075    }
076}