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 */
017package org.apache.logging.log4j.message;
018
019import org.apache.logging.log4j.util.PerformanceSensitive;
020import org.apache.logging.log4j.util.StringBuilderFormattable;
021
022/**
023 * Messages implementing this interface are reused between logging calls.
024 * <p>
025 * If a Message is reusable, downstream components should not hand over this instance to another thread, but extract its
026 * content (via the {@link StringBuilderFormattable#formatTo(StringBuilder)} method) instead.
027 * </p>
028 * @see ReusableMessageFactory
029 * @since 2.6
030 */
031@PerformanceSensitive("allocation")
032public interface ReusableMessage extends Message, StringBuilderFormattable {
033
034    /**
035     * Returns the parameter array that was used to initialize this reusable message and replaces it with the specified
036     * array. The returned parameter array will no longer be modified by this reusable message. The specified array is
037     * now "owned" by this reusable message and can be modified if necessary for the next log event.
038     * </p><p>
039     * ReusableMessages that have no parameters return the specified array.
040     * </p><p>
041     * This method is used by asynchronous loggers to pass the parameter array to a background thread without
042     * allocating new objects.
043     * The actual number of parameters in the returned array can be determined with {@link #getParameterCount()}.
044     * </p>
045     *
046     * @param emptyReplacement the parameter array that can be used for subsequent uses of this reusable message.
047     *         This replacement array must have at least 10 elements (the number of varargs supported by the Logger
048     *         API).
049     * @return the parameter array for the current message content. This may be a vararg array of any length, or it may
050     *         be a reusable array of 10 elements used to hold the unrolled vararg parameters.
051     * @see #getParameterCount()
052     */
053    Object[] swapParameters(Object[] emptyReplacement);
054
055    /**
056     * Returns the number of parameters that was used to initialize this reusable message for the current content.
057     * <p>
058     * The parameter array returned by {@link #swapParameters(Object[])} may be larger than the actual number of
059     * parameters. Callers should use this method to determine how many elements the array contains.
060     * </p>
061     * @return the current number of parameters
062     */
063    short getParameterCount();
064
065    /**
066     * Returns an immutable snapshot of the current internal state of this reusable message. The returned snapshot
067     * will not be affected by subsequent modifications of this reusable message.
068     *
069     * @return an immutable snapshot of this message
070     */
071    Message memento();
072}