1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache license, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the license for the specific language governing permissions and
15 * limitations under the license.
16 */
17 package org.apache.logging.log4j.message;
18
19 import org.apache.logging.log4j.util.PerformanceSensitive;
20 import org.apache.logging.log4j.util.StringBuilders;
21
22 /**
23 * Mutable Message wrapper around an Object message.
24 * @since 2.6
25 */
26 @PerformanceSensitive("allocation")
27 public class ReusableObjectMessage implements ReusableMessage, ParameterVisitable {
28 private static final long serialVersionUID = 6922476812535519960L;
29
30 private transient Object obj;
31
32 public void set(final Object object) {
33 this.obj = object;
34 }
35
36 /**
37 * Returns the formatted object message.
38 *
39 * @return the formatted object message.
40 */
41 @Override
42 public String getFormattedMessage() {
43 return String.valueOf(obj);
44 }
45
46 @Override
47 public void formatTo(final StringBuilder buffer) {
48 StringBuilders.appendValue(buffer, obj);
49 }
50
51 /**
52 * Returns the object formatted using its toString method.
53 *
54 * @return the String representation of the object.
55 */
56 @Override
57 public String getFormat() {
58 return obj instanceof String ? (String) obj : null;
59 }
60
61 /**
62 * Returns the object parameter.
63 *
64 * @return The object.
65 * @since 2.7
66 */
67 public Object getParameter() {
68 return obj;
69 }
70
71 /**
72 * Returns the object as if it were a parameter.
73 *
74 * @return The object.
75 */
76 @Override
77 public Object[] getParameters() {
78 return new Object[] {obj};
79 }
80
81 @Override
82 public String toString() {
83 return getFormattedMessage();
84 }
85
86 /**
87 * Gets the message if it is a throwable.
88 *
89 * @return the message if it is a throwable.
90 */
91 @Override
92 public Throwable getThrowable() {
93 return obj instanceof Throwable ? (Throwable) obj : null;
94 }
95
96 /**
97 * This message does not have any parameters, so this method returns the specified array.
98 * @param emptyReplacement the parameter array to return
99 * @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 }