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.StringBuilders;
021
022/**
023 * Mutable Message wrapper around an Object message.
024 * @since 2.6
025 */
026@PerformanceSensitive("allocation")
027public class ReusableObjectMessage implements ReusableMessage, ParameterVisitable, Clearable {
028    private static final long serialVersionUID = 6922476812535519960L;
029
030    private transient Object obj;
031
032    public void set(final Object object) {
033        this.obj = object;
034    }
035
036    /**
037     * Returns the formatted object message.
038     *
039     * @return the formatted object message.
040     */
041    @Override
042    public String getFormattedMessage() {
043        return String.valueOf(obj);
044    }
045
046    @Override
047    public void formatTo(final StringBuilder buffer) {
048        StringBuilders.appendValue(buffer, obj);
049    }
050
051    /**
052     * Returns the object formatted using its toString method.
053     *
054     * @return the String representation of the object.
055     */
056    @Override
057    public String getFormat() {
058        return obj instanceof String ? (String) obj : null;
059    }
060
061    /**
062     * Returns the object parameter.
063     *
064     * @return The object.
065     * @since 2.7
066     */
067    public Object getParameter() {
068        return obj;
069    }
070
071    /**
072     * Returns the object as if it were a parameter.
073     *
074     * @return The object.
075     */
076    @Override
077    public Object[] getParameters() {
078        return new Object[] {obj};
079    }
080
081    @Override
082    public String toString() {
083        return getFormattedMessage();
084    }
085
086    /**
087     * Gets the message if it is a throwable.
088     *
089     * @return the message if it is a throwable.
090     */
091    @Override
092    public Throwable getThrowable() {
093        return obj instanceof Throwable ? (Throwable) obj : null;
094    }
095
096    /**
097     * This message has exactly one parameter (the object), so returns it as the first parameter in the array.
098     * @param emptyReplacement the parameter array to return
099     * @return the specified array
100     */
101    @Override
102    public Object[] swapParameters(final Object[] emptyReplacement) {
103        // it's unlikely that emptyReplacement is of length 0, but if it is,
104        // go ahead and allocate the memory now;
105        // this saves an allocation in the future when this buffer is re-used
106        if (emptyReplacement.length == 0) {
107            final Object[] params = new Object[10]; // Default reusable parameter buffer size
108            params[0] = obj;
109            return params;
110        }
111        emptyReplacement[0] = obj;
112        return emptyReplacement;
113    }
114
115    /**
116     * This message has exactly one parameter (the object), so always returns one.
117     * @return 1
118     */
119    @Override
120    public short getParameterCount() {
121        return 1;
122    }
123
124    @Override
125    public <S> void forEachParameter(final ParameterConsumer<S> action, final S state) {
126        action.accept(obj, 0, state);
127    }
128
129    @Override
130    public Message memento() {
131        return new ObjectMessage(obj);
132    }
133
134    @Override
135    public void clear() {
136        obj = null;
137    }
138}