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 does not have any parameters, so this method returns the specified 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        return emptyReplacement;
104    }
105
106    /**
107     * This message does not have any parameters so this method always returns zero.
108     * @return 0 (zero)
109     */
110    @Override
111    public short getParameterCount() {
112        return 0;
113    }
114
115    @Override
116    public <S> void forEachParameter(ParameterConsumer<S> action, S state) {
117        action.accept(obj, 0, state);
118    }
119
120    @Override
121    public Message memento() {
122        return new ObjectMessage(obj);
123    }
124
125    @Override
126    public void clear() {
127        obj = null;
128    }
129}