View Javadoc
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.StringBuilderFormattable;
21  
22  /**
23   * Mutable Message wrapper around an Object message.
24   * @since 2.6
25   */
26  @PerformanceSensitive("allocation")
27  public class ReusableObjectMessage implements ReusableMessage {
28      private static final long serialVersionUID = 6922476812535519960L;
29  
30      private transient Object obj;
31      private transient String objectString;
32  
33      public void set(Object object) {
34          this.obj = object;
35      }
36  
37      /**
38       * Returns the formatted object message.
39       *
40       * @return the formatted object message.
41       */
42      @Override
43      public String getFormattedMessage() {
44          return String.valueOf(obj);
45      }
46  
47      @Override
48      public void formatTo(final StringBuilder buffer) {
49          if (obj == null || obj instanceof String) {
50              buffer.append((String) obj);
51          } else if (obj instanceof StringBuilderFormattable) {
52              ((StringBuilderFormattable) obj).formatTo(buffer);
53          } else if (obj instanceof CharSequence) {
54              buffer.append((CharSequence) obj);
55          } else {
56              buffer.append(obj);
57          }
58      }
59  
60      /**
61       * Returns the object formatted using its toString method.
62       *
63       * @return the String representation of the object.
64       */
65      @Override
66      public String getFormat() {
67          return getFormattedMessage();
68      }
69  
70      /**
71       * Returns the object as if it were a parameter.
72       *
73       * @return The object.
74       */
75      @Override
76      public Object[] getParameters() {
77          return new Object[] {obj};
78      }
79  
80      @Override
81      public String toString() {
82          return getFormattedMessage();
83      }
84  
85      /**
86       * Gets the message if it is a throwable.
87       *
88       * @return the message if it is a throwable.
89       */
90      @Override
91      public Throwable getThrowable() {
92          return obj instanceof Throwable ? (Throwable) obj : null;
93      }
94  
95      /**
96       * This message does not have any parameters, so this method returns the specified array.
97       * @param emptyReplacement the parameter array to return
98       * @return the specified array
99       */
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 }