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.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, Clearable {
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 has exactly one parameter (the object), so returns it as the first parameter in the 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         // it's unlikely that emptyReplacement is of length 0, but if it is,
104         // go ahead and allocate the memory now;
105         // this saves an allocation in the future when this buffer is re-used
106         if (emptyReplacement.length == 0) {
107             final Object[] params = new Object[10]; // Default reusable parameter buffer size
108             params[0] = obj;
109             return params;
110         }
111         emptyReplacement[0] = obj;
112         return emptyReplacement;
113     }
114 
115     /**
116      * This message has exactly one parameter (the object), so always returns one.
117      * @return 1
118      */
119     @Override
120     public short getParameterCount() {
121         return 1;
122     }
123 
124     @Override
125     public <S> void forEachParameter(final ParameterConsumer<S> action, final S state) {
126         action.accept(obj, 0, state);
127     }
128 
129     @Override
130     public Message memento() {
131         return new ObjectMessage(obj);
132     }
133 
134     @Override
135     public void clear() {
136         obj = null;
137     }
138 }