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; 021import org.apache.logging.log4j.util.StringBuilders; 022 023/** 024 * Mutable Message wrapper around an Object message. 025 * @since 2.6 026 */ 027@PerformanceSensitive("allocation") 028public class ReusableObjectMessage implements ReusableMessage { 029 private static final long serialVersionUID = 6922476812535519960L; 030 031 private transient Object obj; 032 private transient String objectString; 033 034 public void set(final Object object) { 035 this.obj = object; 036 } 037 038 /** 039 * Returns the formatted object message. 040 * 041 * @return the formatted object message. 042 */ 043 @Override 044 public String getFormattedMessage() { 045 return String.valueOf(obj); 046 } 047 048 @Override 049 public void formatTo(final StringBuilder buffer) { 050 StringBuilders.appendValue(buffer, obj); 051 } 052 053 /** 054 * Returns the object formatted using its toString method. 055 * 056 * @return the String representation of the object. 057 */ 058 @Override 059 public String getFormat() { 060 return getFormattedMessage(); 061 } 062 063 /** 064 * Returns the object parameter. 065 * 066 * @return The object. 067 * @since 2.7 068 */ 069 public Object getParameter() { 070 return obj; 071 } 072 073 /** 074 * Returns the object as if it were a parameter. 075 * 076 * @return The object. 077 */ 078 @Override 079 public Object[] getParameters() { 080 return new Object[] {obj}; 081 } 082 083 @Override 084 public String toString() { 085 return getFormattedMessage(); 086 } 087 088 /** 089 * Gets the message if it is a throwable. 090 * 091 * @return the message if it is a throwable. 092 */ 093 @Override 094 public Throwable getThrowable() { 095 return obj instanceof Throwable ? (Throwable) obj : null; 096 } 097 098 /** 099 * This message does not have any parameters, so this method returns the specified array. 100 * @param emptyReplacement the parameter array to return 101 * @return the specified array 102 */ 103 @Override 104 public Object[] swapParameters(final Object[] emptyReplacement) { 105 return emptyReplacement; 106 } 107 108 /** 109 * This message does not have any parameters so this method always returns zero. 110 * @return 0 (zero) 111 */ 112 @Override 113 public short getParameterCount() { 114 return 0; 115 } 116 117 @Override 118 public Message memento() { 119 return new ObjectMessage(obj); 120 } 121}