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