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